-
Notifications
You must be signed in to change notification settings - Fork 1
/
BluetoothGlove.ino
181 lines (145 loc) · 4.8 KB
/
BluetoothGlove.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
/*
The Bluetooth LE Glove Listener is part of the VCoS NSF project, and is intended to listen for updates to a BLE characteristic.
This characteristic contains a byte array that indicates which motor(s) should vibrate to provide haptic feedback to the user.
The current interface is as follows:
[Motor (1 - 6 or 7). Each number corresponds to a motor, or 7 for all motors)]
[Intensity (0..100 percent)]
[Duration (1..100 percent of a second)]
These values are sent in a byte array in the following format:
[motor, intensity, duration]
Every time the characteristic is updated, it will overwrite the duration of the previous run, terminating it immediately.
*/
#include <Arduino.h>
#include <SPI.h>
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BLEGatt.h"
#include "BluefruitConfig.h"
// Play with these (INTENSITY_SCALE has minimum of 3)
#define INTENSITY_SCALE 70
#define INTENSITY_MIN 25
#define INTENSITY_MAX 100
// Pin Mapping for IST Glove: MOT0 - 3 MOT1 - 8 MOT2 - 12 MOT3 - 23
int motorpins[7] = {
0,13,12,10,6,5,3 }; // motor driver pins 0 through 5, ideally spaced 60 degrees apart
/* Pin Mapping for Dev Glove: MOT0 - 3 MOT1 - 8 MOT2 - 12 MOT4 - 19
int motorpins[6] = {
3,8,12,19,23,14 };
*/
// Create the bluefruit object
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
Adafruit_BLEGatt gatt(ble);
int32_t gattServiceId;
int32_t gattCharId;
uint8_t motor = 0;
uint8_t intensity = 0;
long duration = 0;
void setup(void) {
Serial.begin(115200);
for(int i = 0; i < 7; i++) {
pinMode(motorpins[i], OUTPUT);
}
digitalWriteAll(LOW);
if (!ble.begin(VERBOSE_MODE))
{
Serial.println("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?");
}
/* Disable command echo from Bluefruit */
ble.verbose(false);
ble.echo(false);
}
// Write to every motor
void digitalWriteAll(uint8_t state) {
for (int m = 1; m < 7; m++)
{
digitalWrite(motorpins[m], state);
}
}
void parsePacket(String packet) {
String motorBuffer = packet.substring(0, 2);
String intensityBuffer = packet.substring(3, 5);
String durationBuffer = packet.substring(6);
const char* motorStr = motorBuffer.c_str();
const char* intensityStr = intensityBuffer.c_str();
const char* durationStr = durationBuffer.c_str();
motor = constrain(strtoul(motorStr, NULL, 16), 0, 7);
intensity = constrain(strtoul(intensityStr, NULL, 16), 0, 100);
intensity = map(intensity, 0, 100, INTENSITY_MIN, INTENSITY_MAX);
duration = constrain(strtoul(durationStr, NULL, 16), 1, 100);
duration *= 10 * 1000;
}
bool checkCharacteristic() {
// Read the buffer
ble.println("AT+GATTCHAR=1");
ble.readline();
if (strcmp(ble.buffer, "OK") == 0) {
// no data
return;
}
// Parse our data
parsePacket(String(ble.buffer));
if (motor != 0) {
return true;
}
}
void runMotor() {
// Reset the characteristic
String command = "AT+GATTCHAR=1,0x000000";
ble.println(command);
Serial.print("Running motor: "); Serial.println(motor);
Serial.print(" Duration: "); Serial.println(duration);
Serial.print(" Intensity: "); Serial.println(intensity);
unsigned long start_time = micros();
// Calculate length of the duty cycle
word cycle_time = INTENSITY_SCALE * intensity;
// Make sure every motor is off when running a new motor
digitalWriteAll(LOW);
// Keep running until the full duration has passed
while(start_time + duration > micros()) {
// Pulse the motors based on our duty cycle
if(motor == 7) {
digitalWriteAll(HIGH);
}
else {
digitalWrite(motorpins[motor], HIGH);
}
delayMicroseconds(cycle_time);
// If there's another byte waiting, read it right away
if(checkCharacteristic()) {
break;
}
// Pulse the motors based on our duty cycle
if(motor == 7) {
digitalWriteAll(LOW);
}
else {
digitalWrite(motorpins[motor], LOW);
}
// This algorithm breaks up the delays into chunks to keep checking
// for the right time to stop the loop.
// The shorter our delay, the less we have to break up the delays
int iterations = map(intensity, INTENSITY_MIN, INTENSITY_MAX,
INTENSITY_MIN, INTENSITY_MAX * 2);
for(uint8_t i = 0; i < iterations; i++)
{
delayMicroseconds(((100 * INTENSITY_SCALE) - cycle_time) / iterations);
if(start_time + duration < micros()) {
break;
}
}
// If the above breaks, just use this:
// delayMicroseconds(((100 * INTENSITY_SCALE) - cycle_time));
}
}
void loop(void) {
motor = 0;
intensity = 0;
duration = 0;
checkCharacteristic();
// Check our motor characteristic to see if we need to run a motor
if (motor != 0) {
runMotor();
}
// Make sure every motor is off
digitalWriteAll(LOW);
}