I am currently working on a robot and I have finally gotten the circuitry and the coded to work nicely, said robot is and underwater robot. I am using an arduino to control the robot by having it output servo values between 0-180 to the motor controllers. this works great for about 2-20 seconds before it freezes for a period of time and then works again, this cycle repeats it's self. I have a USB host sheild that the arduino is pluged in to then 4 pwm pins are used to communicate with the motor controllers. I am using an Uno if that matter, my code is what follows.
#include <Servo.h>
#include <XBOXUSB.h>
#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>
#endif
// 0 is full reverse, 90 full stop, 180 full forward
USB Usb; //declares a Usb device usb
XBOXUSB Xbox(&Usb); //sets a xbox usb device at usbs location
Servo Y1M; // Servo for Y motion (Forward, Backward)
Servo Y2M; // Servo for x motion (Left, Right)
Servo Z1M; // Servo for z motion (Up, down)
Servo Z2M; // Servo for R motion (Rotation)
void setup()
{
Y1M.attach(10, 1000, 2000); //Pick the pin for Y motion
Y2M.attach(3, 1000, 2000); //Pick the pin for X motion
Z1M.attach(5, 1000, 2000); //Pick the pin for Z motion
Z2M.attach(6, 1000, 2000); //Pick the pin for R motion
Serial.begin(115200);
Serial.begin(115200);
#if !defined(__MIPSEL__)
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while (1); //halt
}
Serial.print(F("\r\nXBOX USB Library Started"));
Y1M.write(90);
Y2M.write(90);
Z1M.write(90);
Z2M.write(90);
}
void joysticks()
{
int Prop;
int input;
int Turn;
if (Xbox.getAnalogHat(LeftHatY) > 7500 || Xbox.getAnalogHat(LeftHatY) < -7500) //Sets dead zones for left sticks Y
{
input = Xbox.getAnalogHat(LeftHatY); //set input equal to the y value of the stick
if (input > 0)
{
Prop = (input / 364.07777) + 90;
}
else if (input < 0)
{
Prop = (input / 364.07777) + 90;
}
Serial.print(F("\nY: "));
Serial.print(Prop);
Y1M.write(Prop);
Y2M.write(Prop);
}
if (Xbox.getAnalogHat(LeftHatY) < 7500 && Xbox.getAnalogHat(LeftHatY) > -7500) //fullstop if inside deadzones
{
Serial.print(F("\nY:90"));
Y1M.write(90);
Y2M.write(90);
}
if (Xbox.getAnalogHat(RightHatX) > 7500 || Xbox.getAnalogHat(RightHatX) < -7500) //Sets dead zones for left sticks Y
{
Turn = Xbox.getAnalogHat(RightHatX); //set input equal to the y value of the stick
if (Turn > 0)// turn left if the joystick is pushed to the left
{
Serial.print(F("\nTurning left"));
Y1M.write(160);
Y2M.write(100);
}
else if (Turn < 0)//turn right if joystick pushed right
{
Serial.print(F("\nTurning right"));
Y1M.write(100);
Y2M.write(160);
}
}
}
void updown()
{
int input;
if (Xbox.getButtonPress(L2))// take 0-255 and give a value between 90-0
{
input = Xbox.getButtonPress(L2) / -2.833333333333333 + 90;
Z1M.write(input);
Z2M.write(input);
}
else if (Xbox.getButtonPress(R2))//take 0-255 and give a value between 90-180
{
input = (Xbox.getButtonPress(R2) / 2.833333333333333) + 90;
Z1M.write(input);
Z2M.write(input);
}
else // fullstop
{
Z1M.write(90);
Z2M.write(90);
}
}
void loop() {
Usb.Task();
if (Xbox.Xbox360Connected)
{
joysticks();
updown();
delay(200);
}
}