4180 Fish Tank Project
Dependencies: Motordriver NeoStrip mbed-rtos mbed
Fork of rtos_basic by
main.cpp@12:097b52548f0b, 2017-12-14 (annotated)
- Committer:
- 4180Team
- Date:
- Thu Dec 14 04:56:17 2017 +0000
- Revision:
- 12:097b52548f0b
- Parent:
- 11:0309bef74ba8
Final 4180 Project
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:491820ee784d | 1 | #include "mbed.h" |
mbed_official | 11:0309bef74ba8 | 2 | #include "rtos.h" |
4180Team | 12:097b52548f0b | 3 | #include <stdint.h> |
4180Team | 12:097b52548f0b | 4 | #include "NeoStrip.h" |
4180Team | 12:097b52548f0b | 5 | #include "gt.h" |
4180Team | 12:097b52548f0b | 6 | #include <iomanip> // setprecision |
4180Team | 12:097b52548f0b | 7 | #include <sstream> // stringstream |
4180Team | 12:097b52548f0b | 8 | #include "motordriver.h" |
4180Team | 12:097b52548f0b | 9 | #define N 64 |
4180Team | 12:097b52548f0b | 10 | #define PATTERNS 3 |
4180Team | 12:097b52548f0b | 11 | |
4180Team | 12:097b52548f0b | 12 | RawSerial pi(USBTX, USBRX); |
4180Team | 12:097b52548f0b | 13 | |
4180Team | 12:097b52548f0b | 14 | NeoStrip strip(p18, N); |
4180Team | 12:097b52548f0b | 15 | DigitalIn b1(p20); // brightness up |
4180Team | 12:097b52548f0b | 16 | DigitalIn b2(p19); // brightness down |
4180Team | 12:097b52548f0b | 17 | DigitalIn b3(p21); // next pattern |
4180Team | 12:097b52548f0b | 18 | |
4180Team | 12:097b52548f0b | 19 | Thread t1; |
4180Team | 12:097b52548f0b | 20 | Thread t2; |
4180Team | 12:097b52548f0b | 21 | Mutex ulcd_mutex; |
4180Team | 12:097b52548f0b | 22 | Motor A(p24, p23, p22, 1); // pwm, fwd, rev, can brake |
4180Team | 12:097b52548f0b | 23 | PwmOut led1(LED1); //indicates when mbed is ready |
4180Team | 12:097b52548f0b | 24 | PwmOut led2(LED2); |
4180Team | 12:097b52548f0b | 25 | PwmOut led4(LED4); |
4180Team | 12:097b52548f0b | 26 | AnalogIn photocell(p16); |
4180Team | 12:097b52548f0b | 27 | float volatile x = 0; //global variable to change motor speed |
4180Team | 12:097b52548f0b | 28 | bool test = false; |
4180Team | 12:097b52548f0b | 29 | int hueToRGB(float h); |
4180Team | 12:097b52548f0b | 30 | void pattern0(); |
4180Team | 12:097b52548f0b | 31 | void pattern1(); |
4180Team | 12:097b52548f0b | 32 | void pattern2(); |
4180Team | 12:097b52548f0b | 33 | // array of function pointers to the various patterns |
4180Team | 12:097b52548f0b | 34 | void (*patterns[])(void) = {&pattern0, &pattern1, &pattern2}; |
4180Team | 12:097b52548f0b | 35 | |
4180Team | 12:097b52548f0b | 36 | // |
4180Team | 12:097b52548f0b | 37 | void motor() { |
4180Team | 12:097b52548f0b | 38 | while(true) { |
4180Team | 12:097b52548f0b | 39 | led1 = 1; |
4180Team | 12:097b52548f0b | 40 | if (test) { |
4180Team | 12:097b52548f0b | 41 | x = .30; |
4180Team | 12:097b52548f0b | 42 | A.speed(x); |
4180Team | 12:097b52548f0b | 43 | Thread::wait(1200); |
4180Team | 12:097b52548f0b | 44 | test = false; |
4180Team | 12:097b52548f0b | 45 | A.speed(0); |
4180Team | 12:097b52548f0b | 46 | } |
4180Team | 12:097b52548f0b | 47 | } |
4180Team | 12:097b52548f0b | 48 | } |
4180Team | 12:097b52548f0b | 49 | double volatile bright = 0.05; |
4180Team | 12:097b52548f0b | 50 | bool test2 = false; |
4180Team | 12:097b52548f0b | 51 | float value = 0; |
4180Team | 12:097b52548f0b | 52 | void ambient() { |
4180Team | 12:097b52548f0b | 53 | led2 = 1; |
emilmont | 1:491820ee784d | 54 | while (true) { |
4180Team | 12:097b52548f0b | 55 | if (test2) { |
4180Team | 12:097b52548f0b | 56 | value = photocell; |
4180Team | 12:097b52548f0b | 57 | if (value < .10) value = .10; |
4180Team | 12:097b52548f0b | 58 | if (value > .3) value = .30; |
4180Team | 12:097b52548f0b | 59 | bright = abs(value - .10 - .20); |
4180Team | 12:097b52548f0b | 60 | if (bright > .25) bright = .25; |
4180Team | 12:097b52548f0b | 61 | strip.setBrightness(bright); |
4180Team | 12:097b52548f0b | 62 | } |
4180Team | 12:097b52548f0b | 63 | } |
4180Team | 12:097b52548f0b | 64 | } |
4180Team | 12:097b52548f0b | 65 | int pattern = 0; |
4180Team | 12:097b52548f0b | 66 | void dev_recv() |
4180Team | 12:097b52548f0b | 67 | { |
4180Team | 12:097b52548f0b | 68 | char temp = 0; |
4180Team | 12:097b52548f0b | 69 | while(pi.readable()) { |
4180Team | 12:097b52548f0b | 70 | temp = pi.getc(); |
4180Team | 12:097b52548f0b | 71 | pi.putc(temp); |
4180Team | 12:097b52548f0b | 72 | if (temp=='1') { |
4180Team | 12:097b52548f0b | 73 | bright = .15; |
4180Team | 12:097b52548f0b | 74 | strip.setBrightness(bright); |
4180Team | 12:097b52548f0b | 75 | } |
4180Team | 12:097b52548f0b | 76 | if (temp=='0' && bright > 0){ |
4180Team | 12:097b52548f0b | 77 | bright = 0; |
4180Team | 12:097b52548f0b | 78 | strip.setBrightness(bright); |
4180Team | 12:097b52548f0b | 79 | } |
4180Team | 12:097b52548f0b | 80 | if (temp=='=') test = true; // run motor |
4180Team | 12:097b52548f0b | 81 | if (temp=='a') test2 = true; // ambient mode on |
4180Team | 12:097b52548f0b | 82 | if (temp=='m') test2 = false; // manual mode on |
4180Team | 12:097b52548f0b | 83 | if (temp=='+') { |
4180Team | 12:097b52548f0b | 84 | bright = bright + .083; |
4180Team | 12:097b52548f0b | 85 | if (bright > .25) bright = .25; |
4180Team | 12:097b52548f0b | 86 | strip.setBrightness(bright); |
4180Team | 12:097b52548f0b | 87 | } |
4180Team | 12:097b52548f0b | 88 | if (temp=='-') { |
4180Team | 12:097b52548f0b | 89 | bright = bright - .083; |
4180Team | 12:097b52548f0b | 90 | if (bright < 0) bright = 0; |
4180Team | 12:097b52548f0b | 91 | strip.setBrightness(bright); |
4180Team | 12:097b52548f0b | 92 | } |
4180Team | 12:097b52548f0b | 93 | if (temp=='p') pattern = (pattern + 1) % 3; |
emilmont | 1:491820ee784d | 94 | } |
emilmont | 1:491820ee784d | 95 | } |
emilmont | 1:491820ee784d | 96 | int main() { |
4180Team | 12:097b52548f0b | 97 | pi.baud(9600); |
4180Team | 12:097b52548f0b | 98 | pi.attach(&dev_recv, Serial::RxIrq); |
4180Team | 12:097b52548f0b | 99 | b1.mode(PullUp); |
4180Team | 12:097b52548f0b | 100 | b2.mode(PullUp); |
4180Team | 12:097b52548f0b | 101 | b3.mode(PullUp); |
4180Team | 12:097b52548f0b | 102 | bool b3o = b3; // old copy of button 3 to poll for changes |
4180Team | 12:097b52548f0b | 103 | bright = .05; |
4180Team | 12:097b52548f0b | 104 | strip.setBrightness(bright); // set default brightness |
4180Team | 12:097b52548f0b | 105 | t1.start(ambient); |
4180Team | 12:097b52548f0b | 106 | t2.start(motor); |
emilmont | 1:491820ee784d | 107 | |
4180Team | 12:097b52548f0b | 108 | while (1) { |
4180Team | 12:097b52548f0b | 109 | //button 1 increases brightness |
4180Team | 12:097b52548f0b | 110 | if (b1 && bright < .25) |
4180Team | 12:097b52548f0b | 111 | { |
4180Team | 12:097b52548f0b | 112 | bright += 0.005; |
4180Team | 12:097b52548f0b | 113 | if (bright > .25) |
4180Team | 12:097b52548f0b | 114 | bright = .25; |
4180Team | 12:097b52548f0b | 115 | strip.setBrightness(bright); |
4180Team | 12:097b52548f0b | 116 | } |
4180Team | 12:097b52548f0b | 117 | |
4180Team | 12:097b52548f0b | 118 | // button 2 decreases brightness |
4180Team | 12:097b52548f0b | 119 | if (b2 && bright > 0) |
4180Team | 12:097b52548f0b | 120 | { |
4180Team | 12:097b52548f0b | 121 | bright -= 0.005; |
4180Team | 12:097b52548f0b | 122 | if (bright < 0) |
4180Team | 12:097b52548f0b | 123 | bright = 0; |
4180Team | 12:097b52548f0b | 124 | strip.setBrightness(bright); |
4180Team | 12:097b52548f0b | 125 | } |
4180Team | 12:097b52548f0b | 126 | |
4180Team | 12:097b52548f0b | 127 | // button 3 changes the pattern, only do stuff when its state has changed |
4180Team | 12:097b52548f0b | 128 | if (b3 != b3o) |
4180Team | 12:097b52548f0b | 129 | { |
4180Team | 12:097b52548f0b | 130 | if (b3 && ++pattern == PATTERNS) |
4180Team | 12:097b52548f0b | 131 | pattern = 0; |
4180Team | 12:097b52548f0b | 132 | b3o = b3; |
4180Team | 12:097b52548f0b | 133 | } |
4180Team | 12:097b52548f0b | 134 | // run the pattern update function which sets the strip's pixels |
4180Team | 12:097b52548f0b | 135 | patterns[pattern](); |
4180Team | 12:097b52548f0b | 136 | strip.write(); |
4180Team | 12:097b52548f0b | 137 | wait_ms(10); |
emilmont | 1:491820ee784d | 138 | } |
emilmont | 1:491820ee784d | 139 | } |
4180Team | 12:097b52548f0b | 140 | |
4180Team | 12:097b52548f0b | 141 | //// pattern0 displays a static image |
4180Team | 12:097b52548f0b | 142 | void pattern0() |
4180Team | 12:097b52548f0b | 143 | { |
4180Team | 12:097b52548f0b | 144 | strip.setPixels(0, N, gt_img); |
4180Team | 12:097b52548f0b | 145 | } |
4180Team | 12:097b52548f0b | 146 | |
4180Team | 12:097b52548f0b | 147 | // display a shifting rainbow, all colors have maximum |
4180Team | 12:097b52548f0b | 148 | // saturation and value, with evenly spaced hue |
4180Team | 12:097b52548f0b | 149 | void pattern1() |
4180Team | 12:097b52548f0b | 150 | { |
4180Team | 12:097b52548f0b | 151 | static float dh = 360.0 / N; |
4180Team | 12:097b52548f0b | 152 | static float x = 0; |
4180Team | 12:097b52548f0b | 153 | for (int i = 0; i < N; i++) |
4180Team | 12:097b52548f0b | 154 | strip.setPixel(i, hueToRGB((dh * i) - x)); |
4180Team | 12:097b52548f0b | 155 | x += 1; |
4180Team | 12:097b52548f0b | 156 | if (x > 360) |
4180Team | 12:097b52548f0b | 157 | x = 0; |
4180Team | 12:097b52548f0b | 158 | } |
4180Team | 12:097b52548f0b | 159 | // display a shifting gradient between red and blue |
4180Team | 12:097b52548f0b | 160 | void pattern2() |
4180Team | 12:097b52548f0b | 161 | { |
4180Team | 12:097b52548f0b | 162 | // offset for each pixel to allow the pattern to move |
4180Team | 12:097b52548f0b | 163 | static float x = 0; |
4180Team | 12:097b52548f0b | 164 | float r, b, y; |
4180Team | 12:097b52548f0b | 165 | for (int i = 0; i < N; i++) |
4180Team | 12:097b52548f0b | 166 | { |
4180Team | 12:097b52548f0b | 167 | // y is a scaled position between 0 (red) and 1.0 (blue) |
4180Team | 12:097b52548f0b | 168 | y = 1.0 * i / (N - 1) + x; |
4180Team | 12:097b52548f0b | 169 | if (y > 1) |
4180Team | 12:097b52548f0b | 170 | y -= 1; |
4180Team | 12:097b52548f0b | 171 | // if on the left half, red is decreasing and blue is increasng |
4180Team | 12:097b52548f0b | 172 | if (y < 0.5) |
4180Team | 12:097b52548f0b | 173 | { |
4180Team | 12:097b52548f0b | 174 | b = 2 * y; |
4180Team | 12:097b52548f0b | 175 | r = 1 - b; |
4180Team | 12:097b52548f0b | 176 | } |
4180Team | 12:097b52548f0b | 177 | // else red is increasing and blue is decreasing |
4180Team | 12:097b52548f0b | 178 | else |
4180Team | 12:097b52548f0b | 179 | { |
4180Team | 12:097b52548f0b | 180 | r = 2 * (y - 0.5); |
4180Team | 12:097b52548f0b | 181 | b = 1 - r; |
4180Team | 12:097b52548f0b | 182 | } |
4180Team | 12:097b52548f0b | 183 | // scale to integers and set the pixel |
4180Team | 12:097b52548f0b | 184 | strip.setPixel(i, (uint8_t)(r * 255), 0, (uint8_t)(b * 200)); |
4180Team | 12:097b52548f0b | 185 | } |
4180Team | 12:097b52548f0b | 186 | x += 0.003; |
4180Team | 12:097b52548f0b | 187 | if (x > 1) |
4180Team | 12:097b52548f0b | 188 | x = 0; |
4180Team | 12:097b52548f0b | 189 | } |
4180Team | 12:097b52548f0b | 190 | |
4180Team | 12:097b52548f0b | 191 | // Converts HSV to RGB with the given hue, assuming |
4180Team | 12:097b52548f0b | 192 | // maximum saturation and value |
4180Team | 12:097b52548f0b | 193 | int hueToRGB(float h) |
4180Team | 12:097b52548f0b | 194 | { |
4180Team | 12:097b52548f0b | 195 | // lots of floating point magic from the internet and scratching my head |
4180Team | 12:097b52548f0b | 196 | float r, g, b; |
4180Team | 12:097b52548f0b | 197 | if (h > 360) |
4180Team | 12:097b52548f0b | 198 | h -= 360; |
4180Team | 12:097b52548f0b | 199 | if (h < 0) |
4180Team | 12:097b52548f0b | 200 | h += 360; |
4180Team | 12:097b52548f0b | 201 | int i = (int)(h / 60.0); |
4180Team | 12:097b52548f0b | 202 | float f = (h / 60.0) - i; |
4180Team | 12:097b52548f0b | 203 | float q = 1 - f; |
4180Team | 12:097b52548f0b | 204 | switch (i % 6) |
4180Team | 12:097b52548f0b | 205 | { |
4180Team | 12:097b52548f0b | 206 | case 0: r = 1; g = f; b = 0; break; |
4180Team | 12:097b52548f0b | 207 | case 1: r = q; g = 1; b = 0; break; |
4180Team | 12:097b52548f0b | 208 | case 2: r = 0; g = 1; b = f; break; |
4180Team | 12:097b52548f0b | 209 | case 3: r = 0; g = q; b = 1; break; |
4180Team | 12:097b52548f0b | 210 | case 4: r = f; g = 0; b = 1; break; |
4180Team | 12:097b52548f0b | 211 | case 5: r = 1; g = 0; b = q; break; |
4180Team | 12:097b52548f0b | 212 | default: r = 0; g = 0; b = 0; break; |
4180Team | 12:097b52548f0b | 213 | } |
4180Team | 12:097b52548f0b | 214 | // scale to integers and return the packed value |
4180Team | 12:097b52548f0b | 215 | uint8_t R = (uint8_t)(r * 255); |
4180Team | 12:097b52548f0b | 216 | uint8_t G = (uint8_t)(g * 255); |
4180Team | 12:097b52548f0b | 217 | uint8_t B = (uint8_t)(b * 255); |
4180Team | 12:097b52548f0b | 218 | return (R << 16) | (G << 8) | B; |
4180Team | 12:097b52548f0b | 219 | } |
4180Team | 12:097b52548f0b | 220 | // test code, this demonstrates working motor drivers. |
4180Team | 12:097b52548f0b | 221 | // full reverse to full stop, dynamicaly brake and switch off. |
4180Team | 12:097b52548f0b | 222 |