I am trying to make a 2dof robotic arm that can draw (for a school project). It is controlled by a joystick, the plan being that the arm moves in the direction the joystick is pushed. I have done some inverse kinematics and the equations definitely work as I have tested them on graphs, however I am pretty new to Arduino, and am struggling to get the code to work. It still doesn't draw in straight lines, and once past a certain point, it randomly starts spasming. Any help?
- I have attached the code, and a photo of the robot (both joints are 11.5cm long)
straight_line2.ino (2.11 KB)
If you are trying to power the servos from the Arduino 5V output, that is a problem. Use 4xAA instead, and connect all the grounds.
It looks like you have debug output, so do the values of deg1 and deg2 jump suddenly at the time it goes crazy?
Failure to draw in straight lines is probably due to the joystick moving faster than the motors can keep up with. To fix it you might try limiting the maximum linear speed it will move. Something like
deltaX = newX - oldX;
deltaY = newY - oldY;
speed = sqrt(deltaX*deltaX + deltaY*deltaY) / (newTime - oldTime);
if (speed > maxSpeed)
{
newX = oldX + deltaX * maxSpeed / speed;
newY = oldY + deltaY * maxSpeed / speed;
}
Then calculate the joint angles from newX and newY. Alternatively you could do speed limiting based on the rate of change of the joint angles, but this way is easier.