Skip to content

Commit d3cf4d4

Browse files
committed
working code, driving motors, with (some calibration in the PWM RC drivers)
1 parent a8375d0 commit d3cf4d4

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

robot-xbox-controller-arduino.ino

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,41 @@ void process_buttons() {
6767

6868
int16_t lh = Xbox.getAnalogHat(LeftHatY, i);
6969
int16_t rh = Xbox.getAnalogHat(RightHatY, i);
70-
Serial.print(F("L: "));
71-
Serial.print(lh);
72-
Serial.print(F("\t\tR: "));
73-
Serial.print(rh);
74-
Serial.println();
70+
71+
set_motor(c2m_scale(lh), c2m_scale(rh));
72+
// Serial.print(F("L: "));
73+
// Serial.print(lh);
74+
// Serial.print(F("\t\tR: "));
75+
// Serial.print(rh);
76+
// Serial.println();
7577
}
7678
}
7779
}
7880

79-
void set_motor(int16_t left, int16_t right) {
81+
// dead zone threshold for the xbox joystick
82+
#define DEAD_ZONE_THRESHOLD 5000
83+
84+
int16_t c2m_scale(int16_t stick) {
85+
// establish a deadzone
86+
if (abs(stick) < DEAD_ZONE_THRESHOLD) {
87+
stick = 0;
88+
}
89+
90+
// make it range from 0 to 250 or so
91+
int8_t ret = stick/260 + 126;
92+
93+
return ret;
94+
}
95+
96+
void set_motor(int8_t left, int8_t right) {
8097
// FIXME -- do motor control here
98+
Serial.print(F("L: "));
99+
Serial.print(left);
100+
Serial.print(F("\t\tR: "));
101+
Serial.print(right);
102+
Serial.println();
103+
analogWrite(2, left);
104+
analogWrite(3, right);
81105
}
82106

83107

0 commit comments

Comments
 (0)