I wanted to create a robotic arm which could move in an harmonic way and in this project, I have used it to play to tic-tac-toe.
The robotic arm is controlled by Arduino Due (but you can use any Arduino board, even Arduino Uno)
I use as input a IR remote controller in order to choose both who starts (Arduino or player) and where you put your pawn. I used the minimax algorithm of pietrotofy.it, because I was focused on controlling the movement of the 6DOF arm.
Here's how I built it...STEP ONE: Assembly
I calibrated the 6 MG966R servos, because every servo has a different pulse width for 0 degrees and 180 degrees. Then i assembled the kit ROTU3 with the 6 servo and i created a simple code for controlling and testing the arm, in which i could move only one servo by one.
Moving the servo one by one was boring and the arm was difficult to control, so i thougth it would have been better sending to Arduino the three cordinates (x,y,z), and changing these, it would have calculated the angle of all the 6 servos.
In order to do this, I created a 3D simulation on GeoGebra and with a bit of trigonometry I these were the results
Then I have implemented the code on Arduino ( with the right regulations ). Now, changing only three parameters (x,y,z) I could control the arm in an easy and intuitive way.STEP THREE: Geometric calculation
In this paragrafh I'll show you how Arduino find the the angle of all the servos on the base of the three coordinates (x,y,z).
We know that:
- ‘A’ is the origin of our Cartesian sistem, so it has cordinates of 0,0,0..
- ‘D’ is the point that we have to reach;
- AB,BC,CD (humerus,ulna,gripper) are the lengths of each arm segments.
We want to know the angle in A,B,C.
Firstly we calculate AD with Pythagorean theorem ( I prefer that the point z = 0, is on the ground, so I put z = z-BaseHeight; however this doesn't change anything for user ).
Secondly, we make assumption that angle B and C are the same and to simplify our calculation we add the point ‘E’. The triangle BCE is isosceles, so BE = CE. This triangle can be divided in two right triangle, so:
We know all the angle, so we can send the angle with opporune regulation to the servos (in my case adding or subtracting 90° due to MY arm's frame). Now we can control our arm. Well yes, but there are some problems when
- 'z' or 'y' are negative; nothing have been changing since when they are positive
- If the coordinates are too big or too small it simply goes crazy.
The last problem is easier to correct, we have to add some limitation ( I have called them checkmin and checkmax) but to calculate them, we have to found where the calculation can give impossible result. The problem is in alfa, infact we have a square root, so we put everything is under that greater than zero (checkmin); moreover the function 'arrcos' accept valor from -1 to 1, so we set this limit (checkmax).
If we want that it reaches also negative 'z', we simply give at the second servo non gamma+omega, but gamma-omega.
Instead if we want that the arm goes to negative 'y', we gave to all the servos 180-the angle we should have given to them.STEP FOUR: Improvements
After the first tries, I noticed that the arm movement were too fast, and therefore it was not nice to look and sometimes it crashed into something. So I written a function ( MyServoWriteGradual() ), where if the variation of servo position is too big, this function move the servos all togheter in a period of time which depends on the variation of the biggest position. (for example if a servo has to move of 50 degrees, the servo will arrive in position after 500 milliseconds, if it has to move of 100, it will arrive in 1000 millisecond).
STEP FOUR: Starting play tic-tac-toe
After that I adapted the code of pierotofy.it to my needs, I implemented it on arduino.