|
| 1 | +// SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +// Triangular leg robot. |
| 6 | + |
| 7 | +// Bluetooth code is from Feather M0 Bluefruit controller example. |
| 8 | +// Explainatory comments kept intact. |
| 9 | + |
| 10 | +// Adafruit invests time and resources providing this open source code. |
| 11 | +// Please support Adafruit and open source hardware by purchasing |
| 12 | +// products from Adafruit! |
| 13 | + |
| 14 | +// Written by Dave Astels for Adafruit Industries |
| 15 | +// Copyright (c) 2018 Adafruit Industries |
| 16 | +// Licensed under the MIT license. |
| 17 | + |
| 18 | +// All text above must be included in any redistribution. |
| 19 | + |
| 20 | +#include <stdarg.h> |
| 21 | +#include <string.h> |
| 22 | +#include <Arduino.h> |
| 23 | +#include <SPI.h> |
| 24 | +#include "Adafruit_BLE.h" |
| 25 | +#include "Adafruit_BluefruitLE_SPI.h" |
| 26 | +#include "Adafruit_BluefruitLE_UART.h" |
| 27 | + |
| 28 | +#include "BluefruitConfig.h" |
| 29 | + |
| 30 | +#include "Adafruit_Crickit.h" |
| 31 | +#include "seesaw_motor.h" |
| 32 | + |
| 33 | +#define FACTORYRESET_ENABLE 1 |
| 34 | +#define MINIMUM_FIRMWARE_VERSION "0.6.6" |
| 35 | +#define MODE_LED_BEHAVIOUR "MODE" |
| 36 | + |
| 37 | +// function prototypes over in packetparser.cpp |
| 38 | +uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout); |
| 39 | +float parsefloat(uint8_t *buffer); |
| 40 | +void printHex(const uint8_t * data, const uint32_t numBytes); |
| 41 | + |
| 42 | +// the packet buffer |
| 43 | +extern uint8_t packetbuffer[]; |
| 44 | + |
| 45 | +//#define DEBUG 1 |
| 46 | + |
| 47 | + |
| 48 | +Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); |
| 49 | + |
| 50 | + |
| 51 | +//------------------------------------------------------------------------------ |
| 52 | +// setup crickit & motors |
| 53 | + |
| 54 | +Adafruit_Crickit crickit; |
| 55 | +seesaw_Motor right_leg(&crickit); |
| 56 | +seesaw_Motor left_leg(&crickit); |
| 57 | + |
| 58 | +seesaw_Motor *legs[2] = {&right_leg, &left_leg}; |
| 59 | +const __FlashStringHelper *leg_names[] = {F("right"), F("left")}; |
| 60 | + |
| 61 | +const int RIGHT = 0; |
| 62 | +const int LEFT = 1; |
| 63 | + |
| 64 | +//------------------------------------------------------------------------------ |
| 65 | +// conditional output routines |
| 66 | + |
| 67 | +void error(const __FlashStringHelper *err) |
| 68 | +{ |
| 69 | + digitalWrite(13, HIGH); |
| 70 | +#ifdef DEBUG |
| 71 | + Serial.println(err); |
| 72 | +#endif |
| 73 | + while (1); |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +void log(const __FlashStringHelper *msg) |
| 78 | +{ |
| 79 | +#ifdef DEBUG |
| 80 | + Serial.println(msg); |
| 81 | +#endif |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +void set_leg(int leg, float velocity) |
| 87 | +{ |
| 88 | + if (leg != RIGHT && leg != LEFT) { |
| 89 | + error(F("Bad leg specifier")); |
| 90 | + } |
| 91 | + if (velocity < -1.0 || velocity > 1.0) { |
| 92 | + error(F("Velocity out of -1.0//1.0 range")); |
| 93 | + } |
| 94 | + |
| 95 | +#ifdef DEBUG |
| 96 | + Serial.print(F("Setting ")); |
| 97 | + Serial.print(leg_names[leg]); |
| 98 | + Serial.print(F(" to ")); |
| 99 | + Serial.println(velocity); |
| 100 | +#endif |
| 101 | + |
| 102 | + legs[leg]->throttle(velocity); |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | +void stop() |
| 107 | +{ |
| 108 | + set_leg(RIGHT, 0.0); |
| 109 | + set_leg(LEFT, 0.0); |
| 110 | +} |
| 111 | + |
| 112 | + |
| 113 | +void forward(float speed) |
| 114 | +{ |
| 115 | + set_leg(RIGHT, speed); |
| 116 | + set_leg(LEFT, speed); |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +void reverse(float speed) |
| 121 | +{ |
| 122 | + set_leg(RIGHT, speed * -1); |
| 123 | + set_leg(LEFT, speed * -1); |
| 124 | +} |
| 125 | + |
| 126 | + |
| 127 | +void rotate_clockwise(float speed) |
| 128 | +{ |
| 129 | + set_leg(RIGHT, speed * -1); |
| 130 | + set_leg(LEFT, speed); |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | +void rotate_counterclockwise(float speed) |
| 135 | +{ |
| 136 | + set_leg(RIGHT, speed); |
| 137 | + set_leg(LEFT, speed * -1); |
| 138 | +} |
| 139 | + |
| 140 | + |
| 141 | +void initialize() |
| 142 | +{ |
| 143 | + stop(); |
| 144 | +} |
| 145 | + |
| 146 | + |
| 147 | +//------------------------------------------------------------------------------ |
| 148 | +// Start things up |
| 149 | + |
| 150 | +void setup() |
| 151 | +{ |
| 152 | + pinMode(13, OUTPUT); |
| 153 | + digitalWrite(13, LOW); |
| 154 | + |
| 155 | +#ifdef DEBUG |
| 156 | + while (!Serial); // required for Flora & Micro |
| 157 | + delay(500); |
| 158 | + Serial.begin(115200); |
| 159 | +#endif |
| 160 | + |
| 161 | + log(F("FlippyBot")); |
| 162 | + log(F("-----------------------------------------")); |
| 163 | + |
| 164 | + // Initialise the module |
| 165 | + log(F("Initialising the Bluefruit LE module: ")); |
| 166 | + |
| 167 | + if ( !ble.begin(VERBOSE_MODE) ) |
| 168 | + { |
| 169 | + error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?")); |
| 170 | + } |
| 171 | + |
| 172 | + log( F("OK!") ); |
| 173 | + |
| 174 | + if ( FACTORYRESET_ENABLE ) |
| 175 | + { |
| 176 | + // Perform a factory reset to make sure everything is in a known state |
| 177 | + log(F("Performing a factory reset: ")); |
| 178 | + if ( ! ble.factoryReset() ){ |
| 179 | + error(F("Couldn't factory reset")); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + // Disable command echo from Bluefruit |
| 184 | + ble.echo(false); |
| 185 | + |
| 186 | + log(F("Requesting Bluefruit info:")); |
| 187 | + // Print Bluefruit information |
| 188 | + ble.info(); |
| 189 | + |
| 190 | + log(F("Please use Adafruit Bluefruit LE app to connect in Controller mode")); |
| 191 | + log(F("Then activate/use the sensors, color picker, game controller, etc!\n")); |
| 192 | + |
| 193 | + ble.verbose(false); // debug info is a little annoying after this point! |
| 194 | + |
| 195 | + // Wait for connection |
| 196 | + while (! ble.isConnected()) { |
| 197 | + delay(500); |
| 198 | + } |
| 199 | + |
| 200 | + log(F("******************************")); |
| 201 | + |
| 202 | + // LED Activity command is only supported from 0.6.6 |
| 203 | + if ( ble.isVersionAtLeast(MINIMUM_FIRMWARE_VERSION) ) |
| 204 | + { |
| 205 | + // Change Mode LED Activity |
| 206 | + log(F("Change LED activity to " MODE_LED_BEHAVIOUR)); |
| 207 | + ble.sendCommandCheckOK("AT+HWModeLED=" MODE_LED_BEHAVIOUR); |
| 208 | + } |
| 209 | + |
| 210 | + // Set Bluefruit to DATA mode |
| 211 | + log( F("Switching to DATA mode!") ); |
| 212 | + ble.setMode(BLUEFRUIT_MODE_DATA); |
| 213 | + |
| 214 | + log(F("******************************")); |
| 215 | + |
| 216 | + if (!crickit.begin()) { |
| 217 | + error(F("Error initializing CRICKIT!")); |
| 218 | + } |
| 219 | + log(F("Crickit started")); |
| 220 | + |
| 221 | + right_leg.attach(CRICKIT_MOTOR_A1, CRICKIT_MOTOR_A2); |
| 222 | + left_leg.attach(CRICKIT_MOTOR_B1, CRICKIT_MOTOR_B2); |
| 223 | +} |
| 224 | + |
| 225 | + |
| 226 | +// Fill these functions in with the movement scripts you want attached to |
| 227 | +// the controller's 1-4 buttons |
| 228 | + |
| 229 | +void demo1() |
| 230 | +{ |
| 231 | + forward(1.0); |
| 232 | + delay(5000); |
| 233 | + rotate_clockwise(1.0); |
| 234 | + delay(2000); |
| 235 | + forward(0.75); |
| 236 | + delay(4000); |
| 237 | + rotate_counterclockwise(1.0); |
| 238 | + delay(3000); |
| 239 | + stop(); |
| 240 | +} |
| 241 | + |
| 242 | + |
| 243 | +void demo2() |
| 244 | +{ |
| 245 | +} |
| 246 | + |
| 247 | + |
| 248 | +void demo3() |
| 249 | +{ |
| 250 | +} |
| 251 | + |
| 252 | + |
| 253 | +void demo4() |
| 254 | +{ |
| 255 | +} |
| 256 | + |
| 257 | + |
| 258 | +//------------------------------------------------------------------------------ |
| 259 | +// Main loop |
| 260 | + |
| 261 | +void loop() |
| 262 | +{ |
| 263 | + // Wait for new data to arrive |
| 264 | + uint8_t len = readPacket(&ble, BLE_READPACKET_TIMEOUT); |
| 265 | + if (len == 0) return; |
| 266 | + |
| 267 | + // Got a packet! |
| 268 | + // printHex(packetbuffer, len); |
| 269 | + |
| 270 | + // Buttons |
| 271 | + if (packetbuffer[1] == 'B') { |
| 272 | + uint8_t buttnum = packetbuffer[2] - '0'; |
| 273 | + boolean pressed = packetbuffer[3] - '0'; |
| 274 | + |
| 275 | +#ifdef DEBUG |
| 276 | + Serial.print ("Button "); Serial.print(buttnum); |
| 277 | + if (pressed) { |
| 278 | + Serial.println(" pressed"); |
| 279 | + } else { |
| 280 | + Serial.println(" released"); |
| 281 | + } |
| 282 | +#endif |
| 283 | + switch(buttnum) { |
| 284 | + case 1: |
| 285 | + if (pressed) { |
| 286 | + demo1(); |
| 287 | + } |
| 288 | + break; |
| 289 | + case 2: |
| 290 | + if (pressed) { |
| 291 | + demo2(); |
| 292 | + } |
| 293 | + break; |
| 294 | + case 3: |
| 295 | + if (pressed) { |
| 296 | + demo3(); |
| 297 | + } |
| 298 | + break; |
| 299 | + case 4: |
| 300 | + if (pressed) { |
| 301 | + demo4(); |
| 302 | + } |
| 303 | + break; |
| 304 | + case 5: |
| 305 | + if (pressed) { |
| 306 | + forward(1.0); |
| 307 | + } else { |
| 308 | + stop(); |
| 309 | + } |
| 310 | + break; |
| 311 | + case 6: |
| 312 | + if (pressed) { |
| 313 | + reverse(1.0); |
| 314 | + } else { |
| 315 | + stop(); |
| 316 | + } |
| 317 | + break; |
| 318 | + case 7: |
| 319 | + if (pressed) { |
| 320 | + rotate_counterclockwise(1.0); |
| 321 | + } else { |
| 322 | + stop(); |
| 323 | + } |
| 324 | + break; |
| 325 | + case 8: |
| 326 | + if (pressed) { |
| 327 | + rotate_clockwise(1.0); |
| 328 | + } else { |
| 329 | + stop(); |
| 330 | + } |
| 331 | + break; |
| 332 | + } |
| 333 | + } |
| 334 | +} |
0 commit comments