Issues with code with Arduino, Kinect, and moving 2 Servo Motors

edited March 2015 in Arduino

Hello! I have been working on a project with the Arduino and Kinect by moving a servo motor based on the closest depth value. This worked successfully, but when I split the screen into two, I was unable to get both motors to work separately (based off of two different closest values). I know that the Kinect is able to sense the two different values, but I am not sure where I am going wrong! There is some issue with the second motor (in port 9/closestdepthvalue2 in my code). It won't move at all, like it's not registering the values sent from Processing. There is nothing wrong with my servo motors, and my Arduino board seems to be working fine. If someone could look at my code to see if there are any glaring errors/any tips that you have, that would be so nice and helpful! :) I'm sorry, I'm just new at programming and robotics, and I don't quite have the skills to do this debugging all by myself. I'll post my Processing code and Arduino code at the end of the post. Thank you!

Here's some more information on my code, if it will help: I use the closest depth value from the one half of the screen to send information to my first servo motor using closestDepthValue1, and I send the other half to the second servo using closestDepthValue2. Because I want the two servos to have the same angles, I sent the same value to each depending on the depth, but just incremented by 200 to differentiate. So closestDepthValue2 would send 220 as a value, but the motor would hopefully only move to 20 degrees. (Is there a more efficient/practical way to do this?) I also print the depth image from the Kinect to see what the camera is actually sensing, and that seems to work pretty well, just that Processing is not communicating with the Arduino correctly.

Thank you again! Sorry for the inconvenience! :)

Arduino Code: #include <Servo.h> Servo one; Servo two; int nextServo = 0; int servoAngles = 0; void setup() { one.attach(8); two.attach(9); Serial.begin(9600); } void loop() { if(Serial.available()) { int servoAngle = Serial.read(); if(servoAngle <= 160) { servoAngles = servoAngle; one.write(servoAngles); } if(servoAngle > 160) { servoAngles = servoAngle - 200; two.write(servoAngles); } } }

Processing Code: `import processing.serial.*; Serial myPort; import SimpleOpenNI.*; SimpleOpenNI kinect; int closestValue1; int closestValue2; int closestX; int closestY; int closestP; int closestM; void setup() { size(640, 480); String portName = Serial.list()[4]; myPort = new Serial(this, portName, 9600); kinect = new SimpleOpenNI(this); kinect.enableDepth(); } void draw() { closestValue1 = 8000; closestValue2 = 8000; kinect.update(); int[] depthValues = kinect.depthMap(); for (int y = 0; y < 480; y++) { for (int x = 0; x < 320; x++) { int i = x + y * 320; int currentDepthValue1 = depthValues[i]; if (currentDepthValue1 > 0 && currentDepthValue1 < closestValue1) { closestValue1 = currentDepthValue1; closestX = x; closestY = y; if (currentDepthValue1 >= 890) { myPort.write(20); } if (currentDepthValue1 >= 803 && currentDepthValue1 < 890) { myPort.write(90); } if (currentDepthValue1 < 803) { myPort.write(160); } } } } for (int p = 0; p < 480; p++) { for (int m = 320; m < 640; m++) { int t = m + p * 320; int currentDepthValue2 = depthValues[t]; if (currentDepthValue2 > 0 && currentDepthValue2 < closestValue2) { closestValue2 = currentDepthValue2; closestM = m; closestP = p; if (currentDepthValue2 >= 890) { myPort.write(220); } if (currentDepthValue2 >= 803 && currentDepthValue2 < 890) { myPort.write(290); } if (currentDepthValue2 < 803) { myPort.write(360); } } } }

image(kinect.depthImage(), 0, 0); fill(255, 0, 0); ellipse(closestX, closestY, 25, 25); ellipse(closestM, closestP, 25, 25); }`

Answers

  • And oh my goodness I cannot format the Processing code for the life of me, so I completely understand if none of you would bother to read it. Sorry!

  • Your formated code should look like havina Tab space from the beginning of the line. Select entire cose and precc C button above or Ctrl+O

Sign In or Register to comment.