Go Down

Topic: Sweeping servo program question (Read 1 time) previous topic - next topic

PaulS

The value in the write() call IS the angle to turn to, so I don't understand your difficulty.

The Serial.read() function reads bytes, which can hold a value between 0 and 255. So, the simplest thing is for the PC to send the new value to go to as a byte, not a string. Then, simply call Servo.write() with the value read from the serial port. The PC app should also send an ID to define which servo the value is for.

Something like this would work on the Arduino, then.

Code: [Select]
Servo pan;
Servo tlt;

void loop()
{
   if(Serial.available() > 1) // Need two bytes ('T' & n or 'P' & n)
   {
      char which = Serial.read();
      int thePos = Serial.read();

      if(which == 'T')
         tlt.write(thePos);
      else if(which == 'P')
         pan.write(thePos);
   }
}

mblackwolf

Ok. I am gonna try to explain what I mean. I know the value in write is the angle to turn to. However, If I have the servo panning the angle sent from the serial isnt the angle needed by the servo. I am using digital servos so I can slow down the rotation speed so to sweep i can use.

myservo.write(0)
myservo.write(180)

which will sweep the full 180 degrees. Now however say the camera picks up a red object at 90 degrees. It used the pixel and view angle values to calculate the needed angle of rotation based on where the servo is now. Lets say the program determine the servo needs to rotate 15 degrees furthur from the current location. If it was to send this and use your way the byte value sent would be 15. So the servo would then rotate backward to 15 degrees when I actually intended it to go to 105 degrees. I could write the programmer code to write the actual rotation angle needed but I would need some feedback of the current servo location.

PaulS

Quote
I could write the programmer code to write the actual rotation angle needed but I would need some feedback of the current servo location.

If the PC is totally in charge of servo position, then, no, you don't need any feedback. Not that you'd get any, anyway, since if you try to read a servo's location, the value you get back is the last value you told it to go to, regardless of whether it was able to get there, or not.

mblackwolf

I think that answers my question without the feedback I am either going to have to step through the sweep so I know the approximate position or keep the camera stationary so that the exact angle of rotation needed can be found. thanks for your help. The only other option I have thought of is possibly using hacked full rotation servos and instuting some form of PID control where the programmer code will give a left or right value based on how far it need to move and give a 90 degree (no motion) value if the object is in the center. I may see how this works out. Thanks for your help though I do appreciate it.

PaulS

If actual position information is essential, a real rotary encoder would be better than hacking a servo. Much higher resolution and repeatability.

I don't understand why the actual angle is critical. If the computer is tracking something, and it appears to be moving to the left, it should be telling the servo to move to the right. If the object appears to be moving while the servo isn't, it is either because the servo is drifting (unlikely), the object being tracked is moving (likely), or because the servo is drifting. In any event, the simplest solution is simply for the PC to tell the Arduino three values, instead - which servo, which direction, and how far ('P' '+' 2), and have the Arduino figure out which angle to send the servo to. If the new position isn't right, the PC will send a new increment.

It doesn't matter which way the servo is actually pointing. It's either pointing where the computer wants or it isn't. The PC can issue corrections until the Arduino gets it right.

Go Up