From eaecfead6390db790b5346e416ea5ee38c33cf98 Mon Sep 17 00:00:00 2001 From: Carlos Puchol Date: Wed, 17 Jun 2015 19:06:10 -0700 Subject: [PATCH] golden working code between [-77, 127, 177] range --- motor_charz/motor_charz.ino | 94 +++++++++++++++++++++++++++++++ robot-xbox-controller-arduino.ino | 8 +-- 2 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 motor_charz/motor_charz.ino diff --git a/motor_charz/motor_charz.ino b/motor_charz/motor_charz.ino new file mode 100644 index 0000000..ec79791 --- /dev/null +++ b/motor_charz/motor_charz.ino @@ -0,0 +1,94 @@ +/* + * Robot controller with the Xbox Wireless Receiver library on Arduino + */ + +#include + +// Satisfy the IDE, which needs to see the include statment in the ino too. +#ifdef dobogusinclude +#include +#include +#endif + +USB Usb; +XBOXRECV Xbox(&Usb); + +uint8_t controlling = 0; + +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 + int done = 0; + + Serial.print(F("\r\nWelcome to the robot controller software!")); + + while (!done) { + if (Usb.Init() == -1) { + Serial.print(F("\r\nOSC did not start")); + delay(2000); + } else { + done = 1; + } + } + //Xbox.setLedBlink(ALL, 0); + Serial.print(F("\r\nPress the Start button to start controlling the robot")); +} + +void loop() { + Usb.Task(); + if (Xbox.XboxReceiverConnected) { + process_buttons(); + } +} + +uint8_t current_motor = 127; + +void process_buttons() { + for (uint8_t i = 0; i < 4; i++) { + if (Xbox.Xbox360Connected[i]) { + if (!controlling) { + if (Xbox.getButtonClick(START, i)) { + Xbox.setLedMode(ALTERNATING, i); + controlling = 1; + } else { + return; + } + } + + if (Xbox.getButtonClick(BACK, i) || Xbox.getButtonClick(XBOX, i) || Xbox.getButtonClick(SYNC, i)) { + controlling = 0; + // stop everything + set_motor(127, 127); + Xbox.setLedBlink(ALL, i); + Serial.print(F("Battery: ")); + Serial.print(Xbox.getBatteryLevel(i)); // The battery level in the range 0-3 + return; + } + + if (Xbox.getButtonClick(Y, i) && current_motor < 250) { + current_motor += 5; + set_motor(current_motor, 0); + } + if (Xbox.getButtonClick(A, i) && current_motor > 0) { + current_motor -= 5; + set_motor(current_motor, 0); + } + } + } +} + + +void set_motor(uint8_t left, uint8_t right) { + // FIXME -- do motor control here + Serial.print(F("L: ")); + Serial.print(left); + Serial.print(F("\t\tR: ")); + Serial.print(right); + Serial.println(); + analogWrite(2, left); + analogWrite(3, right); +} + + diff --git a/robot-xbox-controller-arduino.ino b/robot-xbox-controller-arduino.ino index 9412950..b46fe0f 100644 --- a/robot-xbox-controller-arduino.ino +++ b/robot-xbox-controller-arduino.ino @@ -79,21 +79,21 @@ void process_buttons() { } // dead zone threshold for the xbox joystick -#define DEAD_ZONE_THRESHOLD 5000 +#define DEAD_ZONE_THRESHOLD 6000 -int16_t c2m_scale(int16_t stick) { +uint8_t c2m_scale(int16_t stick) { // establish a deadzone if (abs(stick) < DEAD_ZONE_THRESHOLD) { stick = 0; } // make it range from 0 to 250 or so - int8_t ret = stick/260 + 126; + uint8_t ret = stick/656 + 127; return ret; } -void set_motor(int8_t left, int8_t right) { +void set_motor(uint8_t left, uint8_t right) { // FIXME -- do motor control here Serial.print(F("L: ")); Serial.print(left);