Go Down

### Topic: Differential drive from Joystick (Read 654 times)previous topic - next topic

#### AntonyG ##### Feb 10, 2018, 09:19 pmLast Edit: Feb 14, 2018, 10:10 pm by AntonyG
Hi.
Please forgive me as its my first post here.. Not asking for help I have been working on getting a Differential drive for my project. Lots of snippets of code around but not what I wanted for my project.

Code: [Select]
` /* With a little help from :-  http://home.kendra.com/mauser/joystick.html  http://forum.arduino.cc/index.php?topic=114063.0  Gives me 100% forward speed and 50% revirse speed.  1.Get X and Y from the Joystick, do whatever scaling and calibrating you need to do based on your hardware.  2.Invert X  3.Calculate R+L (Call it V): V =(100-ABS(X)) * (Y/100) + Y  4.Calculate R-L (Call it W): W= (100-ABS(Y)) * (X/100) + X  5.Calculate R: R = (V+W) /2  6.Calculate L: L= (V-W)/2  7.Do any scaling on R and L your hardware may require.  8.Send those values to your Robot.  9.Go back to 1.*/int Deadzone = 4;int coordX = 0;                  // variable to read the value from the analog pin 0int coordY = 0;                  // variable to read the value from the analog pin 1void setup(){  Serial.begin(9600);}void loop() {  coordY = analogRead(2);   //cordY min = 7, Mid = 519,  Max = 1018  coordX = analogRead(3);     //cordX Min = 3, Mid = 522,  Max = 1020  int coordXmapped = map(coordX, 0, 1023, 100, -100); //maps from 0-1023 POT range to -100 - 100 range of formula  int coordYmapped = map(coordY, 0, 1023, 100, -100); //maps from 0-1023 POT range to -100 - 100 range of formula  int coordXinvert = (coordXmapped * -1);  int v = ((100 - abs(coordXinvert)) * (coordYmapped / 100) + coordYmapped);  int w = ((100 - abs(coordYmapped)) * (coordXinvert / 100) + coordXinvert);  int rightwheel = ((w - v) / 1);  int leftwheel = ((v + w) / 1);  int Left_motor = abs(leftwheel) < Deadzone ? 0 : leftwheel;      // add dead zone from Joystick  int Right_motor = abs(rightwheel) < Deadzone ? 0 : rightwheel;     //  add dead zone from Joystick  }`
Well Iv'e managed to  insert my code in the correct manner.
As im'e new to the Arduino I would like to be told what right and what's NOT right.
Hope someone finds my work usefull.

Regards Antony.

Just trying to learn so to understand how the world around me works.

Go Up