PS2 Control of Brushless Motor and ESC via Arduino
Re: PS2 Control of Brushless Motor and ESC via Arduino
For mine I simply used "x" for forward and "o" for reverse. Without using the stick both right and left thrusters move forward (RTF / LTF) or reverse (RTR / LTR). Throw in the analog stick for steering and you'll see the math function below for how this works. On the extremes, for a full throttle (forward or reverse) with hard rudder in one direction; Use get one thruster to move at full speed and the other not moving at all. With no throttle and hard rudder to one direction you get one thruster to move forward at full speed and the other moving in reverse at full speed.
I hope this helps
if(ps2x.Button(PSB_BLUE)){
TF = ps2x.Analog(PSAB_BLUE);
RTF = TF;
LTF = TF;
RTR = 0;
LTR = 0;
}
else{
RTF = 0;
LTF = 0;
TF = 0;
}
if(ps2x.Button(PSB_CIRCLE)){
TR = ps2x.Analog(PSAB_CIRCLE);
RTF = 0;
LTF = 0;
RTR = TR;
LTR = TR;
}
else{
RTR = 0;
LTR = 0;
TR = 0;
}
if(ps2x.Analog(PSS_LX) <= 100){
LT = map(ps2x.Analog(PSS_LX), 100, 0, 0, 255);
if(TF == 0 && TR == 0){
LTR = LT;
RTF = LT;
}
else if(TF >= 1){
LTF = TF - LT;
if (LTF < 0){
LTR = map(LTF, -1, -255, 1, 255);
LTF = 0;
}
RTF = TF + LT;
if (RTF > 255){
RTF = 255;
}
}
else if (TR >= 1){
LTR = TR - LT;
if (LTR < 0){
LTF = map(LTR, -1, -255, 1, 255);
LTR = 0;
}
RTR = TR + LT;
if (RTR > 255){
RTR = 255;
}
}
}
if(ps2x.Analog(PSS_LX) >= 155){
RT = map(ps2x.Analog(PSS_LX), 155, 255, 0, 255);
if(TF == 0 && TR == 0){
LTF = RT;
RTR = RT;
}
else if(TF >= 1){
LTF = TF + RT;
if (LTF > 255){
LTF = 255;
}
RTF = TF - RT;
if (RTF < 0){
RTR = map(RTF, -1, -255, 1, 255);
RTF = 0;
}
}
else if (TR >= 1){
LTR = TR + RT;
if (LTR > 255){
LTR = 255;
}
RTR = TR - RT;
if (RTR < 0){
RTF = map(RTR, -1, -255, 1, 255);
RTR = 0;
}
}
I hope this helps
if(ps2x.Button(PSB_BLUE)){
TF = ps2x.Analog(PSAB_BLUE);
RTF = TF;
LTF = TF;
RTR = 0;
LTR = 0;
}
else{
RTF = 0;
LTF = 0;
TF = 0;
}
if(ps2x.Button(PSB_CIRCLE)){
TR = ps2x.Analog(PSAB_CIRCLE);
RTF = 0;
LTF = 0;
RTR = TR;
LTR = TR;
}
else{
RTR = 0;
LTR = 0;
TR = 0;
}
if(ps2x.Analog(PSS_LX) <= 100){
LT = map(ps2x.Analog(PSS_LX), 100, 0, 0, 255);
if(TF == 0 && TR == 0){
LTR = LT;
RTF = LT;
}
else if(TF >= 1){
LTF = TF - LT;
if (LTF < 0){
LTR = map(LTF, -1, -255, 1, 255);
LTF = 0;
}
RTF = TF + LT;
if (RTF > 255){
RTF = 255;
}
}
else if (TR >= 1){
LTR = TR - LT;
if (LTR < 0){
LTF = map(LTR, -1, -255, 1, 255);
LTR = 0;
}
RTR = TR + LT;
if (RTR > 255){
RTR = 255;
}
}
}
if(ps2x.Analog(PSS_LX) >= 155){
RT = map(ps2x.Analog(PSS_LX), 155, 255, 0, 255);
if(TF == 0 && TR == 0){
LTF = RT;
RTR = RT;
}
else if(TF >= 1){
LTF = TF + RT;
if (LTF > 255){
LTF = 255;
}
RTF = TF - RT;
if (RTF < 0){
RTR = map(RTF, -1, -255, 1, 255);
RTF = 0;
}
}
else if (TR >= 1){
LTR = TR + RT;
if (LTR > 255){
LTR = 255;
}
RTR = TR - RT;
if (RTR < 0){
RTF = map(RTR, -1, -255, 1, 255);
RTR = 0;
}
}
Re: PS2 Control of Brushless Motor and ESC via Arduino
Found this link on the web, great explanation on tank steering controll with one stick....
http://home.kendra.com/mauser/Joystick.html
Like I said im not a computer person but I think this could be made to work.
Im coping and pasting some code from the arduino forum.....
int joyPin1 = 4; // slider variable connecetd to analog pin 0
int joyPin2 = 5; // slider variable connecetd to analog pin 1
int coordX = 0; // variable to read the value from the analog pin 0
int coordY = 0; // variable to read the value from the analog pin 1
int centerX = 527; // we measured the value for the center of the joystick
int centerY = 512;
int actualZone = 0;
int previousZone = 0;
void setup()
{
Serial.begin(9600);
}
void loop() {
// reads the value of the variable joystick
coordX = analogRead(joyPin1);
coordY = analogRead(joyPin2);
//maps from 0-1023 POT range to -100 - 100 range of formula
int coordXmapped = map(coordX,0,1023,-100,100);
int coordYmapped = map(coordY,0,1023,-100,100);
// we print int the terminal
Serial.print("X Axis: ");
Serial.print(coordX); //joystick X
Serial.print("\n");
Serial.print("Y axis: ");
Serial.print(coordY); //joystick Y
Serial.print("\n");
Serial.print("X Axis Mapped: ");
Serial.print(coordXmapped); //joystick X mapped value
Serial.print("\n");
Serial.print("Y Axis Mapped: ");
Serial.print(coordYmapped); //joystick Y mapped value
Serial.print("\n");
// invert the X coordinate to be used in the formula
int coordXinvert = (coordXmapped*-1);
// the formulas
int v = ((100-abs(coordXinvert)) * (coordYmapped/100)+coordYmapped);
int w = ((100-abs(coordYmapped)) * (coordXinvert/100) + coordXinvert);
int rightwheel = ((v+w)/2);
int leftwheel = ((v-w)/2);
Serial.print("rightwheel: ");
Serial.print(rightwheel);
Serial.print("\n");
Serial.print("leftwheel: ");
Serial.print(leftwheel);
Serial.print("\n");
//Now we have to use some trickery to setup the motor controller outputs
int rightfrontback = 1;
if(rightwheel<0)
{
rightfrontback = 0;
}
int leftfrontback = 1;
if(leftwheel<0)
{
leftfrontback = 0;
}
int rightmotor = map(abs(rightwheel),0,100,0,255);
int leftmotor = map(abs(leftwheel),0,100,0,255);
Serial.print("rightwheel motor: ");
Serial.print(rightmotor);
Serial.print("\n");
Serial.print("rightwheel motor direction: ");
Serial.print(rightfrontback);
Serial.print("\n");
Serial.print("leftwheel motor: ");
Serial.print(leftmotor);
Serial.print("\n");
Serial.print("leftwheel motor direction: ");
Serial.print(leftfrontback);
Serial.print("\n") ;
Serial.print("------------------------------------------") ;
Serial.print("\n") ;
delay (5000);
}
There may be an error in the code, but I cant see where it is. Here is the link to where it is on the arduino forum, If someone can see the error let me know where it is please!
http://arduino.cc/forum/index.php/topic ... #msg858136
http://home.kendra.com/mauser/Joystick.html
Like I said im not a computer person but I think this could be made to work.
Im coping and pasting some code from the arduino forum.....
int joyPin1 = 4; // slider variable connecetd to analog pin 0
int joyPin2 = 5; // slider variable connecetd to analog pin 1
int coordX = 0; // variable to read the value from the analog pin 0
int coordY = 0; // variable to read the value from the analog pin 1
int centerX = 527; // we measured the value for the center of the joystick
int centerY = 512;
int actualZone = 0;
int previousZone = 0;
void setup()
{
Serial.begin(9600);
}
void loop() {
// reads the value of the variable joystick
coordX = analogRead(joyPin1);
coordY = analogRead(joyPin2);
//maps from 0-1023 POT range to -100 - 100 range of formula
int coordXmapped = map(coordX,0,1023,-100,100);
int coordYmapped = map(coordY,0,1023,-100,100);
// we print int the terminal
Serial.print("X Axis: ");
Serial.print(coordX); //joystick X
Serial.print("\n");
Serial.print("Y axis: ");
Serial.print(coordY); //joystick Y
Serial.print("\n");
Serial.print("X Axis Mapped: ");
Serial.print(coordXmapped); //joystick X mapped value
Serial.print("\n");
Serial.print("Y Axis Mapped: ");
Serial.print(coordYmapped); //joystick Y mapped value
Serial.print("\n");
// invert the X coordinate to be used in the formula
int coordXinvert = (coordXmapped*-1);
// the formulas
int v = ((100-abs(coordXinvert)) * (coordYmapped/100)+coordYmapped);
int w = ((100-abs(coordYmapped)) * (coordXinvert/100) + coordXinvert);
int rightwheel = ((v+w)/2);
int leftwheel = ((v-w)/2);
Serial.print("rightwheel: ");
Serial.print(rightwheel);
Serial.print("\n");
Serial.print("leftwheel: ");
Serial.print(leftwheel);
Serial.print("\n");
//Now we have to use some trickery to setup the motor controller outputs
int rightfrontback = 1;
if(rightwheel<0)
{
rightfrontback = 0;
}
int leftfrontback = 1;
if(leftwheel<0)
{
leftfrontback = 0;
}
int rightmotor = map(abs(rightwheel),0,100,0,255);
int leftmotor = map(abs(leftwheel),0,100,0,255);
Serial.print("rightwheel motor: ");
Serial.print(rightmotor);
Serial.print("\n");
Serial.print("rightwheel motor direction: ");
Serial.print(rightfrontback);
Serial.print("\n");
Serial.print("leftwheel motor: ");
Serial.print(leftmotor);
Serial.print("\n");
Serial.print("leftwheel motor direction: ");
Serial.print(leftfrontback);
Serial.print("\n") ;
Serial.print("------------------------------------------") ;
Serial.print("\n") ;
delay (5000);
}
There may be an error in the code, but I cant see where it is. Here is the link to where it is on the arduino forum, If someone can see the error let me know where it is please!
http://arduino.cc/forum/index.php/topic ... #msg858136
Re: PS2 Control of Brushless Motor and ESC via Arduino
Is there a way to open the library so I can see what the buttons and such are named in the ps2 library? Like I said I'm new to programming and have no clue...
Edit, midnight 10-9-12
Ok I think I found it in the keywords file folder, Man this sucks!! I should spend more time learning how to programm instead of just jumping in and going for the gusto. Honestly I have no Idea when it comes to this stuff, but I think I can make it work. Nothing like jumping to the end of the book first huh LOL
Ugg ok Im going to bed, Ill start trying this out more in the next couple of days. My hat is off to those who understand this stuff.
Edit, midnight 10-9-12
Ok I think I found it in the keywords file folder, Man this sucks!! I should spend more time learning how to programm instead of just jumping in and going for the gusto. Honestly I have no Idea when it comes to this stuff, but I think I can make it work. Nothing like jumping to the end of the book first huh LOL
Ugg ok Im going to bed, Ill start trying this out more in the next couple of days. My hat is off to those who understand this stuff.
Re: PS2 Control of Brushless Motor and ESC via Arduino
the libraries live in arduino/libraries/ps2 library name. the actual code is probably in a .cpp file. There is also a header file (.h) that contains the function prototypes. the keywords file just tells the arduino ide which words to change the color of to make it easier to see.
Re: PS2 Control of Brushless Motor and ESC via Arduino
Thanks derelicte, Ill keep poking around with it until I figure it out. And if that doesnt work Ill beat it out of it with a big hammer LOL.
Re: PS2 Control of Brushless Motor and ESC via Arduino
OK I get it now, just run the example programm and it prints out the values on which input... Didnt need the big hammer after all.
Re: PS2 Control of Brushless Motor and ESC via Arduino
Just started working on a similar project. Xbox controller >>>>arduino >>>tether>>>> arduino on rov. I have the controller working fine with the servo library for the control of the ESC's. Just starting with the whole arduino programming. The questions I have are I need bi-directional communications between the arduinos for control of ESC, lights and also sending data back to the surface. What comm. protocol, serial rs485? Lost at this point.
Re: PS2 Control of Brushless Motor and ESC via Arduino
Two wire (or whatever its called) worked for me. Just connect the grounds between the two arduinos, then connect rx of arduino 1 to tx of arduino 2....then tx of arduino 1 to rx of arduino 2. Make sure to disconnect this circuit when uploading new code to the arduino.
Then you just need to code in your com protocol in each of arduinos.
An example of what the code looks like for arduino is here: viewtopic.php?f=15&t=348
Then you just need to code in your com protocol in each of arduinos.
An example of what the code looks like for arduino is here: viewtopic.php?f=15&t=348
Re: PS2 Control of Brushless Motor and ESC via Arduino
over what distance have you had it connected like that?eska-dh wrote:Two wire (or whatever its called) worked for me. Just connect the grounds between the two arduinos, then connect rx of arduino 1 to tx of arduino 2....then tx of arduino 1 to rx of arduino 2. Make sure to disconnect this circuit when uploading new code to the arduino.
Then you just need to code in your com protocol in each of arduinos.
An example of what the code looks like for arduino is here: viewtopic.php?f=15&t=348
Re: PS2 Control of Brushless Motor and ESC via Arduino
100' of cat5 cable.