Continuing the discussion from Maestro serial control with Arduino & USB Shield:
After remapping some pins on both the MC33926 shield as well as the USB Host Shield, I have been progressing quite well with my goal of controlling a robot with a wired Xbox controller. The hardware seems to be under control, but now I have hit what I think is a software related snag.
To break it down, here is an excerpt from the sample code from the USB Host Shield Library for controlling the xbox. Note that there is more to loop() than I have reproduced here, but I have cut it for brevity/relevance.
/*
Example sketch for the Xbox 360 USB library - developed by Kristian Lauszus
*/
#include <XBOXUSB.h>
// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>
#endif
USB Usb;
XBOXUSB Xbox(&Usb);
void setup() {
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"));
}
void loop() {
Usb.Task();
if (Xbox.Xbox360Connected) {
if (Xbox.getButtonPress(L2) || Xbox.getButtonPress(R2)) {
Serial.print("L2: ");
Serial.print(Xbox.getButtonPress(L2));
Serial.print("\tR2: ");
Serial.println(Xbox.getButtonPress(R2));
Xbox.setRumbleOn(Xbox.getButtonPress(L2), Xbox.getButtonPress(R2));
} else
Xbox.setRumbleOn(0, 0);
}
}
With the above code, if I hold down either trigger, the rumble is activated, and the controller shakes until I release the trigger. I tried to modify this code to control motors1 and 2 with the left and right triggers, respectively. It works, but only briefly. After about three seconds of continuously holding in the triggers, the speed gets locked in. Even if I release the triggers, the motors continue to move. Could this be an issue with the timer? Or maybe I need to add in a longer delay? The troublesome portion of the code follows below.
#include <XBOXUSB.h>
// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>
#endif
USB Usb;
XBOXUSB Xbox(&Usb);
#include "DualMC33926MotorShield.h"
#include <PololuMaestro.h>
#include <SoftwareSerial.h>
SoftwareSerial maestroSerial(16,17);
DualMC33926MotorShield md;
MicroMaestro maestro(maestroSerial);
long int XboxLT;
long int XboxRT;
void setup() {
Serial.begin(9600);
maestroSerial.begin(9600);
md.init();
md.setM1Speed(0);
md.setM2Speed(0);
#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"));
}
void loop() {
Usb.Task();
if (Xbox.Xbox360Connected) {
if (Xbox.getButtonPress(L2) || Xbox.getButtonPress(R2)) {
Serial.print("\tL2:");
Serial.print(Xbox.getButtonPress(L2));
XboxLT = map((Xbox.getButtonPress(L2)), 0, 255, 0, 300);
md.setM2Speed(XboxLT);
Serial.print("\tXboxLT:");
Serial.print(XboxLT);
Serial.print("\tR2:");
Serial.print(Xbox.getButtonPress(R2));
XboxRT = map((Xbox.getButtonPress(R2)), 0, 255, 0, 300);
md.setM1Speed(XboxRT);
Serial.print("\tXboxRT:");
Serial.print(XboxRT);
Serial.println();
delay(50);
}else
md.setM1Speed(0);
md.setM2Speed(0);
}
}
Thanks!
Mike