-
Notifications
You must be signed in to change notification settings - Fork 769
/
Ada_BLE_RC.ino
413 lines (317 loc) · 10.1 KB
/
Ada_BLE_RC.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// SPDX-FileCopyrightText: 2018 Limor Fried for Adafruit Industries
//
// SPDX-License-Identifier: MIT
/*********************************************************************
This is an example for our nRF51822 based Bluefruit LE modules
Modified to drive a 3-wheeled BLE Robot Rover! by http://james.devi.to
Pick one up today in the Adafruit shop!
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in
any redistribution
*********************************************************************/
#include <string.h>
#include <Arduino.h>
#include <SPI.h>
#if not defined (_VARIANT_ARDUINO_DUE_X_)
#include <SoftwareSerial.h>
#endif
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"
#include "BluefruitConfig.h"
#include <Wire.h>
#include <Adafruit_MotorShield.h>
// #include "utility/Adafruit_PWMServoDriver.h"
// #include <Servo.h>
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// And connect 2 DC motors to port M3 & M4 !
Adafruit_DCMotor *L_MOTOR = AFMS.getMotor(4);
Adafruit_DCMotor *R_MOTOR = AFMS.getMotor(3);
//not used, testing acceleration
// int accelTime = 200;
//Name your RC here
String BROADCAST_NAME = "adafruit red robot rover";
String BROADCAST_CMD = String("AT+GAPDEVNAME=" + BROADCAST_NAME);
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
// A small helper
void error(const __FlashStringHelper*err) {
Serial.println(err);
while (1);
}
// function prototypes over in packetparser.cpp
uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout);
float parsefloat(uint8_t *buffer);
void printHex(const uint8_t * data, const uint32_t numBytes);
// the packet buffer
extern uint8_t packetbuffer[];
char buf[60];
/**************************************************************************/
/*!
@brief Sets up the HW an the BLE module (this function is called
automatically on startup)
*/
/**************************************************************************/
void setup(void)
{
Serial.begin(9600);
AFMS.begin(); // create with the default frequency 1.6KHz
// turn on motors
L_MOTOR->setSpeed(0);
L_MOTOR->run(RELEASE);
R_MOTOR->setSpeed(0);
R_MOTOR->run(RELEASE);
Serial.begin(115200);
Serial.println(F("Adafruit Bluefruit Robot Controller Example"));
Serial.println(F("-----------------------------------------"));
/* Initialize the module */
BLEsetup();
}
int velocity = 0;
float x, y;
int L_restrict = 0;
int R_restrict = 0;
unsigned long lastAccelPacket = 0;
bool modeToggle = false;
void loop(void)
{
// read new packet data
uint8_t len = readPacket(&ble, BLE_READPACKET_TIMEOUT);
// if (len == 0) return;
// Read from Accelerometer input
if( accelMode() ) {
lastAccelPacket = millis();
modeToggle = true;
return;
}
// Stop motors if accelerometer data is turned off (100ms timeout)
if( millis() - lastAccelPacket > 100 & modeToggle) {
L_MOTOR->run(RELEASE);
R_MOTOR->run(RELEASE);
modeToggle = false;
return;
}
//if no accelerometer, use control pad
if( !modeToggle ) buttonMode();
}
bool accelMode(){
if (packetbuffer[1] == 'A') {
x = parsefloat( packetbuffer + 2 );
y = parsefloat( packetbuffer + 6 );
if( x <= -0.55 ){
x += 0.55;
x *= -100.0;
L_MOTOR->run( BACKWARD );
R_MOTOR->run( BACKWARD );
if( x >= 45 ) x = 45;
if( x <= 0 ) x = 0;
velocity = map( x, 0, 45, 0 ,255 );
}
else if( x >= -0.25 ){
x+= 0.25;
x *= 100;
L_MOTOR->run( FORWARD );
R_MOTOR->run( FORWARD );
if( x>= 45 ) x = 45;
if( x<= 0 ) x = 0;
velocity = map( x, 0, 45, 0, 255 );
}
else{
L_MOTOR->run( RELEASE );
R_MOTOR->run( RELEASE );
velocity = 0;
}
//account for L / R accel
if( y >= 0.1 ){
y -= 0.1;
y *= 100;
if( y >= 50 ) y = 50;
if( y <= 0 ) y = 0;
L_restrict = fscale( y, 0.0, 50.0, 0.0, 100.0, -4.0 );
}
else if( y <= -0.1 ){
y += 0.1;
y *= -100;
if( y>= 50 ) y = 50;
if( y<= 0 ) y = 0;
R_restrict = fscale( y, 0.0, 50.0, 0.0, 100.0, -4.0 );
}
else{
L_restrict = 0;
R_restrict = 0;
}
float Lpercent = ( 100.0 - L_restrict ) / 100.00 ;
float Rpercent = ( 100.0 - R_restrict ) / 100.00 ;
// Serial.print( x );
// Serial.print( "\t" );
// Serial.print( Lpercent );
// Serial.print( "\t" );
// Serial.print( velocity );
// Serial.print( "\t" );
// Serial.println( Rpercent );
L_MOTOR->setSpeed( velocity * Lpercent );
R_MOTOR->setSpeed( velocity * Rpercent );
return true;
}
return false;
}
bool isMoving = false;
bool buttonMode(){
static unsigned long lastPress = 0;
// Buttons
if (packetbuffer[1] == 'B') {
uint8_t buttnum = packetbuffer[2] - '0';
boolean pressed = packetbuffer[3] - '0';
// Serial.println(buttnum);
Serial.println(isMoving);
if (pressed) {
isMoving = true;
if(buttnum == 5){
L_MOTOR->run(FORWARD);
R_MOTOR->run(FORWARD);
}
if(buttnum == 6){
L_MOTOR->run(BACKWARD);
R_MOTOR->run(BACKWARD);
}
if(buttnum == 7){
L_MOTOR->run(RELEASE);
R_MOTOR->run(FORWARD);
}
if(buttnum == 8){
L_MOTOR->run(FORWARD);
R_MOTOR->run(RELEASE);
}
lastPress = millis();
L_MOTOR->setSpeed(255);
R_MOTOR->setSpeed(255);
}
else {
isMoving = false;
L_MOTOR->run(RELEASE);
R_MOTOR->run(RELEASE);
}
return true;
}
// if(isMoving){
// unsigned long timeSincePress = millis() - lastPress;
// if(timeSincePress <= accelTime){
// Serial.println( timeSincePress ) ;
// int motorSpeed = map( timeSincePress, 0, accelTime, 0, 255 );
// L_MOTOR->setSpeed(motorSpeed);
// R_MOTOR->setSpeed(motorSpeed);
// }
// else{
// // full speed ahead!
// L_MOTOR->setSpeed(255);
// R_MOTOR->setSpeed(255);
// }
// }
return false;
}
void BLEsetup(){
Serial.print(F("Initialising the Bluefruit LE module: "));
if ( !ble.begin(VERBOSE_MODE) )
{
error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
}
Serial.println( F("OK!") );
/* Perform a factory reset to make sure everything is in a known state */
Serial.println(F("Performing a factory reset: "));
if (! ble.factoryReset() ){
error(F("Couldn't factory reset"));
}
//Convert the name change command to a char array
BROADCAST_CMD.toCharArray(buf, 60);
//Change the broadcast device name here!
if(ble.sendCommandCheckOK(buf)){
Serial.println("name changed");
}
delay(250);
//reset to take effect
if(ble.sendCommandCheckOK("ATZ")){
Serial.println("resetting");
}
delay(250);
//Confirm name change
ble.sendCommandCheckOK("AT+GAPDEVNAME");
/* Disable command echo from Bluefruit */
ble.echo(false);
Serial.println("Requesting Bluefruit info:");
/* Print Bluefruit information */
ble.info();
Serial.println(F("Please use Adafruit Bluefruit LE app to connect in Controller mode"));
Serial.println(F("Then activate/use the sensors, color picker, game controller, etc!"));
Serial.println();
ble.verbose(false); // debug info is a little annoying after this point!
/* Wait for connection */
while (! ble.isConnected()) {
delay(500);
}
Serial.println(F("*****************"));
// Set Bluefruit to DATA mode
Serial.println( F("Switching to DATA mode!") );
ble.setMode(BLUEFRUIT_MODE_DATA);
Serial.println(F("*****************"));
}
//Logarithmic mapping function from http://playground.arduino.cc/Main/Fscale
float fscale( float inputValue, float originalMin, float originalMax, float newBegin, float newEnd, float curve){
float OriginalRange = 0;
float NewRange = 0;
float zeroRefCurVal = 0;
float normalizedCurVal = 0;
float rangedValue = 0;
boolean invFlag = 0;
// condition curve parameter
// limit range
if (curve > 10) curve = 10;
if (curve < -10) curve = -10;
curve = (curve * -.1) ; // - invert and scale - this seems more intuitive - postive numbers give more weight to high end on output
curve = pow(10, curve); // convert linear scale into lograthimic exponent for other pow function
/*
Serial.println(curve * 100, DEC); // multply by 100 to preserve resolution
Serial.println();
*/
// Check for out of range inputValues
if (inputValue < originalMin) {
inputValue = originalMin;
}
if (inputValue > originalMax) {
inputValue = originalMax;
}
// Zero Refference the values
OriginalRange = originalMax - originalMin;
if (newEnd > newBegin){
NewRange = newEnd - newBegin;
}
else
{
NewRange = newBegin - newEnd;
invFlag = 1;
}
zeroRefCurVal = inputValue - originalMin;
normalizedCurVal = zeroRefCurVal / OriginalRange; // normalize to 0 - 1 float
/*
Serial.print(OriginalRange, DEC);
Serial.print(" ");
Serial.print(NewRange, DEC);
Serial.print(" ");
Serial.println(zeroRefCurVal, DEC);
Serial.println();
*/
// Check for originalMin > originalMax - the math for all other cases i.e. negative numbers seems to work out fine
if (originalMin > originalMax ) {
return 0;
}
if (invFlag == 0){
rangedValue = (pow(normalizedCurVal, curve) * NewRange) + newBegin;
}
else // invert the ranges
{
rangedValue = newBegin - (pow(normalizedCurVal, curve) * NewRange);
}
return rangedValue;
}