-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTesting_Rig.ino
More file actions
276 lines (227 loc) · 7.83 KB
/
Testing_Rig.ino
File metadata and controls
276 lines (227 loc) · 7.83 KB
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
/*
Name: Testing_Rig.ino
Created: 20/03/2024 3:09:07 PM
Author: Elliot Winterbottom [ID: 10918169]
*/
#include "Wire.h"
#include <Adafruit_DRV2605.h>
Adafruit_DRV2605 drv; // driver board object.
#define TCAADDR 0x70 // I2C multiplexer Address
#define RTPMODE 5
const uint8_t MAXRTP = 120; // max value actually ~128
const uint8_t MIDRTP = 100;
const uint8_t MINRTP = 0;
const uint8_t UPPERMOTOR = 1; // motor connected to port 1 will always be placed on the upper portion of arm
const uint8_t LOWERMOTOR = 0; // motor connected to port 0 will always be placed on the lower portion of arm
const int SIZE_OF_TEST_ARRAY = 45;
const int TEST_ARRAY[SIZE_OF_TEST_ARRAY] = { 1 ,1 ,1 ,1 ,1, 1 ,1 ,1 ,1 ,1, 1 ,1 ,1 ,1 ,1, 2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,3 ,3 ,3 ,3 ,3,3 ,3 ,3 ,3 ,3,3 ,3 ,3 ,3 ,3 }; // 5 tests for each effect. This array is const as test procedure should remain unchanged
int randomised_test_array[SIZE_OF_TEST_ARRAY];
bool testType = 0; // 0 for phantom sensation, 1 for apparent motion
int testno = 0;
int inputSOA = 0;// value ranges from 0 to 1024
void tcaselect(uint8_t i) { // function selects address of component on multiplexer based on port number.
if (i > 7) return;
Wire.beginTransmission(TCAADDR); //the code from this function was taken from: https://learn.adafruit.com/adafruit-tca9548a-1-to-8-i2c-multiplexer-breakout/arduino-wiring-and-test
Wire.write(1 << i);
Wire.endTransmission();
}
void setmotor(uint8_t portNumber, bool ONOFF)
{
tcaselect(portNumber);
if (ONOFF == 1)
{
drv.setRealtimeValue(MIDRTP);
}
else if (ONOFF == 0)
{
drv.setRealtimeValue(MINRTP);
}
}
void setmotor_amplitude(uint8_t portNumber, uint8_t amplitude)
{
if (amplitude > 256 || amplitude < 0 ) // if value is greater than that which can be specified by the motor driver (actual max is 256 but thought a buffer was apropriate).
{
Serial.println("value set exceeds max please try again");
}
else
{
tcaselect(portNumber);
drv.setRealtimeValue(amplitude);
}
}
void phantom_sensation_playback(int motor_selector) // based on input will give a 3 second playback of motors: 0 - neither, 1 - upper, 2 - lower, 3 - both.
{
if (motor_selector == 0)
{
setmotor(UPPERMOTOR, 0);
setmotor(LOWERMOTOR, 0);
delay(3000);
}
else if (motor_selector == 1)
{
delay(100);
setmotor(UPPERMOTOR, 1);
setmotor(LOWERMOTOR, 0);
delay(3000);
setmotor(UPPERMOTOR, 0);
setmotor(LOWERMOTOR, 0);
}
else if (motor_selector == 2)
{
delay(100);
setmotor(UPPERMOTOR, 0);
setmotor(LOWERMOTOR, 1);
delay(3000);
setmotor(UPPERMOTOR, 0);
setmotor(LOWERMOTOR, 0);
}
else if (motor_selector == 3)
{
delay(100);
setmotor(UPPERMOTOR, 1);
setmotor(LOWERMOTOR, 1);
delay(3000);
setmotor(UPPERMOTOR, 0);
setmotor(LOWERMOTOR, 0);
}
}
void apparent_tactile_motion_playback(int duration, int SOA) // version using SOA theory
{
long long int playbackStart = millis();
long long int secondMotorStart = playbackStart + SOA; // first motor is UPPER, second motor is LOWER
long long int firstMotorEnd = playbackStart + duration;
long long int secondMotorEnd = secondMotorStart + duration;
//Serial.println("_______");
// Serial.println(playbackStart);
// Serial.println(secondMotorStart);
// Serial.println(firstMotorEnd);
// Serial.println(secondMotorEnd);
// Serial.println("_______");
if(SOA == 0)
{
setmotor(UPPERMOTOR, 1);
setmotor(LOWERMOTOR, 1);
delay(duration);
setmotor(UPPERMOTOR, 0);
setmotor(LOWERMOTOR, 0);
}
else {
while (1)
{
if (millis() <= firstMotorEnd)
{
setmotor(UPPERMOTOR, 1);
}
else if (millis() > firstMotorEnd)
{
setmotor(UPPERMOTOR, 0);
}
if (millis() <= secondMotorEnd && millis() >= secondMotorStart)
{
setmotor(LOWERMOTOR, 1);
}
else if (millis() > secondMotorEnd)
{
setmotor(LOWERMOTOR, 0);
return; // as soon as the second motor is done end the playback function
}
}
}
}
void test_array_scrambler()
{
for (int i = 0; i < SIZE_OF_TEST_ARRAY; i++) // pulling the values from const so that they can be shuffled
{
randomised_test_array[i] = TEST_ARRAY[i];
}
for (int i = 0; i < SIZE_OF_TEST_ARRAY; i++) // code for this function taken from https://forum.arduino.cc/t/shuffle-an-array-of-ints/333494/2
{
int n = random(0, SIZE_OF_TEST_ARRAY);
int temp = randomised_test_array[n];
randomised_test_array[n] = randomised_test_array[i];
randomised_test_array[i] = temp;
}
}
// the setup function runs once when you press reset or power the board
void setup()
{
pinMode(3, INPUT_PULLUP);
while (!Serial); // check if serial is working
Wire.begin(); // begin I2C comms
Serial.begin(115200);
//Serial.print("please enter the test type: 0 for phantom sensation or 1 for apparent tactile motion");
//testType = Serial.readString();
Serial.println("TCA-Scanner ready! ");
tcaselect(0); // select port to find driver board.
if (!drv.begin())// due to multiplexing, code treats driver boards like a single board.
{
Serial.println(" Driverboard boot up failed"); // displays error message if board is unvavialable
while (1);
}
for (uint8_t t = 0; t < 8; t++) { //the code from this function was taken from and modified: https://learn.adafruit.com/adafruit-tca9548a-1-to-8-i2c-multiplexer-breakout/arduino-wiring-and-test
tcaselect(t);
Serial.print("TCA Port #"); Serial.println(t);
for (uint8_t addr = 0; addr <= 127; addr++) {
if (addr == TCAADDR) continue; // skip over multiplexer address
Wire.beginTransmission(addr);
if (!Wire.endTransmission()) {
Serial.print("Found I2C 0x"); Serial.println(addr, HEX); // check for devices on each port
drv.setMode(RTPMODE); // if the device is a drv then set it's mode to RTP mode
drv.setRealtimeValue(00); // makes sure that motor is turned off
}
}
}
Serial.println("_______");
for (int i = 0; i < SIZE_OF_TEST_ARRAY; i++) // pulling the values from const so that they can be shuffled
{
Serial.println(TEST_ARRAY[i]);
}
Serial.println("_______");
}
// the loop function runs over and over again until power down or reset
void loop() {
testno++;
switch (int(testType))
{
case 0:
test_array_scrambler();
Serial.println(testno);
for (int i = 0; i < SIZE_OF_TEST_ARRAY; i++)
{
while (digitalRead(3) == HIGH)
{
delay(100);
}
phantom_sensation_playback(randomised_test_array[i]);
Serial.println(i);
}
for (int i = 0; i < SIZE_OF_TEST_ARRAY; i++)
{
switch (randomised_test_array[i])
{
case 1:
Serial.println("upper");
break;
case 2:
Serial.println("lower");
break;
case 3:
Serial.println("both");
break;
}
}
break;
case 1:
for (int i = 0; i < 26; i++)
{
while (digitalRead(3) == HIGH)
{
delay(100);
}
apparent_tactile_motion_playback(160, i*8);
Serial.println("SOA:");
Serial.println(i * 8);
}
break;
}
}