|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +#define ENABLE_TOOLS |
| 14 | +#define ENABLE_SPP |
| 15 | +#define ENABLE_PS3 |
| 16 | +#define ENABLE_WII |
| 17 | +#define ENABLE_XBOX |
| 18 | +#define ENABLE_ADK |
| 19 | + |
| 20 | +#include "Balanduino.h" |
| 21 | +#include <Wire.h> |
| 22 | +#include <usbhub.h> |
| 23 | + |
| 24 | +#ifdef ENABLE_ADK |
| 25 | +#include <adk.h> |
| 26 | +#endif |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +#include <Kalman.h> |
| 31 | + |
| 32 | +#ifdef ENABLE_XBOX |
| 33 | +#include <XBOXRECV.h> |
| 34 | +#endif |
| 35 | +#ifdef ENABLE_SPP |
| 36 | +#include <SPP.h> |
| 37 | +#endif |
| 38 | +#ifdef ENABLE_PS3 |
| 39 | +#include <PS3BT.h> |
| 40 | +#endif |
| 41 | +#ifdef ENABLE_WII |
| 42 | +#include <Wii.h> |
| 43 | +#endif |
| 44 | + |
| 45 | + |
| 46 | +Kalman kalman; |
| 47 | + |
| 48 | +#if defined(ENABLE_SPP) || defined(ENABLE_PS3) || defined(ENABLE_WII) || defined(ENABLE_XBOX) || defined(ENABLE_ADK) |
| 49 | +#define ENABLE_USB |
| 50 | +USB Usb; |
| 51 | +#endif |
| 52 | + |
| 53 | +#ifdef ENABLE_ADK |
| 54 | + |
| 55 | +ADK adk(&Usb, "TKJ Electronics", |
| 56 | + "Balanduino", |
| 57 | + "Android App for Balanduino", |
| 58 | + "0.5.0", |
| 59 | + "https://play.google.com/store/apps/details?id=com.tkjelectronics.balanduino", |
| 60 | + "1234"); |
| 61 | +#endif |
| 62 | + |
| 63 | +#ifdef ENABLE_XBOX |
| 64 | +XBOXRECV Xbox(&Usb); |
| 65 | +#endif |
| 66 | + |
| 67 | +#if defined(ENABLE_SPP) || defined(ENABLE_PS3) || defined(ENABLE_WII) |
| 68 | +USBHub Hub(&Usb); |
| 69 | +BTD Btd(&Usb); |
| 70 | +#endif |
| 71 | + |
| 72 | +#ifdef ENABLE_SPP |
| 73 | +SPP SerialBT(&Btd, "Balanduino", "0000"); |
| 74 | +#endif |
| 75 | + |
| 76 | +#ifdef ENABLE_PS3 |
| 77 | +PS3BT PS3(&Btd); |
| 78 | +#endif |
| 79 | + |
| 80 | +#ifdef ENABLE_WII |
| 81 | +WII Wii(&Btd); |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +#endif |
| 87 | + |
| 88 | +void setup() { |
| 89 | + |
| 90 | + Serial.begin(115200); |
| 91 | + |
| 92 | + |
| 93 | + if (!checkInitializationFlags()) |
| 94 | + readEEPROMValues(); |
| 95 | + |
| 96 | + |
| 97 | + pinMode(leftEncoder1, INPUT); |
| 98 | + pinMode(leftEncoder2, INPUT); |
| 99 | + pinMode(rightEncoder1, INPUT); |
| 100 | + pinMode(rightEncoder2, INPUT); |
| 101 | + attachInterrupt(0, leftEncoder, CHANGE); |
| 102 | + attachInterrupt(1, rightEncoder, CHANGE); |
| 103 | + |
| 104 | + |
| 105 | + pinMode(leftEnable, OUTPUT); |
| 106 | + pinMode(rightEnable, OUTPUT); |
| 107 | + digitalWrite(leftEnable, HIGH); |
| 108 | + digitalWrite(rightEnable, HIGH); |
| 109 | + |
| 110 | + |
| 111 | + sbi(pwmPortDirection, leftPWM); |
| 112 | + sbi(leftPortDirection, leftA); |
| 113 | + sbi(leftPortDirection, leftB); |
| 114 | + sbi(pwmPortDirection, rightPWM); |
| 115 | + sbi(rightPortDirection, rightA); |
| 116 | + sbi(rightPortDirection, rightB); |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + TCCR1B = _BV(WGM13) | _BV(CS10); |
| 121 | + ICR1 = PWMVALUE; |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + TCCR1A = _BV(COM1A1) | _BV(COM1B1); |
| 127 | + setPWM(leftPWM, 0); |
| 128 | + setPWM(rightPWM, 0); |
| 129 | + |
| 130 | + |
| 131 | + pinMode(buzzer, OUTPUT); |
| 132 | + |
| 133 | +#ifdef ENABLE_USB |
| 134 | + if (Usb.Init() == -1) { |
| 135 | + Serial.print(F("OSC did not start")); |
| 136 | + digitalWrite(buzzer, HIGH); |
| 137 | + while (1); |
| 138 | + } |
| 139 | +#endif |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | +#ifdef ENABLE_PS3 |
| 144 | + PS3.attachOnInit(onInit); |
| 145 | +#endif |
| 146 | +#ifdef ENABLE_WII |
| 147 | + Wii.attachOnInit(onInit); |
| 148 | +#endif |
| 149 | +#ifdef ENABLE_XBOX |
| 150 | + Xbox.attachOnInit(onInit); |
| 151 | +#endif |
| 152 | + |
| 153 | + |
| 154 | + Wire.begin(); |
| 155 | + |
| 156 | + while (i2cRead(0x75, i2cBuffer, 1)); |
| 157 | + if (i2cBuffer[0] != 0x68) { |
| 158 | + Serial.print(F("Error reading sensor")); |
| 159 | + digitalWrite(buzzer, HIGH); |
| 160 | + while (1); |
| 161 | + } |
| 162 | + |
| 163 | + i2cBuffer[0] = 19; |
| 164 | + i2cBuffer[1] = 0x00; |
| 165 | + i2cBuffer[2] = 0x00; |
| 166 | + i2cBuffer[3] = 0x00; |
| 167 | + while (i2cWrite(0x19, i2cBuffer, 4, false)); |
| 168 | + while (i2cWrite(0x6B, 0x09, true)); |
| 169 | + |
| 170 | + delay(100); |
| 171 | + |
| 172 | + |
| 173 | + while (i2cRead(0x3D, i2cBuffer, 4)); |
| 174 | + accY = ((i2cBuffer[0] << 8) | i2cBuffer[1]); |
| 175 | + accZ = ((i2cBuffer[2] << 8) | i2cBuffer[3]); |
| 176 | + |
| 177 | + |
| 178 | + accAngle = (atan2((double)accY - cfg.accYzero, (double)accZ - cfg.accZzero) + PI) * RAD_TO_DEG; |
| 179 | + |
| 180 | + kalman.setAngle(accAngle); |
| 181 | + pitch = accAngle; |
| 182 | + gyroAngle = accAngle; |
| 183 | + |
| 184 | + |
| 185 | + calibrateGyro(); |
| 186 | + |
| 187 | + pinMode(LED_BUILTIN, OUTPUT); |
| 188 | + |
| 189 | + |
| 190 | + digitalWrite(buzzer, HIGH); |
| 191 | + delay(100); |
| 192 | + digitalWrite(buzzer, LOW); |
| 193 | + |
| 194 | + |
| 195 | + kalmanTimer = micros(); |
| 196 | + pidTimer = kalmanTimer; |
| 197 | + encoderTimer = kalmanTimer; |
| 198 | + imuTimer = millis(); |
| 199 | + reportTimer = imuTimer; |
| 200 | + ledTimer = imuTimer; |
| 201 | + blinkTimer = imuTimer; |
| 202 | +} |
| 203 | + |
| 204 | +void loop() { |
| 205 | +#ifdef ENABLE_WII |
| 206 | + if (Wii.wiimoteConnected) |
| 207 | + Usb.Task(); |
| 208 | +#endif |
| 209 | + |
| 210 | + |
| 211 | + while (i2cRead(0x3D, i2cBuffer, 8)); |
| 212 | + accY = ((i2cBuffer[0] << 8) | i2cBuffer[1]); |
| 213 | + accZ = ((i2cBuffer[2] << 8) | i2cBuffer[3]); |
| 214 | + gyroX = ((i2cBuffer[6] << 8) | i2cBuffer[7]); |
| 215 | + |
| 216 | + |
| 217 | + |
| 218 | + accAngle = (atan2((double)accY - cfg.accYzero, (double)accZ - cfg.accZzero) + PI) * RAD_TO_DEG; |
| 219 | + |
| 220 | + uint32_t timer = micros(); |
| 221 | + |
| 222 | + if ((accAngle < 90 && pitch > 270) || (accAngle > 270 && pitch < 90)) { |
| 223 | + kalman.setAngle(accAngle); |
| 224 | + pitch = accAngle; |
| 225 | + gyroAngle = accAngle; |
| 226 | + } else { |
| 227 | + gyroRate = ((double)gyroX - gyroXzero) / 131.0; |
| 228 | + double dt = (double)(timer - kalmanTimer) / 1000000.0; |
| 229 | + gyroAngle += gyroRate * dt; |
| 230 | + if (gyroAngle < 0 || gyroAngle > 360) |
| 231 | + gyroAngle = pitch; |
| 232 | + pitch = kalman.getAngle(accAngle, gyroRate, dt); |
| 233 | + } |
| 234 | + kalmanTimer = timer; |
| 235 | + |
| 236 | + |
| 237 | +#ifdef ENABLE_WII |
| 238 | + if (Wii.wiimoteConnected) |
| 239 | + Usb.Task(); |
| 240 | +#endif |
| 241 | + |
| 242 | + |
| 243 | + timer = micros(); |
| 244 | + |
| 245 | + |
| 246 | + if ((layingDown && (pitch < cfg.targetAngle - 10 || pitch > cfg.targetAngle + 10)) || (!layingDown && (pitch < cfg.targetAngle - 45 || pitch > cfg.targetAngle + 45))) { |
| 247 | + layingDown = true; |
| 248 | + stopAndReset(); |
| 249 | + } else { |
| 250 | + layingDown = false; |
| 251 | + updatePID(cfg.targetAngle, targetOffset, turningOffset, (double)(timer - pidTimer) / 1000000.0); |
| 252 | + } |
| 253 | + pidTimer = timer; |
| 254 | + |
| 255 | + |
| 256 | + timer = micros(); |
| 257 | + if (timer - encoderTimer >= 100000) { |
| 258 | + encoderTimer = timer; |
| 259 | + int32_t wheelPosition = getWheelsPosition(); |
| 260 | + wheelVelocity = wheelPosition - lastWheelPosition; |
| 261 | + lastWheelPosition = wheelPosition; |
| 262 | + |
| 263 | + if (abs(wheelVelocity) <= 40 && !stopped) { |
| 264 | + targetPosition = wheelPosition; |
| 265 | + stopped = true; |
| 266 | + } |
| 267 | + |
| 268 | + batteryCounter++; |
| 269 | + if (batteryCounter > 10) { |
| 270 | + batteryCounter = 0; |
| 271 | + batteryVoltage = (double)analogRead(VBAT) / 63.050847458; |
| 272 | + if (batteryVoltage < 10.2 && batteryVoltage > 5) |
| 273 | + digitalWrite(buzzer, HIGH); |
| 274 | + else |
| 275 | + digitalWrite(buzzer, LOW); |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + |
| 280 | +#ifdef ENABLE_USB |
| 281 | + readUsb(); |
| 282 | +#endif |
| 283 | +#ifdef ENABLE_TOOLS |
| 284 | + checkSerialData(); |
| 285 | +#endif |
| 286 | +#if defined(ENABLE_TOOLS) || defined(ENABLE_SPP) |
| 287 | + printValues(); |
| 288 | +#endif |
| 289 | + |
| 290 | +#if defined(ENABLE_SPP) || defined(ENABLE_PS3) || defined(ENABLE_WII) |
| 291 | + if (Btd.isReady()) { |
| 292 | + timer = millis(); |
| 293 | + if ((Btd.watingForConnection && timer - blinkTimer > 1000) || (!Btd.watingForConnection && timer - blinkTimer > 100)) { |
| 294 | + blinkTimer = timer; |
| 295 | + ledState = !ledState; |
| 296 | + digitalWrite(LED_BUILTIN, ledState); |
| 297 | + } |
| 298 | + } else if (ledState) { |
| 299 | + ledState = !ledState; |
| 300 | + digitalWrite(LED_BUILTIN, ledState); |
| 301 | + } |
| 302 | +#endif |
| 303 | +} |
| 304 | + |
0 commit comments