DreamForce 2013 Mini-Hack Challenge Project

Dependencies:   ADXL345 USBDevice filter mbed

Fork of df-minihack-slingshot by Doug Anson

Committer:
ansond
Date:
Fri Nov 01 17:29:59 2013 +0000
Revision:
2:e1c07ecec050
Parent:
1:d9d593d4ea39
Child:
3:08207d3fcf3e
updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:a2c33a8eded1 1 /* mbed USB Slingshot,
ansond 0:a2c33a8eded1 2 *
ansond 0:a2c33a8eded1 3 * Copyright (c) 2010-2011 mbed.org, MIT License
ansond 0:a2c33a8eded1 4 *
ansond 0:a2c33a8eded1 5 * smokrani, sford, danson, sgrove
ansond 0:a2c33a8eded1 6 *
ansond 0:a2c33a8eded1 7 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ansond 0:a2c33a8eded1 8 * and associated documentation files (the "Software"), to deal in the Software without
ansond 0:a2c33a8eded1 9 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
ansond 0:a2c33a8eded1 10 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
ansond 0:a2c33a8eded1 11 * Software is furnished to do so, subject to the following conditions:
ansond 0:a2c33a8eded1 12 *
ansond 0:a2c33a8eded1 13 * The above copyright notice and this permission notice shall be included in all copies or
ansond 0:a2c33a8eded1 14 * substantial portions of the Software.
ansond 0:a2c33a8eded1 15 *
ansond 0:a2c33a8eded1 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ansond 0:a2c33a8eded1 17 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ansond 0:a2c33a8eded1 18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ansond 0:a2c33a8eded1 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ansond 0:a2c33a8eded1 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ansond 0:a2c33a8eded1 21 */
ansond 0:a2c33a8eded1 22
ansond 1:d9d593d4ea39 23 //
ansond 1:d9d593d4ea39 24 // DreamForce 2013 Challenge:
ansond 1:d9d593d4ea39 25 //
ansond 2:e1c07ecec050 26 // Goal: modify the code below to adjust the sling body angle (theta) to take into account
ansond 1:d9d593d4ea39 27 // the relative angle between the sling body and the sling band.
ansond 1:d9d593d4ea39 28 //
ansond 1:d9d593d4ea39 29 //
ansond 1:d9d593d4ea39 30 // Mini-hack challenge: Your mission, should you choose to accept it, is to complete the function
ansond 1:d9d593d4ea39 31 // "potentiometer_value_to_degrees()" below (around line 129) to return a reasonable
ansond 1:d9d593d4ea39 32 // estimate of the sling band angle relative to the sling body
ansond 1:d9d593d4ea39 33 //
ansond 1:d9d593d4ea39 34 //
ansond 1:d9d593d4ea39 35
ansond 1:d9d593d4ea39 36 // DreamForce2013 TUNABLES START
ansond 1:d9d593d4ea39 37
ansond 1:d9d593d4ea39 38 // maximum angular value of the sling band
ansond 1:d9d593d4ea39 39 float max_sling_angle = 30.0;
ansond 1:d9d593d4ea39 40
ansond 1:d9d593d4ea39 41 // minimum angluar value of the sling band
ansond 1:d9d593d4ea39 42 float min_sling_angle = -30.0;
ansond 1:d9d593d4ea39 43
ansond 1:d9d593d4ea39 44 // DreamForce2013 TUNABLES END
ansond 1:d9d593d4ea39 45
ansond 1:d9d593d4ea39 46 // Sling Tunables Start - be careful changing these
ansond 1:d9d593d4ea39 47
ansond 1:d9d593d4ea39 48 // stretch start threshold
ansond 1:d9d593d4ea39 49 float stretch_start_threshold = 0.4;
ansond 1:d9d593d4ea39 50
ansond 1:d9d593d4ea39 51 // fire threshold
ansond 1:d9d593d4ea39 52 float fire_threshold = 0.15;
ansond 1:d9d593d4ea39 53
ansond 1:d9d593d4ea39 54 // fire timing threshold
ansond 1:d9d593d4ea39 55 float fire_timing_threshold = 3.0;
ansond 1:d9d593d4ea39 56
ansond 1:d9d593d4ea39 57 // scaling for mouse movement
ansond 1:d9d593d4ea39 58 int mouse_scale = 230;
ansond 1:d9d593d4ea39 59
ansond 1:d9d593d4ea39 60 // Sling Tunables End
ansond 1:d9d593d4ea39 61
ansond 1:d9d593d4ea39 62 // definition of PI
ansond 1:d9d593d4ea39 63 #define M_PI 3.14159
ansond 1:d9d593d4ea39 64
ansond 1:d9d593d4ea39 65 // Includes
ansond 0:a2c33a8eded1 66 #include "mbed.h"
ansond 0:a2c33a8eded1 67 #include "USBMouse.h"
ansond 0:a2c33a8eded1 68 #include "ADXL345.h"
ansond 0:a2c33a8eded1 69
ansond 0:a2c33a8eded1 70 // Physical interfaces
ansond 0:a2c33a8eded1 71 USBMouse mouse;
ansond 0:a2c33a8eded1 72 ADXL345 accelerometer(p5, p6, p7, p8);
ansond 0:a2c33a8eded1 73 AnalogIn stretch_sensor(p15);
ansond 0:a2c33a8eded1 74 BusOut leds(LED1, LED2, LED3, LED4);
ansond 0:a2c33a8eded1 75
ansond 1:d9d593d4ea39 76 // Potentiometers
ansond 1:d9d593d4ea39 77 AnalogIn pot_1(p19);
ansond 1:d9d593d4ea39 78 //AnalogIn pot_2(p20);
ansond 1:d9d593d4ea39 79
ansond 1:d9d593d4ea39 80 // keep track of mouse position
ansond 1:d9d593d4ea39 81 int current_x = 0;
ansond 1:d9d593d4ea39 82 int current_y = 0;
ansond 1:d9d593d4ea39 83
ansond 1:d9d593d4ea39 84 // Potentiometer filters
ansond 1:d9d593d4ea39 85 #include "filter.h"
ansond 1:d9d593d4ea39 86 medianFilter prefilter(13);
ansond 1:d9d593d4ea39 87 medianFilter postfilter(7);
ansond 1:d9d593d4ea39 88
ansond 1:d9d593d4ea39 89 // return radians for a given degree
ansond 1:d9d593d4ea39 90 float degrees_to_radians(float degrees) {
ansond 1:d9d593d4ea39 91 float radians = ((M_PI*degrees)/180.0);
ansond 1:d9d593d4ea39 92 return radians;
ansond 1:d9d593d4ea39 93 }
ansond 1:d9d593d4ea39 94
ansond 1:d9d593d4ea39 95 // return degrees for a given radian
ansond 1:d9d593d4ea39 96 float radians_to_degrees(float radians) {
ansond 1:d9d593d4ea39 97 float degrees = ((180*radians)/M_PI);
ansond 1:d9d593d4ea39 98 return degrees;
ansond 1:d9d593d4ea39 99 }
ansond 1:d9d593d4ea39 100
ansond 1:d9d593d4ea39 101 // return an average of two degrees
ansond 1:d9d593d4ea39 102 float compute_average(float deg1, float deg2) {
ansond 1:d9d593d4ea39 103 return (float)((deg1+deg2)/2);
ansond 1:d9d593d4ea39 104 }
ansond 1:d9d593d4ea39 105
ansond 1:d9d593d4ea39 106 //
ansond 1:d9d593d4ea39 107 // DreamForce 2013 Challenge:
ansond 1:d9d593d4ea39 108 // get_potentiometer_value() reads the potentiometer, filters its value and remaps it to [0, 100.0]
ansond 1:d9d593d4ea39 109 //
ansond 1:d9d593d4ea39 110 float get_potentiometer_value(AnalogIn pot) {
ansond 1:d9d593d4ea39 111 float f = pot;
ansond 1:d9d593d4ea39 112 f = prefilter.process(f);
ansond 1:d9d593d4ea39 113 f = (f * 100); // [ 0, 100]
ansond 1:d9d593d4ea39 114 return postfilter.process(f);
ansond 1:d9d593d4ea39 115 }
ansond 1:d9d593d4ea39 116
ansond 0:a2c33a8eded1 117 //
ansond 0:a2c33a8eded1 118 // DreamForce 2013 Challenge:
ansond 1:d9d593d4ea39 119 // potentiometer_value_to_degrees() takes the potentiometer value (val_pot) and
ansond 1:d9d593d4ea39 120 // maps it to an angle between [min_sling_angle, max_sling_angle] as defined in the tunables section
ansond 1:d9d593d4ea39 121 //
ansond 1:d9d593d4ea39 122 // NOTE: This function is INCOMPLETE. To complete it you should:
ansond 1:d9d593d4ea39 123 // 1). Uncomment the debug statement, run the program, and look at raw potentiometer values
ansond 1:d9d593d4ea39 124 // 2). Determine the min and max potentiometer values you wish to scale to degrees
ansond 1:d9d593d4ea39 125 // 3). Determine the 90 degree potentiometer value ("median_pot") that denotes the sling band at 90 to the sling body
ansond 1:d9d593d4ea39 126 // 4). Fill in median_pot, min_pot, max_pot below
ansond 1:d9d593d4ea39 127 // 5). Compile up and give it a try
ansond 1:d9d593d4ea39 128 //
ansond 1:d9d593d4ea39 129 float potentiometer_value_to_degrees(float val_pot) {
ansond 1:d9d593d4ea39 130 float deg = 0.0;
ansond 1:d9d593d4ea39 131 float accuracy = 0.1;
ansond 1:d9d593d4ea39 132
ansond 1:d9d593d4ea39 133 // DEBUG - may need this to calibrate pot values below
ansond 1:d9d593d4ea39 134 //std::printf("Raw pot value=%.1f\r\n",val_pot);
ansond 1:d9d593d4ea39 135
ansond 1:d9d593d4ea39 136 // Potentiometer range: you need to uncomment the debug statement above and determine the following values
ansond 1:d9d593d4ea39 137 float median_pot = 0;
ansond 1:d9d593d4ea39 138 float min_pot = 0;
ansond 1:d9d593d4ea39 139 float max_pot = 0;
ansond 1:d9d593d4ea39 140 float incr_pot = (max_pot*10) - (min_pot*10); // how many .1 increments we have in the interval [min, max]
ansond 1:d9d593d4ea39 141
ansond 1:d9d593d4ea39 142 // Mapped degree range: approx [min_sling_angle, max_sling_angle] degrees so convert to
ansond 1:d9d593d4ea39 143 float min_deg = min_sling_angle;
ansond 1:d9d593d4ea39 144 float max_deg = max_sling_angle;
ansond 1:d9d593d4ea39 145 float incr_deg = (max_deg*10) - (min_deg*10); // how many .1 increments we have in the interval [min, max]
ansond 0:a2c33a8eded1 146
ansond 1:d9d593d4ea39 147 // see if we are centered or not
ansond 1:d9d593d4ea39 148 float centered_pot = fabs(val_pot - median_pot);
ansond 1:d9d593d4ea39 149
ansond 1:d9d593d4ea39 150 // if we are off 90 degrees (i.e. sling body and sling band are not at 90 degrees) - calculate the relative angle
ansond 1:d9d593d4ea39 151 if (centered_pot > accuracy) {
ansond 1:d9d593d4ea39 152 // map to degree range
ansond 1:d9d593d4ea39 153 float conversion = (incr_deg/incr_pot);
ansond 1:d9d593d4ea39 154 deg = min_deg + (conversion*(val_pot - min_pot));
ansond 1:d9d593d4ea39 155 }
ansond 1:d9d593d4ea39 156
ansond 1:d9d593d4ea39 157 // we have to flip the sign of the result
ansond 1:d9d593d4ea39 158 deg = -deg;
ansond 0:a2c33a8eded1 159
ansond 1:d9d593d4ea39 160 // return the calculated degrees
ansond 1:d9d593d4ea39 161 return deg;
ansond 1:d9d593d4ea39 162 }
ansond 1:d9d593d4ea39 163
ansond 1:d9d593d4ea39 164 // adjust the final angle (theta) taking into account the relative angle between the sling body and the sling band.
ansond 1:d9d593d4ea39 165 float adjust_for_sling_angle(float slingshot_body_angle) {
ansond 1:d9d593d4ea39 166 // get the sling angle through approximation with the potentiometer, averaged from both potentiometers
ansond 1:d9d593d4ea39 167 float sling_angle_degrees = compute_average(potentiometer_value_to_degrees(get_potentiometer_value(pot_1)),potentiometer_value_to_degrees(get_potentiometer_value(pot_1)));
ansond 1:d9d593d4ea39 168
ansond 1:d9d593d4ea39 169 // the sling angle is in degrees - so lets convert the body angle to degrees as well
ansond 1:d9d593d4ea39 170 float modified_angle_degrees = radians_to_degrees(slingshot_body_angle);
ansond 1:d9d593d4ea39 171
ansond 1:d9d593d4ea39 172 // we simply add the sling angle to adjust it
ansond 1:d9d593d4ea39 173 modified_angle_degrees += sling_angle_degrees;
ansond 1:d9d593d4ea39 174
ansond 1:d9d593d4ea39 175 // make sure that we are always between 0 and 359 degrees
ansond 1:d9d593d4ea39 176 while (modified_angle_degrees > 360.0) modified_angle_degrees = modified_angle_degrees - 360;
ansond 1:d9d593d4ea39 177 while (modified_angle_degrees < 0.0) modified_angle_degrees = modified_angle_degrees + 360;
ansond 1:d9d593d4ea39 178
ansond 1:d9d593d4ea39 179 // convert the modified angle back to radians
ansond 1:d9d593d4ea39 180 float modified_angle_radians = degrees_to_radians(modified_angle_degrees);
ansond 1:d9d593d4ea39 181
ansond 1:d9d593d4ea39 182 // DEBUG
ansond 1:d9d593d4ea39 183 //std::printf("adjust_for_sling_angle: body_angle=%.1f sling_angle=%.1f modified_angle=%.1f\r\n",radians_to_degrees(slingshot_body_angle),sling_angle_degrees,modified_angle_degrees);
ansond 1:d9d593d4ea39 184
ansond 1:d9d593d4ea39 185 // return the modified angle
ansond 1:d9d593d4ea39 186 return modified_angle_radians;
ansond 0:a2c33a8eded1 187 }
ansond 0:a2c33a8eded1 188
ansond 0:a2c33a8eded1 189 // Return slingshot angle in radians, up > 0 > down
ansond 0:a2c33a8eded1 190 float get_angle() {
ansond 0:a2c33a8eded1 191 int readings[3];
ansond 0:a2c33a8eded1 192 accelerometer.getOutput(readings);
ansond 0:a2c33a8eded1 193 float x = (int16_t)readings[0];
ansond 0:a2c33a8eded1 194 float z = (int16_t)readings[2];
ansond 0:a2c33a8eded1 195 return atan(z / x);
ansond 0:a2c33a8eded1 196 }
ansond 0:a2c33a8eded1 197
ansond 0:a2c33a8eded1 198 // Return normalised stretch value based on bounds of all readings seen
ansond 0:a2c33a8eded1 199 float get_stretch() {
ansond 0:a2c33a8eded1 200 static float min_strength = 0.7;
ansond 0:a2c33a8eded1 201 static float max_strength = 0.7;
ansond 0:a2c33a8eded1 202 float current_strength = stretch_sensor.read();
ansond 0:a2c33a8eded1 203 if(current_strength > max_strength) { max_strength = current_strength; }
ansond 0:a2c33a8eded1 204 if(current_strength < min_strength) { min_strength = current_strength; }
ansond 0:a2c33a8eded1 205 float stretch = (current_strength - min_strength) / (max_strength - min_strength);
ansond 0:a2c33a8eded1 206 return 1.0 - stretch;
ansond 0:a2c33a8eded1 207 }
ansond 0:a2c33a8eded1 208
ansond 0:a2c33a8eded1 209 // move mouse to a location relative to the start point, stepping as needed
ansond 0:a2c33a8eded1 210 void move_mouse(int x, int y) {
ansond 0:a2c33a8eded1 211 const int STEP = 10;
ansond 0:a2c33a8eded1 212
ansond 0:a2c33a8eded1 213 int move_x = x - current_x;
ansond 0:a2c33a8eded1 214 int move_y = y - current_y;
ansond 0:a2c33a8eded1 215
ansond 0:a2c33a8eded1 216 // Move the mouse, in steps of max step size to ensure it is picked up by OS
ansond 0:a2c33a8eded1 217 while(move_x > STEP) { mouse.move(STEP, 0); move_x -= STEP; }
ansond 0:a2c33a8eded1 218 while(move_x < -STEP) { mouse.move(-STEP, 0); move_x += STEP; }
ansond 0:a2c33a8eded1 219 while(move_y > STEP) { mouse.move(0, STEP); move_y -= STEP; }
ansond 0:a2c33a8eded1 220 while(move_y < -STEP) { mouse.move(0, -STEP); move_y += STEP; }
ansond 1:d9d593d4ea39 221 mouse.move(move_x, move_y);
ansond 0:a2c33a8eded1 222
ansond 0:a2c33a8eded1 223 current_x = x;
ansond 0:a2c33a8eded1 224 current_y = y;
ansond 0:a2c33a8eded1 225 }
ansond 1:d9d593d4ea39 226
ansond 1:d9d593d4ea39 227 // reset the mouse position
ansond 1:d9d593d4ea39 228 void reset_mouse() {
ansond 1:d9d593d4ea39 229 current_x = 0;
ansond 1:d9d593d4ea39 230 current_y = 0;
ansond 1:d9d593d4ea39 231 mouse.move(0,0);
ansond 1:d9d593d4ea39 232 }
ansond 0:a2c33a8eded1 233
ansond 0:a2c33a8eded1 234 template <class T>
ansond 0:a2c33a8eded1 235 T filter(T* array, int len, T value) {
ansond 0:a2c33a8eded1 236 T mean = 0.0;
ansond 0:a2c33a8eded1 237 for(int i = 0; i<len - 1; i++) {
ansond 0:a2c33a8eded1 238 mean += array[i + 1];
ansond 0:a2c33a8eded1 239 array[i] = array[i + 1];
ansond 0:a2c33a8eded1 240 }
ansond 0:a2c33a8eded1 241 mean += value;
ansond 0:a2c33a8eded1 242 array[len - 1] = value;
ansond 0:a2c33a8eded1 243 return mean / (T)len;
ansond 0:a2c33a8eded1 244 }
ansond 0:a2c33a8eded1 245
ansond 0:a2c33a8eded1 246 typedef enum {
ansond 0:a2c33a8eded1 247 WAITING = 2,
ansond 0:a2c33a8eded1 248 AIMING = 4,
ansond 0:a2c33a8eded1 249 FIRING = 8
ansond 0:a2c33a8eded1 250 } state_t;
ansond 0:a2c33a8eded1 251
ansond 0:a2c33a8eded1 252 int main() {
ansond 1:d9d593d4ea39 253 bool loop_forever = true;
ansond 0:a2c33a8eded1 254 leds = 1;
ansond 1:d9d593d4ea39 255
ansond 1:d9d593d4ea39 256 // init mouse tracking
ansond 1:d9d593d4ea39 257 reset_mouse();
ansond 0:a2c33a8eded1 258
ansond 0:a2c33a8eded1 259 // setup accelerometer
ansond 0:a2c33a8eded1 260 accelerometer.setPowerControl(0x00);
ansond 0:a2c33a8eded1 261 accelerometer.setDataFormatControl(0x0B);
ansond 0:a2c33a8eded1 262 accelerometer.setDataRate(ADXL345_3200HZ);
ansond 0:a2c33a8eded1 263 accelerometer.setPowerControl(0x08);
ansond 0:a2c33a8eded1 264
ansond 0:a2c33a8eded1 265 state_t state = WAITING;
ansond 0:a2c33a8eded1 266 Timer timer;
ansond 0:a2c33a8eded1 267
ansond 0:a2c33a8eded1 268 float angles[8] = {0};
ansond 0:a2c33a8eded1 269 float stretches[8] = {0};
ansond 0:a2c33a8eded1 270
ansond 1:d9d593d4ea39 271 while(loop_forever) {
ansond 0:a2c33a8eded1 272 // get the slingshot parameters
ansond 0:a2c33a8eded1 273 float this_stretch = get_stretch();
ansond 0:a2c33a8eded1 274 float this_angle = get_angle();
ansond 0:a2c33a8eded1 275
ansond 0:a2c33a8eded1 276 // apply some filtering
ansond 0:a2c33a8eded1 277 float stretch = filter(stretches, 8, this_stretch);
ansond 0:a2c33a8eded1 278 float angle = filter(angles, 8, this_angle);
ansond 0:a2c33a8eded1 279
ansond 0:a2c33a8eded1 280 // DreamForce 2013 Challenge: Adjust the angle to account for the relative angle between the sling and the slingshot body
ansond 0:a2c33a8eded1 281 angle = adjust_for_sling_angle(angle);
ansond 0:a2c33a8eded1 282
ansond 0:a2c33a8eded1 283 leds = state;
ansond 1:d9d593d4ea39 284
ansond 0:a2c33a8eded1 285 // act based on the current state
ansond 0:a2c33a8eded1 286 switch (state) {
ansond 0:a2c33a8eded1 287 case WAITING:
ansond 1:d9d593d4ea39 288 if(stretch > stretch_start_threshold) { // significant stretch, considered starting
ansond 0:a2c33a8eded1 289 mouse.press(MOUSE_LEFT);
ansond 0:a2c33a8eded1 290 state = AIMING;
ansond 0:a2c33a8eded1 291 }
ansond 0:a2c33a8eded1 292 break;
ansond 0:a2c33a8eded1 293
ansond 0:a2c33a8eded1 294 case AIMING:
ansond 1:d9d593d4ea39 295 if(stretch - this_stretch > fire_threshold) { // rapid de-stretch, considered a fire
ansond 0:a2c33a8eded1 296 mouse.release(MOUSE_LEFT);
ansond 1:d9d593d4ea39 297 reset_mouse();
ansond 0:a2c33a8eded1 298 timer.start();
ansond 0:a2c33a8eded1 299 state = FIRING;
ansond 1:d9d593d4ea39 300 }
ansond 1:d9d593d4ea39 301 else if(stretch < stretch_start_threshold) { // de-stretch
ansond 1:d9d593d4ea39 302 reset_mouse();
ansond 1:d9d593d4ea39 303 timer.stop();
ansond 1:d9d593d4ea39 304 timer.reset();
ansond 1:d9d593d4ea39 305 state = WAITING;
ansond 0:a2c33a8eded1 306 } else {
ansond 1:d9d593d4ea39 307 int x = 0.0 - cos(angle) * stretch * mouse_scale;
ansond 1:d9d593d4ea39 308 int y = sin(angle) * stretch * mouse_scale;
ansond 0:a2c33a8eded1 309 move_mouse(x, y);
ansond 0:a2c33a8eded1 310 }
ansond 0:a2c33a8eded1 311 break;
ansond 0:a2c33a8eded1 312
ansond 0:a2c33a8eded1 313 case FIRING:
ansond 1:d9d593d4ea39 314 if(timer > fire_timing_threshold) {
ansond 0:a2c33a8eded1 315 timer.stop();
ansond 0:a2c33a8eded1 316 timer.reset();
ansond 1:d9d593d4ea39 317 reset_mouse();
ansond 0:a2c33a8eded1 318 state = WAITING;
ansond 0:a2c33a8eded1 319 }
ansond 0:a2c33a8eded1 320 break;
ansond 0:a2c33a8eded1 321 };
ansond 0:a2c33a8eded1 322
ansond 1:d9d593d4ea39 323 // wait for 100ms
ansond 1:d9d593d4ea39 324 wait_ms(100);
ansond 0:a2c33a8eded1 325 }
ansond 0:a2c33a8eded1 326 }