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:39:12 2013 +0000
Revision:
3:08207d3fcf3e
Parent:
2:e1c07ecec050
Child:
4:c82d5978d626
updated

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 3:08207d3fcf3e 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 3:08207d3fcf3e 76 // Potentiometer
ansond 1:d9d593d4ea39 77 AnalogIn pot_1(p19);
ansond 1:d9d593d4ea39 78
ansond 1:d9d593d4ea39 79 // keep track of mouse position
ansond 1:d9d593d4ea39 80 int current_x = 0;
ansond 1:d9d593d4ea39 81 int current_y = 0;
ansond 1:d9d593d4ea39 82
ansond 1:d9d593d4ea39 83 // Potentiometer filters
ansond 1:d9d593d4ea39 84 #include "filter.h"
ansond 1:d9d593d4ea39 85 medianFilter prefilter(13);
ansond 1:d9d593d4ea39 86 medianFilter postfilter(7);
ansond 1:d9d593d4ea39 87
ansond 1:d9d593d4ea39 88 // return radians for a given degree
ansond 1:d9d593d4ea39 89 float degrees_to_radians(float degrees) {
ansond 1:d9d593d4ea39 90 float radians = ((M_PI*degrees)/180.0);
ansond 1:d9d593d4ea39 91 return radians;
ansond 1:d9d593d4ea39 92 }
ansond 1:d9d593d4ea39 93
ansond 1:d9d593d4ea39 94 // return degrees for a given radian
ansond 1:d9d593d4ea39 95 float radians_to_degrees(float radians) {
ansond 1:d9d593d4ea39 96 float degrees = ((180*radians)/M_PI);
ansond 1:d9d593d4ea39 97 return degrees;
ansond 1:d9d593d4ea39 98 }
ansond 1:d9d593d4ea39 99
ansond 1:d9d593d4ea39 100 // get_potentiometer_value() reads the potentiometer, filters its value and remaps it to [0, 100.0]
ansond 1:d9d593d4ea39 101 float get_potentiometer_value(AnalogIn pot) {
ansond 1:d9d593d4ea39 102 float f = pot;
ansond 3:08207d3fcf3e 103 f = prefilter.process(f); // pre-filter
ansond 3:08207d3fcf3e 104 f = (f * 100); // remap: [ 0, 100]
ansond 3:08207d3fcf3e 105 return postfilter.process(f); // post-filter after remap
ansond 1:d9d593d4ea39 106 }
ansond 1:d9d593d4ea39 107
ansond 0:a2c33a8eded1 108 //
ansond 0:a2c33a8eded1 109 // DreamForce 2013 Challenge:
ansond 1:d9d593d4ea39 110 // potentiometer_value_to_degrees() takes the potentiometer value (val_pot) and
ansond 1:d9d593d4ea39 111 // maps it to an angle between [min_sling_angle, max_sling_angle] as defined in the tunables section
ansond 1:d9d593d4ea39 112 //
ansond 1:d9d593d4ea39 113 // NOTE: This function is INCOMPLETE. To complete it you should:
ansond 1:d9d593d4ea39 114 // 1). Uncomment the debug statement, run the program, and look at raw potentiometer values
ansond 1:d9d593d4ea39 115 // 2). Determine the min and max potentiometer values you wish to scale to degrees
ansond 1:d9d593d4ea39 116 // 3). Determine the 90 degree potentiometer value ("median_pot") that denotes the sling band at 90 to the sling body
ansond 1:d9d593d4ea39 117 // 4). Fill in median_pot, min_pot, max_pot below
ansond 1:d9d593d4ea39 118 // 5). Compile up and give it a try
ansond 1:d9d593d4ea39 119 //
ansond 1:d9d593d4ea39 120 float potentiometer_value_to_degrees(float val_pot) {
ansond 1:d9d593d4ea39 121 float deg = 0.0;
ansond 1:d9d593d4ea39 122 float accuracy = 0.1;
ansond 1:d9d593d4ea39 123
ansond 1:d9d593d4ea39 124 // DEBUG - may need this to calibrate pot values below
ansond 1:d9d593d4ea39 125 //std::printf("Raw pot value=%.1f\r\n",val_pot);
ansond 1:d9d593d4ea39 126
ansond 3:08207d3fcf3e 127 // Potentiometer range: INCOMPLETE: you need to uncomment the print statement above and determine median_pot, min_pot, max_pot
ansond 3:08207d3fcf3e 128 float median_pot = 0.0;
ansond 3:08207d3fcf3e 129 float min_pot = 0.0;
ansond 3:08207d3fcf3e 130 float max_pot = 0.0;
ansond 1:d9d593d4ea39 131 float incr_pot = (max_pot*10) - (min_pot*10); // how many .1 increments we have in the interval [min, max]
ansond 1:d9d593d4ea39 132
ansond 1:d9d593d4ea39 133 // Mapped degree range: approx [min_sling_angle, max_sling_angle] degrees so convert to
ansond 1:d9d593d4ea39 134 float min_deg = min_sling_angle;
ansond 1:d9d593d4ea39 135 float max_deg = max_sling_angle;
ansond 1:d9d593d4ea39 136 float incr_deg = (max_deg*10) - (min_deg*10); // how many .1 increments we have in the interval [min, max]
ansond 0:a2c33a8eded1 137
ansond 1:d9d593d4ea39 138 // see if we are centered or not
ansond 1:d9d593d4ea39 139 float centered_pot = fabs(val_pot - median_pot);
ansond 1:d9d593d4ea39 140
ansond 1:d9d593d4ea39 141 // 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 142 if (centered_pot > accuracy) {
ansond 1:d9d593d4ea39 143 // map to degree range
ansond 1:d9d593d4ea39 144 float conversion = (incr_deg/incr_pot);
ansond 1:d9d593d4ea39 145 deg = min_deg + (conversion*(val_pot - min_pot));
ansond 1:d9d593d4ea39 146 }
ansond 1:d9d593d4ea39 147
ansond 1:d9d593d4ea39 148 // we have to flip the sign of the result
ansond 1:d9d593d4ea39 149 deg = -deg;
ansond 0:a2c33a8eded1 150
ansond 1:d9d593d4ea39 151 // return the calculated degrees
ansond 1:d9d593d4ea39 152 return deg;
ansond 1:d9d593d4ea39 153 }
ansond 1:d9d593d4ea39 154
ansond 1:d9d593d4ea39 155 // adjust the final angle (theta) taking into account the relative angle between the sling body and the sling band.
ansond 1:d9d593d4ea39 156 float adjust_for_sling_angle(float slingshot_body_angle) {
ansond 3:08207d3fcf3e 157 // get the sling angle through approximation with the potentiometer
ansond 3:08207d3fcf3e 158 float sling_angle_degrees = potentiometer_value_to_degrees(get_potentiometer_value(pot_1));
ansond 1:d9d593d4ea39 159
ansond 1:d9d593d4ea39 160 // the sling angle is in degrees - so lets convert the body angle to degrees as well
ansond 1:d9d593d4ea39 161 float modified_angle_degrees = radians_to_degrees(slingshot_body_angle);
ansond 1:d9d593d4ea39 162
ansond 1:d9d593d4ea39 163 // we simply add the sling angle to adjust it
ansond 1:d9d593d4ea39 164 modified_angle_degrees += sling_angle_degrees;
ansond 1:d9d593d4ea39 165
ansond 1:d9d593d4ea39 166 // make sure that we are always between 0 and 359 degrees
ansond 1:d9d593d4ea39 167 while (modified_angle_degrees > 360.0) modified_angle_degrees = modified_angle_degrees - 360;
ansond 1:d9d593d4ea39 168 while (modified_angle_degrees < 0.0) modified_angle_degrees = modified_angle_degrees + 360;
ansond 1:d9d593d4ea39 169
ansond 1:d9d593d4ea39 170 // convert the modified angle back to radians
ansond 1:d9d593d4ea39 171 float modified_angle_radians = degrees_to_radians(modified_angle_degrees);
ansond 1:d9d593d4ea39 172
ansond 1:d9d593d4ea39 173 // DEBUG
ansond 1:d9d593d4ea39 174 //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 175
ansond 1:d9d593d4ea39 176 // return the modified angle
ansond 1:d9d593d4ea39 177 return modified_angle_radians;
ansond 0:a2c33a8eded1 178 }
ansond 0:a2c33a8eded1 179
ansond 0:a2c33a8eded1 180 // Return slingshot angle in radians, up > 0 > down
ansond 0:a2c33a8eded1 181 float get_angle() {
ansond 0:a2c33a8eded1 182 int readings[3];
ansond 0:a2c33a8eded1 183 accelerometer.getOutput(readings);
ansond 0:a2c33a8eded1 184 float x = (int16_t)readings[0];
ansond 0:a2c33a8eded1 185 float z = (int16_t)readings[2];
ansond 0:a2c33a8eded1 186 return atan(z / x);
ansond 0:a2c33a8eded1 187 }
ansond 0:a2c33a8eded1 188
ansond 0:a2c33a8eded1 189 // Return normalised stretch value based on bounds of all readings seen
ansond 0:a2c33a8eded1 190 float get_stretch() {
ansond 0:a2c33a8eded1 191 static float min_strength = 0.7;
ansond 0:a2c33a8eded1 192 static float max_strength = 0.7;
ansond 0:a2c33a8eded1 193 float current_strength = stretch_sensor.read();
ansond 0:a2c33a8eded1 194 if(current_strength > max_strength) { max_strength = current_strength; }
ansond 0:a2c33a8eded1 195 if(current_strength < min_strength) { min_strength = current_strength; }
ansond 0:a2c33a8eded1 196 float stretch = (current_strength - min_strength) / (max_strength - min_strength);
ansond 0:a2c33a8eded1 197 return 1.0 - stretch;
ansond 0:a2c33a8eded1 198 }
ansond 0:a2c33a8eded1 199
ansond 0:a2c33a8eded1 200 // move mouse to a location relative to the start point, stepping as needed
ansond 0:a2c33a8eded1 201 void move_mouse(int x, int y) {
ansond 0:a2c33a8eded1 202 const int STEP = 10;
ansond 0:a2c33a8eded1 203
ansond 0:a2c33a8eded1 204 int move_x = x - current_x;
ansond 0:a2c33a8eded1 205 int move_y = y - current_y;
ansond 0:a2c33a8eded1 206
ansond 0:a2c33a8eded1 207 // Move the mouse, in steps of max step size to ensure it is picked up by OS
ansond 0:a2c33a8eded1 208 while(move_x > STEP) { mouse.move(STEP, 0); move_x -= STEP; }
ansond 0:a2c33a8eded1 209 while(move_x < -STEP) { mouse.move(-STEP, 0); move_x += STEP; }
ansond 0:a2c33a8eded1 210 while(move_y > STEP) { mouse.move(0, STEP); move_y -= STEP; }
ansond 0:a2c33a8eded1 211 while(move_y < -STEP) { mouse.move(0, -STEP); move_y += STEP; }
ansond 1:d9d593d4ea39 212 mouse.move(move_x, move_y);
ansond 0:a2c33a8eded1 213
ansond 0:a2c33a8eded1 214 current_x = x;
ansond 0:a2c33a8eded1 215 current_y = y;
ansond 0:a2c33a8eded1 216 }
ansond 1:d9d593d4ea39 217
ansond 1:d9d593d4ea39 218 // reset the mouse position
ansond 1:d9d593d4ea39 219 void reset_mouse() {
ansond 1:d9d593d4ea39 220 current_x = 0;
ansond 1:d9d593d4ea39 221 current_y = 0;
ansond 1:d9d593d4ea39 222 mouse.move(0,0);
ansond 1:d9d593d4ea39 223 }
ansond 0:a2c33a8eded1 224
ansond 0:a2c33a8eded1 225 template <class T>
ansond 0:a2c33a8eded1 226 T filter(T* array, int len, T value) {
ansond 0:a2c33a8eded1 227 T mean = 0.0;
ansond 0:a2c33a8eded1 228 for(int i = 0; i<len - 1; i++) {
ansond 0:a2c33a8eded1 229 mean += array[i + 1];
ansond 0:a2c33a8eded1 230 array[i] = array[i + 1];
ansond 0:a2c33a8eded1 231 }
ansond 0:a2c33a8eded1 232 mean += value;
ansond 0:a2c33a8eded1 233 array[len - 1] = value;
ansond 0:a2c33a8eded1 234 return mean / (T)len;
ansond 0:a2c33a8eded1 235 }
ansond 0:a2c33a8eded1 236
ansond 0:a2c33a8eded1 237 typedef enum {
ansond 0:a2c33a8eded1 238 WAITING = 2,
ansond 0:a2c33a8eded1 239 AIMING = 4,
ansond 0:a2c33a8eded1 240 FIRING = 8
ansond 0:a2c33a8eded1 241 } state_t;
ansond 0:a2c33a8eded1 242
ansond 0:a2c33a8eded1 243 int main() {
ansond 1:d9d593d4ea39 244 bool loop_forever = true;
ansond 0:a2c33a8eded1 245 leds = 1;
ansond 1:d9d593d4ea39 246
ansond 1:d9d593d4ea39 247 // init mouse tracking
ansond 1:d9d593d4ea39 248 reset_mouse();
ansond 0:a2c33a8eded1 249
ansond 0:a2c33a8eded1 250 // setup accelerometer
ansond 0:a2c33a8eded1 251 accelerometer.setPowerControl(0x00);
ansond 0:a2c33a8eded1 252 accelerometer.setDataFormatControl(0x0B);
ansond 0:a2c33a8eded1 253 accelerometer.setDataRate(ADXL345_3200HZ);
ansond 0:a2c33a8eded1 254 accelerometer.setPowerControl(0x08);
ansond 0:a2c33a8eded1 255
ansond 0:a2c33a8eded1 256 state_t state = WAITING;
ansond 0:a2c33a8eded1 257 Timer timer;
ansond 0:a2c33a8eded1 258
ansond 0:a2c33a8eded1 259 float angles[8] = {0};
ansond 0:a2c33a8eded1 260 float stretches[8] = {0};
ansond 0:a2c33a8eded1 261
ansond 1:d9d593d4ea39 262 while(loop_forever) {
ansond 0:a2c33a8eded1 263 // get the slingshot parameters
ansond 0:a2c33a8eded1 264 float this_stretch = get_stretch();
ansond 0:a2c33a8eded1 265 float this_angle = get_angle();
ansond 0:a2c33a8eded1 266
ansond 0:a2c33a8eded1 267 // apply some filtering
ansond 0:a2c33a8eded1 268 float stretch = filter(stretches, 8, this_stretch);
ansond 0:a2c33a8eded1 269 float angle = filter(angles, 8, this_angle);
ansond 0:a2c33a8eded1 270
ansond 0:a2c33a8eded1 271 // DreamForce 2013 Challenge: Adjust the angle to account for the relative angle between the sling and the slingshot body
ansond 0:a2c33a8eded1 272 angle = adjust_for_sling_angle(angle);
ansond 0:a2c33a8eded1 273
ansond 0:a2c33a8eded1 274 leds = state;
ansond 1:d9d593d4ea39 275
ansond 0:a2c33a8eded1 276 // act based on the current state
ansond 0:a2c33a8eded1 277 switch (state) {
ansond 0:a2c33a8eded1 278 case WAITING:
ansond 1:d9d593d4ea39 279 if(stretch > stretch_start_threshold) { // significant stretch, considered starting
ansond 0:a2c33a8eded1 280 mouse.press(MOUSE_LEFT);
ansond 0:a2c33a8eded1 281 state = AIMING;
ansond 0:a2c33a8eded1 282 }
ansond 0:a2c33a8eded1 283 break;
ansond 0:a2c33a8eded1 284
ansond 0:a2c33a8eded1 285 case AIMING:
ansond 1:d9d593d4ea39 286 if(stretch - this_stretch > fire_threshold) { // rapid de-stretch, considered a fire
ansond 0:a2c33a8eded1 287 mouse.release(MOUSE_LEFT);
ansond 1:d9d593d4ea39 288 reset_mouse();
ansond 0:a2c33a8eded1 289 timer.start();
ansond 0:a2c33a8eded1 290 state = FIRING;
ansond 1:d9d593d4ea39 291 }
ansond 1:d9d593d4ea39 292 else if(stretch < stretch_start_threshold) { // de-stretch
ansond 1:d9d593d4ea39 293 reset_mouse();
ansond 1:d9d593d4ea39 294 timer.stop();
ansond 1:d9d593d4ea39 295 timer.reset();
ansond 1:d9d593d4ea39 296 state = WAITING;
ansond 0:a2c33a8eded1 297 } else {
ansond 1:d9d593d4ea39 298 int x = 0.0 - cos(angle) * stretch * mouse_scale;
ansond 1:d9d593d4ea39 299 int y = sin(angle) * stretch * mouse_scale;
ansond 0:a2c33a8eded1 300 move_mouse(x, y);
ansond 0:a2c33a8eded1 301 }
ansond 0:a2c33a8eded1 302 break;
ansond 0:a2c33a8eded1 303
ansond 0:a2c33a8eded1 304 case FIRING:
ansond 1:d9d593d4ea39 305 if(timer > fire_timing_threshold) {
ansond 0:a2c33a8eded1 306 timer.stop();
ansond 0:a2c33a8eded1 307 timer.reset();
ansond 1:d9d593d4ea39 308 reset_mouse();
ansond 0:a2c33a8eded1 309 state = WAITING;
ansond 0:a2c33a8eded1 310 }
ansond 0:a2c33a8eded1 311 break;
ansond 0:a2c33a8eded1 312 };
ansond 0:a2c33a8eded1 313
ansond 1:d9d593d4ea39 314 // wait for 100ms
ansond 1:d9d593d4ea39 315 wait_ms(100);
ansond 0:a2c33a8eded1 316 }
ansond 0:a2c33a8eded1 317 }