Skip to content

Commit eaecfea

Browse files
committed
golden working code between [-77, 127, 177] range
1 parent d3cf4d4 commit eaecfea

File tree

2 files changed

+98
-4
lines changed

2 files changed

+98
-4
lines changed

motor_charz/motor_charz.ino

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Robot controller with the Xbox Wireless Receiver library on Arduino
3+
*/
4+
5+
#include <XBOXRECV.h>
6+
7+
// Satisfy the IDE, which needs to see the include statment in the ino too.
8+
#ifdef dobogusinclude
9+
#include <spi4teensy3.h>
10+
#include <SPI.h>
11+
#endif
12+
13+
USB Usb;
14+
XBOXRECV Xbox(&Usb);
15+
16+
uint8_t controlling = 0;
17+
18+
void setup() {
19+
Serial.begin(115200);
20+
#if !defined(__MIPSEL__)
21+
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
22+
#endif
23+
int done = 0;
24+
25+
Serial.print(F("\r\nWelcome to the robot controller software!"));
26+
27+
while (!done) {
28+
if (Usb.Init() == -1) {
29+
Serial.print(F("\r\nOSC did not start"));
30+
delay(2000);
31+
} else {
32+
done = 1;
33+
}
34+
}
35+
//Xbox.setLedBlink(ALL, 0);
36+
Serial.print(F("\r\nPress the Start button to start controlling the robot"));
37+
}
38+
39+
void loop() {
40+
Usb.Task();
41+
if (Xbox.XboxReceiverConnected) {
42+
process_buttons();
43+
}
44+
}
45+
46+
uint8_t current_motor = 127;
47+
48+
void process_buttons() {
49+
for (uint8_t i = 0; i < 4; i++) {
50+
if (Xbox.Xbox360Connected[i]) {
51+
if (!controlling) {
52+
if (Xbox.getButtonClick(START, i)) {
53+
Xbox.setLedMode(ALTERNATING, i);
54+
controlling = 1;
55+
} else {
56+
return;
57+
}
58+
}
59+
60+
if (Xbox.getButtonClick(BACK, i) || Xbox.getButtonClick(XBOX, i) || Xbox.getButtonClick(SYNC, i)) {
61+
controlling = 0;
62+
// stop everything
63+
set_motor(127, 127);
64+
Xbox.setLedBlink(ALL, i);
65+
Serial.print(F("Battery: "));
66+
Serial.print(Xbox.getBatteryLevel(i)); // The battery level in the range 0-3
67+
return;
68+
}
69+
70+
if (Xbox.getButtonClick(Y, i) && current_motor < 250) {
71+
current_motor += 5;
72+
set_motor(current_motor, 0);
73+
}
74+
if (Xbox.getButtonClick(A, i) && current_motor > 0) {
75+
current_motor -= 5;
76+
set_motor(current_motor, 0);
77+
}
78+
}
79+
}
80+
}
81+
82+
83+
void set_motor(uint8_t left, uint8_t right) {
84+
// FIXME -- do motor control here
85+
Serial.print(F("L: "));
86+
Serial.print(left);
87+
Serial.print(F("\t\tR: "));
88+
Serial.print(right);
89+
Serial.println();
90+
analogWrite(2, left);
91+
analogWrite(3, right);
92+
}
93+
94+

robot-xbox-controller-arduino.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,21 @@ void process_buttons() {
7979
}
8080

8181
// dead zone threshold for the xbox joystick
82-
#define DEAD_ZONE_THRESHOLD 5000
82+
#define DEAD_ZONE_THRESHOLD 6000
8383

84-
int16_t c2m_scale(int16_t stick) {
84+
uint8_t c2m_scale(int16_t stick) {
8585
// establish a deadzone
8686
if (abs(stick) < DEAD_ZONE_THRESHOLD) {
8787
stick = 0;
8888
}
8989

9090
// make it range from 0 to 250 or so
91-
int8_t ret = stick/260 + 126;
91+
uint8_t ret = stick/656 + 127;
9292

9393
return ret;
9494
}
9595

96-
void set_motor(int8_t left, int8_t right) {
96+
void set_motor(uint8_t left, uint8_t right) {
9797
// FIXME -- do motor control here
9898
Serial.print(F("L: "));
9999
Serial.print(left);

0 commit comments

Comments
 (0)