i could really use some help with my arduino project it is 2 motors controlled in tandem by a joystick, or it would be if i could get it to work. i have a code for the motors to run separately it is this:
#include <Servo.h>
const int servo1 = 3; // first servo
const int servo2 = 10; // second servo
const int joyH = 3; // L/R Parallax Thumbstick
const int joyV = 4; // U/D Parallax Thumbstick
int servoVal; // variable to read the value from the analog pin
Servo myservo1;
Servo myservo2;
void setup() {
myservo1.attach(servo1);
myservo2.attach(servo2);
Serial.begin(9600);
}
void loop(){
outputJoystick();
servoVal = analogRead(joyH);
servoVal = map(servoVal, 0, 1023,1000, 1750);
myservo2.write(servoVal);
servoVal = analogRead(joyV);
servoVal = map(servoVal, 0, 1023, 1250, 1750);
myservo1.write(servoVal);
delay(15);
}
/**
* Display joystick values
*/
void outputJoystick(){
Serial.print(analogRead(joyH));
Serial.print ("---");
Serial.print(analogRead(joyV));
Serial.println ("----------------");
}
but what i need it to do is run both of the DC motors in tandem and the bot needs to be able to make zero point turns. any help at all would be extremely helpful, thank you for your time
run both of the DC motors in tandem and the bot needs to be able to make zero point turns
"Tandem" implies, to me, one behind the other, so I don't see how you could make a zero turn.
Please use code tags when posting code.
sorry i meant i need them both to run parallel to each other at the same time, any advice?
sorry i meant i need them both to run parallel to each other at the same time, any advice?
Does this mean you want the servos to turn in opposite directions to make the zero point turns? If so, does your joystick have a trigger or button?
yes i want them to go opposite directions when making the turns and, no it does not have a trigger or switch. is it possible to make them both go the same way when the joystick is in the forward or backwards positions and go opposite directions when in the left or right positions? if so please help, any advice for coding this would be appreciated.
The problem you may have with your setup is that a joystick is capable of having both your forward/reverse and turn commands at the same time (like joystick pushed forward and to the left at the same time). You might put the stick shaft in a + cutout such that the joystick can't be put in a diagonal position. A code fix might be possible by making exclusive loops for forward/reverse and turning.
im not actually sure about this because im new and i havent tried it. but you can try and add the Horizontal and Vertical values together to produce a factor which the motors move.
RightMotorValue = Vertical - Horizontal;
LeftMotorValue = Vertical + Horizontal;
so when the joystick is all the way to the left, R = 0 - (-joyval), L = 0 + (-joyval) making it turn left.
the only problem with this method is when the joystick is on one of the corners you get double the joyval.
this might seem like an odd question but regardless, is it possible to use the style of code that i have been using to run the motors parallel at all times? if so does anyone have any advice how? Ive become stuck.
this might seem like an odd question but regardless, is it possible to use the style of code that i have been using to run the motors parallel at all times? if so does anyone have any advice how? Ive become stuck.
If you mean have the opposite side continuous rotation servos move forward and reverse together, then the below is a usual way where the pot value is mapped to 0-180 deg.
myservo1.write(n);
myservo2.write(180-n);
it worked...kinda, the code made the 1 run continually in reverse, what i need is for when the joystick value is above the center value heading forward on the joystick to have both motors run forward (same idea in reverse), and when the joystick is headed left the two values are one half of the value forward for one motor and the other motors value is half the value going backwards. sorry if i seem clueless as i kinda am towards complex programming. once again any help would be appreciated.
once again any help would be appreciated.
You go first. Post the code you have.
The change you propose was what I considered when I first read your topic. They should be easy to implement.
The only issue with your proposal is that you won't be able to spin on a dime.
If, instead, you use the left and right amount as a differential to be applied to both motors (one positive, one negative), then when the stick is centered fore and aft (at the midpoint of the throw), then you can still have a positive and negative wheel speed, and can spin in place. The speed at which you spin is related to how far left or right you move the stick. Which way you move the stick controls the direction of spin.
#include <Servo.h>
const int servo1 = 3; // first servo
const int servo2 = 10; // second servo
const int joyH = 3; // L/R Parallax Thumbstick
const int joyV = 4; // U/D Parallax Thumbstick
int servoVal1; // variable to read the value from the analog pin
int servoVal2;
int servoVal3; // variable to read the value from the analog pin
int servoVal4;
Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
void setup() {
// Servo
myservo1.attach(servo1); // attaches the servo
myservo2.attach(servo2); // attaches the servo
// Inizialize Serial
Serial.begin(9600);
}
void loop(){
// Display Joystick values using the serial monitor
outputJoystick();
servoVal3 = analogRead(joyV);
servoVal4 = analogRead(joyV);
servoVal3 = map(servoVal3, 0, 1023,1000, 1750); // scale it to use it with the servo (result between 0 and 180)
servoVal4 = map(servoVal4, 0, 1023, 1250, 1750); // scale it to use it with the servo (result between 70 and 180)
// Read the horizontal joystick value (value between 0 and 1023)
servoVal1 = analogRead(joyH);
servoVal2 = analogRead(joyH);
servoVal1 = map(servoVal1, 0, 1023,1000, 1750); // scale it to use it with the servo (result between 0 and 180)
servoVal2 = map(servoVal2, 0, 1023, 1250, 1750); // scale it to use it with the servo (result between 70 and 180)
myservo2.write(servoVal1); // sets the servo position according to the scaled value
myservo1.write(servoVal2);
delay(15); // waits for the servo to get there
}
/**
* Display joystick values
*/
void outputJoystick(){
Serial.print(analogRead(joyH));
Serial.print ("---");
Serial.print(analogRead(joyV));
Serial.println ("----------------");
}
this is the new code i came up with to run the robot to make zero point turns, the issue is now that i cant figure out how to make the bot run in parallel when moving forwards and backwards, any ideas?
Below is some pot test code. You might make something similar using "while" loops to trap the input of one of the pots while it is not centered, excluding any control input from the other pot. That should prevent control conflicts between forward/reverse and clockwise/counterclockwise servo operation.
//zoomkat dual pot/servo test 12-29-12
//view output using the serial monitor
#include <Servo.h>
Servo myservo1;
Servo myservo2;
int potpin1 = 0; //analog input pin A0
int potpin2 = 1;
int newval1, oldval1;
int newval2, oldval2;
void setup()
{
Serial.begin(9600);
myservo1.attach(2);
myservo2.attach(3);
Serial.println("testing dual pot servo");
}
void loop()
{
newval1 = analogRead(potpin1);
newval1 = map(newval1, 0, 1023, 0, 1;
if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){
myservo1.write(newval1);
Serial.print("1- ");
Serial.println(newval1);
oldval1=newval1;
}
newval2 = analogRead(potpin2);
newval2 = map(newval2, 0, 1023, 0, 179);
if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){
myservo2.write(newval2);
Serial.print("2- ");
Serial.println(newval2);
oldval2=newval2;
}
delay(50);
}
I did something simular with DC motors. It uses 2 variable "z" -forward movement and "y" - left and right movement.
Note this is only a snip of the full code, the rest is just receiving the data and sending it to here. You may also be able to alter it to use servos instead of DC motors.
Hope this gives you some ideas.
int DRV2 = map(z, -15, 0, 255, 0);
int DRV1 = map(z, 0, 15, 0, 255);
int STRL = map(y, -10, 0, 255, 0);
int STRR = map(y, 0, 10, 0, 255);
//constrain((DRV1,DRV2,STRL,STRR),0,255);
/*Serial.print(DRV1);
Serial.print(",");
Serial.print(DRV2);
Serial.print(",");
Serial.print(STRL);
Serial.print(",");
Serial.print(STRR);
//Serial.print(",");
//Serial.print(y);
Serial.println();*/
if(z > 0)//forwards with skew left or right
{
//Serial.println("Driving");
analogWrite(M1L, constrain(abs(DRV1 - STRL),0,255)); analogWrite(M1R, constrain(abs(DRV1 - STRR),0,255));
digitalWrite(M2L, LOW); digitalWrite(M2R, LOW);
}
else if(z < 0)//backwards with skew left or right
{
//Serial.println("Driving");
digitalWrite(M1L, LOW); digitalWrite(M1R, LOW);
analogWrite(M2L, constrain(abs(DRV2 - STRL),0,255)); analogWrite(M2R, constrain(abs(DRV2 - STRR),0,255));
}
else if(z < 4 && z > -4 && y > 0)//Right (+-4 = limits before movement)
{
//Serial.println("Driving");
digitalWrite(M2L, LOW); analogWrite(M2R, STRR);
analogWrite(M1L, STRR); digitalWrite(M1R, LOW);
}
else if(z < 4 && z > -4 && y < 0)//Left
{
//Serial.println("Driving");
analogWrite(M2L, STRL); digitalWrite(M2R, LOW);
digitalWrite(M1L, LOW); analogWrite(M1R, STRL);
}
else //full stop
{
digitalWrite(M1L, LOW); digitalWrite(M1R, LOW);
digitalWrite(M2L, LOW); digitalWrite(M2R, LOW);
}
#include <Servo.h>
const int servo1 = 3; // first servo
const int servo2 = 10; // second servo
const int joyH = 3; // L/R Parallax Thumbstick
const int joyV = 4; // U/D Parallax Thumbstick
int servoVal1; // variable to read the value from the analog pin
int servoVal2;
int servoVal3; // variable to read the value from the analog pin
int servoVal4;
Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
void setup() {
// Servo
myservo1.attach(servo1); // attaches the servo
myservo2.attach(servo2); // attaches the servo
// Inizialize Serial
Serial.begin(9600);
}
void loop(){
// Display Joystick values using the serial monitor
outputJoystick();
servoVal3 = analogRead(joyV);
servoVal4 = analogRead(joyV);
servoVal3 = map(servoVal3, 0, 1023,1000, 1750); // scale it to use it with the servo (result between 0 and 180)
servoVal4 = map(servoVal4, 0, 1023, 1250, 1750); // scale it to use it with the servo (result between 70 and 180)
// Read the horizontal joystick value (value between 0 and 1023)
servoVal1 = analogRead(joyH);
servoVal2 = analogRead(joyH);
servoVal1 = map(servoVal1, 0, 1023,1000, 1750); // scale it to use it with the servo (result between 0 and 180)
servoVal2 = map(servoVal2, 0, 1023, 1250, 1750); // scale it to use it with the servo (result between 70 and 180)
myservo2.write(servoVal1); // sets the servo position according to the scaled value
myservo1.write(servoVal2);
delay(15); // waits for the servo to get there
}
/**
* Display joystick values
*/
void outputJoystick(){
Serial.print(analogRead(joyH));
Serial.print ("---");
Serial.print(analogRead(joyV));
Serial.println ("----------------");
}
i managed to get the 2 motors to make zero point turns, however i now cant get any headway into running at the same time to go forwards and backwards. any ideas/ help would be massively appreciated.
Why do this?
servoVal3 = map(servoVal3, 0, 1023,1000, 1750); // scale it to use it with the servo (result between 0 and 180)
servoVal4 = map(servoVal4, 0, 1023, 1250, 1750); // scale it to use it with the servo (result between 70 and 180)
.....
servoVal1 = map(servoVal1, 0, 1023,1000, 1750); // scale it to use it with the servo (result between 0 and 180)
servoVal2 = map(servoVal2, 0, 1023, 1250, 1750); // scale it to use it with the servo (result between 70 and 180)
Don't you want to get values between 0 - 90, and 90 - 180? You need to use IF statements like how I have done. If you don't isolate the values, and use them when you need them, you have problems controlling the robot.
Don't you want to get values between 0 - 90, and 90 - 180?
No, the "write" method interprets values outside the normal range of "angles" as pulse lengths expressed in microseconds.
Those mapping ranges look OK to me.
i managed to get the 2 motors to make zero point turns, however i now cant get any headway into running at the same time to go forwards and backwards. any ideas/ help would be massively appreciated.
I'd first make two separate pieces of code specific to each pot (forward/reverse pot and turn pot). Get each working to do its specific function, then work on ways to integrate the two working code sections into single code.
// Display Joystick values using the serial monitor
outputJoystick();
servoVal3 = analogRead(joyV);
servoVal4 = analogRead(joyV);
I still don't understand this code. Write some data to the serial port. Then, get the data to be written. Isn't the horse on the wrong end of the cart?
This is the problem, I cannot find for the life of me a statement or a string of statements that would allow both Val 3 and Val 4 to equal analogRead(joyV) while keeping the map functions different, is there anything that I am missing? Or would it be better to go with tank controls instead of arcade (I only want to do this if it is impossible), again any help would be readily accepted and deeply appreciated.
I showed you how to do it.
int DRV2 = map(z, -15, 0, 255, 0); range -15 to 0 => reverse
int DRV1 = map(z, 0, 15, 0, 255); range 0 to 15 => forward
int STRL = map(y, -10, 0, 255, 0); left
int STRR = map(y, 0, 10, 0, 255); right
Yours should be,
servoDrive = analogRead(joyV);
servoVal3 = map(servoDrive, 0, 1023,1000, 1750); // scale it to use it with the servo (result between 0 and 180) FORWARD
servoVal4 = map(servoDrive, 0, 1023, 1250, 1750); // scale it to use it with the servo (result between 70 and 180) REVERSE
// Read the horizontal joystick value (value between 0 and 1023)
servoTurn = analogRead(joyH);
servoVal1 = map(servoTurn, 0, 1023,1000, 1750); // scale it to use it with the servo (result between 0 and 180) LEFT
servoVal2 = map(servoTurn, 0, 1023, 1250, 1750); // scale it to use it with the servo (result between 70 and 180) RIGHT
Again, your map values I dont think are correct. "(result between 70 and 180), (result between 0 and 180)"
I don't have any servos or pots setup for testing, but the below (modified knob code) might be a way to loop and trap the pot that leaves the centered position and read it in a sub loop until it returns to the centered position again, at which the main loop is reentered. Hopefully only one non centered pot at a time would be in control of the servos to prevent control conflicts. Not that familiar with looping operations.
#include <Servo.h>
Servo myservo1; // create servo object to control a servo
int potpin1 = 0; // analog pin used to connect the potentiometer
int val1; // variable to read the value from the analog pin
Servo myservo2; // create servo object to control a servo
int potpin2 = 0; // analog pin used to connect the potentiometer
int val2; // variable to read the value from the analog pin
void setup()
{
myservo1.attach(8); // attaches the servo on pin 8 to the servo object
myservo2.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
if ((500 < (analogRead(potpin1)) || (analogRead(potpin1)) > 523)){
while ((500 < (analogRead(potpin1)) || (analogRead(potpin1)) > 523)){
val1 = analogRead(potpin1);
val1 = map(val1, 0, 1023, 0, 180);
myservo1.write(val1);
myservo2.write(val1);
delay(15);
}
}
if (500 < (analogRead(potpin2)) || (analogRead(potpin2)) > 523){
while (500 < (analogRead(potpin2)) || (analogRead(potpin2)) > 523){
val2 = analogRead(potpin2);
val2 = map(val2, 0, 1023, 0, 180);
myservo1.write(val2);
myservo2.write(180-val2);
delay(15);
}
}
}
#include <Servo.h>
const int servo1 = 3; // first servo
const int servo2 = 10; // second servo
const int joyH = 3; // L/R Parallax Thumbstick
const int joyV = 4; // U/D Parallax Thumbstick
int servoDrive;
int servoTurn;
int servoVal1; // variable to read the value from the analog pin
int servoVal2;
int servoVal3;
int servoVal4;
Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
void setup() {
// Servo
myservo1.attach(servo1); // attaches the servo
myservo2.attach(servo2); // attaches the servo
// Inizialize Serial
Serial.begin(9600);
}
void loop(){
// Display Joystick values using the serial monitor
outputJoystick();
servoDrive = analogRead(joyV);
servoTurn = analogRead(joyH);
servoVal1 = map(servoTurn, 0, 1023,1000, 1750); // scale it to use it with the servo (result between 0 and 180)
servoVal2 = map(servoTurn, 0, 1023, 1250, 1750); // scale it to use it with the servo (result between 70 and 180)
// scale it to use it with the servo (result between 0 and 180)
servoVal3 = 435 + servoVal4 - servoDrive;
servoVal4 = map(servoDrive, 0, 1023, 1250, 1750);
// scale it to use it with the servo (result between 70 and 180)
myservo2.write(servoVal3); // sets the servo position according to the scaled value
myservo1.write(servoVal4);
// Read the horizontal joystick value (value between 0 and 1023)
//servoTurn = analogRead(joyH);
//servoDrive = analogRead(joyV);
myservo2.write(servoVal1); // sets the servo position according to the scaled value
myservo1.write(servoVal2);
delay(15); // waits for the servo to get there
}
/**
* Display joystick values
*/
void outputJoystick(){
Serial.print(analogRead(joyV));
Serial.print ("----------------");
Serial.print(analogRead(joyH));
Serial.println ("----------------");
}
this is the code i have now, it works when the two parts are separate. forwards and backwards:
#include <Servo.h>
const int servo1 = 3; // first servo
const int servo2 = 10; // second servo
const int joyH = 3; // L/R Parallax Thumbstick
const int joyV = 4; // U/D Parallax Thumbstick
int servoDrive;
int servoTurn;
int servoVal;
int servoVal1; // variable to read the value from the analog pin
int servoVal2;
int servoVal3;
int servoVal4;
Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
void setup() {
// Servo
myservo1.attach(servo1); // attaches the servo
myservo2.attach(servo2); // attaches the servo
// Inizialize Serial
Serial.begin(9600);
}
void loop(){
// Display Joystick values using the serial monitor
outputJoystick();
servoDrive = analogRead(joyV);
// scale it to use it with the servo (result between 0 and 180)
servoVal3 = 435 + servoVal4 - servoDrive;
servoVal4 = map(servoDrive, 0, 1023, 1250, 1750);
// scale it to use it with the servo (result between 70 and 180)
myservo2.write(servoVal3); // sets the servo position according to the scaled value
myservo1.write(servoVal4);
delay(15); // waits for the servo to get there
}
/**
* Display joystick values
*/
void outputJoystick(){
Serial.print(analogRead(joyH));
Serial.print ("---");
Serial.print(analogRead(joyV));
Serial.println ("----------------");
}
and zero point turns:
#include <Servo.h>
const int servo1 = 3; // first servo
const int servo2 = 10; // second servo
const int joyH = 3; // L/R Parallax Thumbstick
const int joyV = 4; // U/D Parallax Thumbstick
int servoDrive;
int servoTurn;
int servoVal1; // variable to read the value from the analog pin
int servoVal2;
int servoVal3;
int servoVal4;
Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
void setup() {
// Servo
myservo1.attach(servo1); // attaches the servo
myservo2.attach(servo2); // attaches the servo
// Inizialize Serial
Serial.begin(9600);
}
void loop(){
// Display Joystick values using the serial monitor
outputJoystick();
servoTurn = analogRead(joyH);
servoVal1 = map(servoTurn, 0, 1023,1000, 1750); // scale it to use it with the servo (result between 0 and 180)
servoVal2 = map(servoTurn, 0, 1023, 1250, 1750); // scale it to use it with the servo (result between 70 and 180)
// Read the horizontal joystick value (value between 0 and 1023)
//servoTurn = analogRead(joyH);
myservo2.write(servoVal1); // sets the servo position according to the scaled value
myservo1.write(servoVal2);
delay(15); // waits for the servo to get there
}
/**
* Display joystick values
*/
void outputJoystick(){
Serial.print(analogRead(joyV));
Serial.print ("----------------");
Serial.print(analogRead(joyH));
Serial.println ("----------------");
}
for some reason (its one i cant find) whenever the 2 parts of the code are together like then one at the top, all of the code for the Forwards and backwards portion are ignored and the robot cannot move forward. is there anything in the code that makes this Occur? if not does anyone have any advice? as always any help would be appreciated and anyone that has posted so far has been of extreme help.
OK it took me a while to figure out how to do it for my own project and this is one of the top posts that come up when googling it, so here is how it is done.
First there is two aspects, motor distribution and speed. Normally distribution and speed is determined by x and y respectfully. However this doesn't work for us.
Instead speed is equal to the distance from the center. (which we will refer to as 0,0).
Next to get the distribution we need the angle of the point with respect to the X-axis.
Specifically: 0 degrees being X=MaxRight, Y=0; 90 degrees X=0, Y=MaxUp; 180 degrees y=0, X= maxleft; 270 degrees x=0, y=maxdown
Let +1=100% of power and -1=-100% power (ie reverse)
Then (* == degrees)
90* => +1, +1
180* => -1,+1
270* => -1,-1
360*/0* => +1,-1
Now as the points move around the circle there needs to be a transition between +1 and -1. Specifically in the first quadrant:
Quadrant one (angle <90*)
at 0* it is +1, -1
at 22.5* it is +1, -.5
at 45* it is +1,0
at 67.5 it is +1, +.5
at 90* it is +1, +1.
Now programmablly the code is
double right_dist;
double left_dist;
if(angle <= 90){
left_dist= +1;
if(angle<45.0){
right_dist= -1*((45.0-angle)/45.0);
}
else{
right_dist= 1*( (angle-45.0)/45.0);
}
}
else if(angle <= 180){
angle = angle - 90;
right_dist= +1;
if(angle<45.0){
left_dist= 1*((45.0-angle)/45.0);
}
else{
left_dist= -1*( (angle-45.0)/45.0);
}
}
else if(angle <= 270){
angle = angle - 180;
left_dist= -1;
if(angle<45.0){
right_dist= 1*((45.0-angle)/45.0);
}
else{
right_dist= -1*( (angle-45.0)/45.0);
}
}
else{ // if(angle <= 360){
angle = angle - 270;
right_dist= -1;
if(angle<45.0){
left_dist= -1 *((45.0-angle)/45.0);
}
else{
left_dist= 1*( (angle-45.0)/45.0);
}
}
motor_left_speed=left_dist*distance
motor_right_speed=right_dist*distance
I'm new to this but I want to achieve something similar but a little less complicated, well in my mind it is . I'm usig a single joystick and 2 motors for an underwater ROV project. Along the y axis its pretty straight foward. stick up both motors full speed fwd, stick down both motors full speed rev and everything in between. Lets call the motors M1 and M2. when i'm refering to the speed of the motors I will just say that full fwd speed is +127, 0 is off and full rev is -127. If that should be different let me know, but for now thats burned into my mind. Ok fwd and rev are cake, turning isn't that hard either. if the joystick is all the way left then m1 is -127 and m2 is +127. if the joystick is half left then m1 is -63 and m2 is +63.
Programming this alone doesn't seem that difficult but when i want to turn while moving forward... joystick all the way to the left and up M1 is 0 and m2 is +127
I have no Idea where to start with this but If it cant happen i can just use two sticks.
I'm new to this but I want to achieve something similar but a little less complicated, well in my mind it is . I'm usig a single joystick and 2 motors for an underwater ROV project. Along the y axis its pretty straight foward. stick up both motors full speed fwd, stick down both motors full speed rev and everything in between. Lets call the motors M1 and M2. when i'm refering to the speed of the motors I will just say that full fwd speed is +127, 0 is off and full rev is -127. If that should be different let me know, but for now thats burned into my mind. Ok fwd and rev are cake, turning isn't that hard either. if the joystick is all the way left then m1 is -127 and m2 is +127. if the joystick is half left then m1 is -63 and m2 is +63.
Programming this alone doesn't seem that difficult but when i want to turn while moving forward... joystick all the way to the left and up M1 is 0 and m2 is +127
I have no Idea where to start with this but If it cant happen i can just use two sticks.
It is possible to do with one stick and this post has a few solutions on it already. But if your still having trouble, post what you have already and we will see what we can do.
Hi,
I solved this problem a few months ago with the help of a very clever member of the Arduino community...
Search for this thread:
Differential Steering using a joystick / programming question....
I hope that helps.
Tim
Hi,
I solved this problem a few months ago with the help of a very clever member of the Arduino community...
Search for this thread:
Differential Steering using a joystick / programming question....
I hope that helps.
Tim
Here it is:
http://forum.arduino.cc/index.php?topic=172581.0
I solved this problem a few months ago with the help of a very clever member of the Arduino community...
One potentiometer controls the left wheel, and the other controls the right. There is currently no reverse, although by remapping the pots to say, -10 to 245 and using an if statement that could easily be handled.
Did you actually provide for reverse?
One potentiometer controls the left wheel, and the other controls the right. There is currently no reverse, although by remapping the pots to say, -10 to 245 and using an if statement that could easily be handled.
Did you actually provide for reverse?
I actually never understood what he was saying in that statement, either - my algorithm, IIRC, as written will automagically handle reverse (I think).
Its actually very simple. A pot has a range from 0 - 1023, and being that you want to control a motor or servo, you will most likely want full forward, full reverse and stop.
Well forward is equal to anything above 512(mid point of pot), and reverse is anything below 512. Now you also want to stop the motor, and that done when your at 512.
So you will need an IF statement.
psudo:
int XSpeed = analogRead( XpotPin );
if ( XSpeed > (512 + deadzone) ) //go forward
...
else if ( XSpeed < (512 - deadzone) ) //go reverse
...
else //XSpeed = 512 +- deadzone, stop
deadzone is needed because your pot will almost never perfectly be centered at 512, so some slack is needed
Its actually very simple.
Can you post the combined working code? The problem is solvable to a certain extent, but I haven't seen single code (that I understand) that provides forward/reverse and left/right from a joystick, especially if the stick is pushed in a diagonal direction. I currently do any actual testing with pots and servos as my hardware is currently disconnected and in various places.
I'll post the full working code later when I get home.
Ok, I have here the simplest form of single joystick control. It by default assumes that the motor pins have pull down resistors, but if you don't have pull down resistors, then just uncomment the other lines.
/*
Simple motor control with square limit joystick
Y(1023)
|---------|
|---------|
X(0)|---512---|X(1023)
|---------|
|---------|
Y(0)
Left Motor Forward/Reverse = LMF/LMR
Right Motor Forward/Reverse = RMF/RMR
*/
const byte LMF = 3;
const byte LMR = 5;
const byte RMF = 6;
const byte RMR = 9;
const byte Xpot = A0;
const byte Ypot = A1;
volatile unsigned int X = 512, Y = 512; // pot values default 512 for center
unsigned int Xspeed = 0, Yspeed = 0; //motor speed default 0 for full stop
const int Xdeadzone = 5, Ydeadzone = 5; //amount of slack needed for pot values
void setup(){
pinMode(LMF, OUTPUT);
pinMode(LMR, OUTPUT);
pinMode(RMF, OUTPUT);
pinMode(RMR, OUTPUT);
Serial.begin(9600);
}
void loop()
{
X = analogRead(Xpot);
Y = analogRead(Ypot);
if (X >= (512 + Xdeadzone))//Forward
{
Xspeed = (X - 512) / 2; // 0 - 255
if(Y > (512 + Ydeadzone)) //Left
{
Yspeed = (Y - 512) / 2;
analogWrite(LMF, Xspeed - Yspeed); analogWrite(RMF, Xspeed);
//digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
}
else if (Y < (512 - Ydeadzone)) //Right
{
Yspeed = (512 - Y) / 2;
analogWrite(LMF, Xspeed); analogWrite(RMF, Xspeed - Yspeed);
//digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
}
else
{
analogWrite(LMF, Xspeed); analogWrite(RMF, Xspeed);
//digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
}
}
else if (X <= (512 - Xdeadzone))//Reverse
{
Xspeed = (512 - X) / 2;
if(Y > (512 + Ydeadzone)) //Left
{
Yspeed = (Y - 512) / 2;
//digitalWrite(LMF, LOW); digitalWrite(RMF, LOW);
analogWrite(LMR, Xspeed - Yspeed); analogWrite(RMR, Xspeed);
}
else if (Y < (512 - Ydeadzone)) //Right
{
Yspeed = (512 - Y) / 2;
//digitalWrite(LMF, LOW); digitalWrite(RMF, LOW);
analogWrite(LMR, Xspeed); analogWrite(RMR, Xspeed - Yspeed);
}
else
{
//digitalWrite(LMF, LOW); digitalWrite(RMF, LOW);
analogWrite(LMR, Xspeed); analogWrite(RMR, Xspeed);
}
}
else // X is between 512 +- deadzone
{
if(Y > (512 + Ydeadzone)) // zero point turn Left
{
digitalWrite(LMF, LOW); analogWrite(RMF, Yspeed);
//digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
}
else if(Y < (512 - Ydeadzone))// zero point turn Right
{
analogWrite(LMF, Yspeed); digitalWrite(RMF, LOW);
//digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
}
else
{ // Full stop
digitalWrite(LMF,LOW);
digitalWrite(RMF,LOW);
digitalWrite(LMR,LOW);
digitalWrite(RMR,LOW);
}
}
}
Simple motor control with square limit joystick
I assume this means no diagonal stick movement. Correct?
This use every position, which includes diagonal.
I called it square limit because the older joysticks like the Atari joystick have a square range. But the current ones like those used on ps2 controller, xbox, or anything else that uses a joystick, has a round limit. These I think need to use sine and cosine to get there full range of movement. I'll do some tests to confirm if that's true or not, later.
Atari joystick:
http://en.wikipedia.org/wiki/File:Joyopis.svg
The one I posted earlier is the one I am currently using, and it works for diagonal positions too. The only difference is that the first one uses the map function, whereas this one is straight forward using IF ELSE to take the difference of X and Y positions to go a certain direction.
The first one I posted is neater but a little more confusing, compared to this "simpler" code that breaks it all down. Not as nice but not as confusing.
so this is the working combined code?
/*
Simple motor control with square limit joystick
Y(1023)
|---------|
|---------|
X(0)|---512---|X(1023)
|---------|
|---------|
Y(0)
Left Motor Forward/Reverse = LMF/LMR
Right Motor Forward/Reverse = RMF/RMR
*/
const byte LMF = 3;
const byte LMR = 5;
const byte RMF = 6;
const byte RMR = 9;
const byte Xpot = A0;
const byte Ypot = A1;
volatile unsigned int X = 512, Y = 512; // pot values default 512 for center
unsigned int Xspeed = 0, Yspeed = 0; //motor speed default 0 for full stop
const int Xdeadzone = 5, Ydeadzone = 5; //amount of slack needed for pot values
void setup(){
pinMode(LMF, OUTPUT);
pinMode(LMR, OUTPUT);
pinMode(RMF, OUTPUT);
pinMode(RMR, OUTPUT);
Serial.begin(9600);
}
void loop()
{
X = analogRead(Xpot);
Y = analogRead(Ypot);
if (X >= (512 + Xdeadzone))//Forward
{
Xspeed = (X - 512) / 2; // 0 - 255
if(Y > (512 + Ydeadzone)) //Left
{
Yspeed = (Y - 512) / 2;
analogWrite(LMF, Xspeed - Yspeed); analogWrite(RMF, Xspeed);
//digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
}
else if (Y < (512 - Ydeadzone)) //Right
{
Yspeed = (512 - Y) / 2;
analogWrite(LMF, Xspeed); analogWrite(RMF, Xspeed - Yspeed);
//digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
}
else
{
analogWrite(LMF, Xspeed); analogWrite(RMF, Xspeed);
//digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
}
}
else if (X <= (512 - Xdeadzone))//Reverse
{
Xspeed = (512 - X) / 2;
if(Y > (512 + Ydeadzone)) //Left
{
Yspeed = (Y - 512) / 2;
//digitalWrite(LMF, LOW); digitalWrite(RMF, LOW);
analogWrite(LMR, Xspeed - Yspeed); analogWrite(RMR, Xspeed);
}
else if (Y < (512 - Ydeadzone)) //Right
{
Yspeed = (512 - Y) / 2;
//digitalWrite(LMF, LOW); digitalWrite(RMF, LOW);
analogWrite(LMR, Xspeed); analogWrite(RMR, Xspeed - Yspeed);
}
else
{
//digitalWrite(LMF, LOW); digitalWrite(RMF, LOW);
analogWrite(LMR, Xspeed); analogWrite(RMR, Xspeed);
}
}
else // X is between 512 +- deadzone
{
if(Y > (512 + Ydeadzone)) // zero point turn Left
{
digitalWrite(LMF, LOW); analogWrite(RMF, Yspeed);
//digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
}
else if(Y < (512 - Ydeadzone))// zero point turn Right
{
analogWrite(LMF, Yspeed); digitalWrite(RMF, LOW);
//digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
}
else
{ // Full stop
digitalWrite(LMF,LOW);
digitalWrite(RMF,LOW);
digitalWrite(LMR,LOW);
digitalWrite(RMR,LOW);
}
}
}
Hi all,
I have run the code that HazardsMind has posted manually, and as far as I can see it is not doing what it is supposed to do?!
Did anyone actually load this into the Arduino, with a Left DC Motor and Right DC motor, and got it to move forward when you push Y up, backwards when you push Y down, and turn left-right when X is pushed left-right?
I want to use servo and radio functions but the includs are incompatible.
Has some one resolve the problem.
Thanks
I
I am french and I have a same problem with arduino and motorshield adafruit.
I try to control a robot with a joystick.
I can control 2 motors directly and I can get the location of the joystick with a switch cas. But I can't connect both to conduct my robot.
Please help me if you can.
thank a lot,
jp
My code for my robot projet is:
// Librairie Adafruit
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_MotorShield monShield = Adafruit_MotorShield(); //création de l'objet shield
Adafruit_DCMotor *moteurGauche = monShield.getMotor(1); //création de l'objet moteurGauche par pointeur et repérage du numéro
Adafruit_DCMotor *moteurDroite = monShield.getMotor(2); //création de l'objet moteurDroite par pointeur et repérage du numéro
// Branchement joystick et moteur
const int x = A0;
const int xMin = 0;
const int xMax = 1023;
const int y = A4;
const int yMin = 0;
const int yMax = 1023;
int neut = 20; // zone neutre
int neutn = -20; // zone neutre négative
int zone;
int zone_val;
// Définition des variables
int lecX, lecY, calX, calY, retX, retY, vit;
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void setup () {
// Initialisation port serie
Serial.begin(9600);
AFMS.begin();
zone_val = 10;
zone = 10;
// Lecture des valeurs en x et y
lecX = analogRead(x);
lecY = analogRead(y);
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void loop() {
// Calibration du joystick en 0,0
calX = analogRead(x)-lecX;
calY = analogRead(y)-lecY;
Serial.print("+++++++> valeur_calX :") && Serial.println (calX);
Serial.print("+++++++> valeur_calY :") && Serial.println (calY);
delay (100);
/*
Serial.print("SENS INITIAL MOTEUR 1 RELEASE++++++> :")&&Serial.println (sensM1);
Serial.print("SENS INITIAL MOTEUR 2 RELEASE++++++> :")&&Serial.println (sensM2);
Serial.print("SENS TEST MOTEUR 3 FORWARD++++++> :")&&Serial.println (sensM3);
delay (1000);
//sensM1 = "FORWARD";
//sensM2 = "FORWARD";
//Serial.print("SENS MOTEUR 1 FORWARD++++++> :") && Serial.println ('sensM1');
//Serial.print("SENS MOTEUR 2 FORWARD++++++> :") && Serial.println ('sensM2');
delay(1000);
*/
// Réatalonnage x et y de 0,1023 à -255,255
//Ré-étalonne la valeur entre 0 et 1023 sur une fourchette entre 0 et 255
retX = map(calX, xMin, xMax, -255 , 255);
retX= constrain(calX, -255, 255);
retY = map(calY, yMin, yMax, -255, 255);
retY = constrain(calY, -255, 255);
Serial.print("=======> valeur_X :") && Serial.println (retX);
Serial.print("=======> valeur_Y :") && Serial.println (retY);
delay (100);
if ((abs(retX)) > (abs(retY)))
{
vit = (abs(retX));
}
else
{
vit = (abs(retY));
}
//++++++++++++++++++
calcul_zone(retX, retY);
Serial.println("POSITION JOYSTICK case valeur :");
Serial.print("======> valeur_X :") && Serial.println (retX);
Serial.print("=======> valeur_Y :") && Serial.println (retY);
delay(100);
Serial.print("ZONE VAL ======> :") && Serial.println(zone);
delay(100);
Serial.println("BONJOUR");
impression(zone);
delay(2000);
ACTION_MOTOR(zone);
//delay(500);
//arret();
//delay(2000);
//Serial.print("ZONE VAL ======> :") && Serial.println(zone_val);
Serial.println("AU REVOIR");
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~é
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
// CALCUL DE ZONE PAR RAPPORT A LA POSITION JOYSTICK (RetX, RetY);
void calcul_zone(int XX, int YY)
{
if (YY <= neutn)
{
if (XX >= neut){zone = 4;}
if (XX <= neutn){zone = 6;}
if ((XX < neut) && (XX > neutn)) {zone = 5;}
}
if (YY >= neut)
{
if (XX >= neut){zone = 2;}
if (XX <= neutn){zone = 8;}
if ((XX < neut) && (XX > neutn)) {zone = 1;}
}
if ((YY > neutn) && (YY < neut))
{
if (XX >= neut){zone = 3;}
if (XX <= neutn){zone = 7;}
if ((XX < neut) && (XX > neutn)) {zone = 0;}
}
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@à
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&++++++++++++++++++++++++++++++++++++++++++++++++++
void arret(){
//fonction d'arrêt des deux moteurs
moteurGauche->run(RELEASE);
moteurDroite->run(RELEASE);
}
void defVitesse(int v){
moteurGauche->setSpeed(v); //on redéfinit la vitesse
moteurDroite->setSpeed(v); //des deux moteurs
}
void avance(int v){
//fonction de marche avant
//defVitesse(v);
moteurGauche->setSpeed(v);
moteurDroite->setSpeed(v);
//appel de la fonction pour définir la vitesse
moteurGauche->run(FORWARD);
moteurDroite->run(FORWARD);
}
void recule(int v){
//fonction de marche arrière
defVitesse(v);
moteurGauche->run(BACKWARD);
moteurDroite->run(BACKWARD);
}
void tourneDroite(int v){
//fonction pour tourner à droite sur place
defVitesse(v);
moteurGauche->run(FORWARD);
moteurDroite->run(BACKWARD);
}
void tourneGauche(int v){
//fonction pour tourner à gauche sur place
defVitesse(v);
moteurGauche->run(BACKWARD);
moteurDroite->run(FORWARD);
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&++++++++++++++++++++++++++++++++++++++++++++++++
//§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
void impression(int zone)
{
switch (zone)
{
case 0:
Serial.println("Robot STOP");
break;
case 1:
Serial.println("Robot AVANCE");
delay(10);
break;
case 2:
Serial.println("Robot AVANCE et TOURNE à GAUCHE");
break;
case 3:
Serial.println("Robot TOURNE à GAUCHE toute");
break;
case 4:
Serial.println("Robot RECULE et TOURNE à GAUCHE");
break;
case 5:
Serial.println("Robot RECULE");
break;
case 6:
Serial.println("Robot RECULE et TOURNE à DROITE");
break;
case 7:
Serial.println("Robot TOURNE à DROITE toute");
break;
case 8:
Serial.println("Robot AVANCE et TOURNE à DROITE");
break;
default:
break;
}
}
//§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
//:::::::::::::::::::::::::::::::::::::::::::::::::
void ACTION_MOTOR(int zone)
{
switch (zone)
{
case 0:
Serial.println("ACTION MOTOR Robot STOP");
arret();
break;
case 1:
Serial.println("ACTION MOTOR Robot AVANCE");
avance(100);
delay(500);
arret();
break;
case 2:
Serial.println("ACTION MOTOR Robot AVANCE et TOURNE à GAUCHE");
break;
case 3:
Serial.println("ACTION MOTOR Robot TOURNE à GAUCHE toute");
break;
case 4:
Serial.println("ACTION MOTOR Robot RECULE et TOURNE à GAUCHE");
break;
case 5:
Serial.println("ACTION MOTOR Robot RECULE");
break;
case 6:
Serial.println("ACTION MOTOR Robot RECULE et TOURNE à DROITE");
break;
case 7:
Serial.println("ACTION MOTOR Robot TOURNE à DROITE toute");
break;
case 8:
Serial.println("ACTION MOTOR Robot AVANCE et TOURNE à DROITE");
break;
default:
break;
}
}
//:::::::::::::::::::::::::::::::::::::::::::::::::::
My code for my robot projet is:
Please read #7 below and put your code in code tags:
http://forum.arduino.cc/index.php/topic,148850.0.html (http://forum.arduino.cc/index.php/topic,148850.0.html)
@jp31310: I did not go into detail with your code, but I think we are in the same line of thought: I have defined "quadrants" and based on the "quadrant" different logic takes place.
You can find my code here: https://github.com/drazha/Son-Dad-Rover-Mk-I (https://github.com/drazha/Son-Dad-Rover-Mk-I)
It is a bit messy and requires refactoring, but the general idea is there...
Can you post the combined working code? The problem is solvable to a certain extent, but I haven't seen single code (that I understand) that provides forward/reverse and left/right from a joystick, especially if the stick is pushed in a diagonal direction. I currently do any actual testing with pots and servos as my hardware is currently disconnected and in various places.
Does my logic - see posts #4 and #5 at:
http://forum.arduino.cc/index.php?topic=172581.0 (http://forum.arduino.cc/index.php?topic=172581.0)
...not work? The OP seemed to think it worked.
I mean - if it doesn't work, I'd certainly like to know - but the logic seems sound.
deadzone is needed because your pot will almost never perfectly be centered at 512, so some slack is needed
For many (most? all?) of the PS2 style thumbsticks - they have a "built-in" deadzone around the center of the stick (in some cases I have heard it is quite large - almost to the point of making the stick unusable). Just something to keep in mind; it should be possible to figure out what the size of the zone is with some test code.
Alternatively, if you have a display available (serial or maybe an LCD) - you could create a "calibration code" section for the thumbstick like the old PC games used that would instruct you to push the stick up to one corner, push it down to the opposite corner, perhaps move it around in a circle, then center it - at each step instructing you to "press a button" (and don't make this the "stick button" that many of these units have - as that could throw off the reading - though with the deadzone it may be ok - eh, play with it). It might even be possible to do the calibration with an LED and some "intelligent" code (and/or a buzzer or something?).
I know that this is an old forum, but for future reference,
#include <Servo.h>
Servo myServo1;
Servo myServo2;
int lXPin = A0;
int lYPin = A1;
int lX;
int lY;
int l;
int r;
void setup() {
myServo1.attach(3);
myServo2.attach(4);
}
void loop() {
lX = analogRead(lXPin);
lY = analogRead(lYPin);
l = lX + lY - 90;
r = lX - lY + 90;
myServo1.write(r);
myServo2.write(l);
}