MecanumRobotPS2Control Ino
MecanumRobotPS2Control Ino
//PS2
#define PS2_DAT 52 //14
#define PS2_CMD 51 //15
#define PS2_SEL 53 //16
#define PS2_CLK 50 //17
int error = 0;
byte type = 0;
byte vibrate = 0;
class SPDMotor {
public:
SPDMotor( int encoderA, int encoderB, bool encoderReversed, int motorPWM, int
motorDir1, int motorDir2 );
/// Activate a SHORT BRAKE mode, which shorts the motor drive EM, clamping
motion.
void hardStop();
private:
Encoder *_encoder;
bool _encoderReversed;
int _motorPWM, _motorDir1, _motorDir2;
_motorPWM = motorPWM;
pinMode( _motorPWM, OUTPUT );
_motorDir1 = motorDir1;
pinMode( _motorDir1, OUTPUT );
_motorDir2 = motorDir2;
pinMode( _motorDir2, OUTPUT );
}
/// Activate a SHORT BRAKE mode, which shorts the motor drive EM, clamping motion.
void SPDMotor::hardStop() {
_speed = 0;
digitalWrite(_motorDir1,HIGH);
digitalWrite(_motorDir2,HIGH);
analogWrite( _motorPWM, 0);
}
SPDMotor *motorLF = new SPDMotor(18, 31, true, 12, 34, 35); // <- Encoder reversed
to make +position measurement be forward.
SPDMotor *motorRF = new SPDMotor(19, 38, false, 8, 36, 37); // <- NOTE: Motor Dir
pins reversed for opposite operation
SPDMotor *motorLR = new SPDMotor( 3, 49, true, 9, 43, 42); // <- Encoder reversed
to make +position measurement be forward.
SPDMotor *motorRR = new SPDMotor( 2, A1, false, 5, A4, A5); // <- NOTE: Motor Dir
pins reversed for opposite operation
void setup()
{
Serial.begin(9600);
delay(300) ;//added delay to give wireless ps2 module some time to startup,
before configuring it
//CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************
if (error == 0) {
Serial.println("Found Controller, configuration successful ");
Serial.println();
Serial.println("SPDMotor control by Aaron Hilton of Steampunk Digital");
Serial.println("=====================================================");
Serial.println("Holding L1 or R1 will activate analog joystick control.");
Serial.println("Left analog stick for forward/back and turning.");
Serial.println("Right analog stick for sideways movement.");
Serial.println("Hold both L1 and R1 for full-speed.");
}
else if (error == 1)
{
Serial.println("No controller found, check PS2 receiver is inserted the correct
way around.");
resetFunc();
}
else if (error == 2)
Serial.println("Controller found but not accepting commands.");
else if (error == 3)
Serial.println("Controller refusing to enter Pressures mode, may not support
it. ");
type = ps2x.readType();
switch (type) {
case 0:
Serial.println("Unknown Controller type found ");
break;
case 1:
Serial.println("DualShock Controller found ");
break;
case 2:
Serial.println("GuitarHero Controller found ");
break;
case 3:
Serial.println("Wireless Sony DualShock Controller found ");
break;
}
}
void loop() {
if (error == 1) //skip loop if no controller found
return;
if (ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) {
int LY = ps2x.Analog(PSS_LY);
int LX = ps2x.Analog(PSS_LX);
int RX = ps2x.Analog(PSS_RX);
float forwardNormalized = (float)(-LY + 128)/127.f;