MagneticLight - Modified version based on ST Components
Dependencies: PololuLedStrip X_NUCLEO_IKS01A1 mbed
main.cpp@4:b44bc6256182, 2017-02-07 (annotated)
- Committer:
- Arkadi
- Date:
- Tue Feb 07 14:48:30 2017 +0000
- Revision:
- 4:b44bc6256182
- Parent:
- 3:8cbf1e2122d4
Magnetic light Setup - Publish version 07/02/2017
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Arkadi | 0:2c66000e0791 | 1 | /* |
Arkadi | 0:2c66000e0791 | 2 | STM32 - Magnetic light - Updated version to work with ST components. (MCU , Magnetic sensor) |
Arkadi | 1:c54fc6087d0d | 3 | Arkadiraf@gmail.com - 07/02/2017 |
Arkadi | 1:c54fc6087d0d | 4 | |
Arkadi | 1:c54fc6087d0d | 5 | Based on my previous arduino version published at: |
Arkadi | 0:2c66000e0791 | 6 | */ |
Arkadi | 0:2c66000e0791 | 7 | |
Arkadi | 0:2c66000e0791 | 8 | /* |
Arkadi | 0:2c66000e0791 | 9 | Hardware setup: STM32 - Nucleo-F432KC |
Arkadi | 0:2c66000e0791 | 10 | Led strip 74 leds ws2812b 60 leds / meter |
Arkadi | 0:2c66000e0791 | 11 | 5V - 5V |
Arkadi | 0:2c66000e0791 | 12 | D11 - Led Din (through voltage converter 3v3->5V) |
Arkadi | 0:2c66000e0791 | 13 | GND - GND |
Arkadi | 0:2c66000e0791 | 14 | |
Arkadi | 1:c54fc6087d0d | 15 | Magnetic sensor LIS3MDL I2C : https://www.pololu.com/product/2737 |
Arkadi | 1:c54fc6087d0d | 16 | 3V3 - Vin |
Arkadi | 1:c54fc6087d0d | 17 | GND - GND |
Arkadi | 1:c54fc6087d0d | 18 | D5 - SCL |
Arkadi | 1:c54fc6087d0d | 19 | D4 - SDA |
Arkadi | 1:c54fc6087d0d | 20 | |
Arkadi | 0:2c66000e0791 | 21 | */ |
Arkadi | 0:2c66000e0791 | 22 | |
Arkadi | 0:2c66000e0791 | 23 | /////////////// |
Arkadi | 0:2c66000e0791 | 24 | // Libraries // |
Arkadi | 0:2c66000e0791 | 25 | /////////////// |
Arkadi | 0:2c66000e0791 | 26 | #include "mbed.h" |
Arkadi | 0:2c66000e0791 | 27 | #include "PololuLedStrip.h" |
Arkadi | 1:c54fc6087d0d | 28 | #include "x_nucleo_iks01a1.h" |
Arkadi | 0:2c66000e0791 | 29 | ////////////// |
Arkadi | 0:2c66000e0791 | 30 | // Defines: // |
Arkadi | 0:2c66000e0791 | 31 | ////////////// |
Arkadi | 0:2c66000e0791 | 32 | #define DEBBUG_MSG1 |
Arkadi | 0:2c66000e0791 | 33 | #define LED_COUNT 74 |
Arkadi | 0:2c66000e0791 | 34 | #define blackColor {0,0,0} |
Arkadi | 0:2c66000e0791 | 35 | #define SAMPLEDELAY 30 // roughly 30 hz // magnetic sensor sample rate / Led update |
Arkadi | 0:2c66000e0791 | 36 | //////////////////// |
Arkadi | 0:2c66000e0791 | 37 | // define Objects // |
Arkadi | 0:2c66000e0791 | 38 | //////////////////// |
Arkadi | 0:2c66000e0791 | 39 | PololuLedStrip ledStrip(D11); |
Arkadi | 0:2c66000e0791 | 40 | Timer timer; |
Arkadi | 0:2c66000e0791 | 41 | Serial pc(SERIAL_TX, SERIAL_RX,57600); |
Arkadi | 1:c54fc6087d0d | 42 | |
Arkadi | 1:c54fc6087d0d | 43 | // Magnetic sensor: |
Arkadi | 1:c54fc6087d0d | 44 | /* Instantiate the expansion board */ |
Arkadi | 1:c54fc6087d0d | 45 | static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D4, D5); |
Arkadi | 1:c54fc6087d0d | 46 | static MagneticSensor *magnetometer = mems_expansion_board->magnetometer; |
Arkadi | 1:c54fc6087d0d | 47 | |
Arkadi | 0:2c66000e0791 | 48 | /////////////// |
Arkadi | 0:2c66000e0791 | 49 | // variables // |
Arkadi | 0:2c66000e0791 | 50 | /////////////// |
Arkadi | 0:2c66000e0791 | 51 | |
Arkadi | 0:2c66000e0791 | 52 | // LED variables: |
Arkadi | 0:2c66000e0791 | 53 | rgb_color colors[LED_COUNT]; |
Arkadi | 0:2c66000e0791 | 54 | rgb_color setColor= {0,0,0}; |
Arkadi | 0:2c66000e0791 | 55 | |
Arkadi | 0:2c66000e0791 | 56 | // define array of pixel vectors based on the compas: |
Arkadi | 0:2c66000e0791 | 57 | float Pixel_Vect_Float[3]= {0,0,0}; // variable to store LED Power |
Arkadi | 0:2c66000e0791 | 58 | const int Pixel_Vect[LED_COUNT][3]= { |
Arkadi | 0:2c66000e0791 | 59 | { -41, -86, -31 }, |
Arkadi | 0:2c66000e0791 | 60 | { -54, -71, -45 }, |
Arkadi | 0:2c66000e0791 | 61 | { -66, -47, -58 }, |
Arkadi | 0:2c66000e0791 | 62 | { -75, -18, -63 }, |
Arkadi | 0:2c66000e0791 | 63 | { -76, 17, -63 }, |
Arkadi | 0:2c66000e0791 | 64 | { -68, 44, -58 }, |
Arkadi | 0:2c66000e0791 | 65 | { -56, 67, -48 }, |
Arkadi | 0:2c66000e0791 | 66 | { -41, 86, -32 }, |
Arkadi | 0:2c66000e0791 | 67 | { -35, 94, -2 }, |
Arkadi | 0:2c66000e0791 | 68 | { -61, 79, -4 }, |
Arkadi | 0:2c66000e0791 | 69 | { -82, 57, -4 }, |
Arkadi | 0:2c66000e0791 | 70 | { -99, 10, -10 }, |
Arkadi | 0:2c66000e0791 | 71 | { -97, -20, -6 }, |
Arkadi | 0:2c66000e0791 | 72 | { -87, -47, -6 }, |
Arkadi | 0:2c66000e0791 | 73 | { -68, -72, -7 }, |
Arkadi | 0:2c66000e0791 | 74 | { -24, -97, -3 }, |
Arkadi | 0:2c66000e0791 | 75 | { -50, -81, 30 }, |
Arkadi | 0:2c66000e0791 | 76 | { -66, -63, 41 }, |
Arkadi | 0:2c66000e0791 | 77 | { -80, -37, 49 }, |
Arkadi | 0:2c66000e0791 | 78 | { -85, 0, 53 }, |
Arkadi | 0:2c66000e0791 | 79 | { -80, 32, 51 }, |
Arkadi | 0:2c66000e0791 | 80 | { -71, 56, 42 }, |
Arkadi | 0:2c66000e0791 | 81 | { -50, 79, 33 }, |
Arkadi | 0:2c66000e0791 | 82 | { -30, 94, 19 }, |
Arkadi | 0:2c66000e0791 | 83 | { -20, 94, 29 }, |
Arkadi | 0:2c66000e0791 | 84 | { -25, 81, 54 }, |
Arkadi | 0:2c66000e0791 | 85 | { -34, 54, 78 }, |
Arkadi | 0:2c66000e0791 | 86 | { -36, 24, 90 }, |
Arkadi | 0:2c66000e0791 | 87 | { -36, -15, 92 }, |
Arkadi | 0:2c66000e0791 | 88 | { -34, -48, 81 }, |
Arkadi | 0:2c66000e0791 | 89 | { -25, -69, 68 }, |
Arkadi | 0:2c66000e0791 | 90 | { -23, -83, 54 }, |
Arkadi | 0:2c66000e0791 | 91 | { 22, -86, 46 }, |
Arkadi | 0:2c66000e0791 | 92 | { 35, -72, 60 }, |
Arkadi | 0:2c66000e0791 | 93 | { 44, -38, 80 }, |
Arkadi | 0:2c66000e0791 | 94 | { 53, -8, 86 }, |
Arkadi | 0:2c66000e0791 | 95 | { 45, 31, 83 }, |
Arkadi | 0:2c66000e0791 | 96 | { 35, 62, 70 }, |
Arkadi | 0:2c66000e0791 | 97 | { 24, 84, 50 }, |
Arkadi | 0:2c66000e0791 | 98 | { 10, 96, 24 }, |
Arkadi | 0:2c66000e0791 | 99 | { 42, 91, 12 }, |
Arkadi | 0:2c66000e0791 | 100 | { 68, 71, 19 }, |
Arkadi | 0:2c66000e0791 | 101 | { 85, 44, 24 }, |
Arkadi | 0:2c66000e0791 | 102 | { 95, 8, 28 }, |
Arkadi | 0:2c66000e0791 | 103 | { 91, -32, 28 }, |
Arkadi | 0:2c66000e0791 | 104 | { 80, -55, 25 }, |
Arkadi | 0:2c66000e0791 | 105 | { 62, -75, 20 }, |
Arkadi | 0:2c66000e0791 | 106 | { 50, -84, 21 }, |
Arkadi | 0:2c66000e0791 | 107 | { 48, -86, -19 }, |
Arkadi | 0:2c66000e0791 | 108 | { 67, -70, -25 }, |
Arkadi | 0:2c66000e0791 | 109 | { 83, -51, -26 }, |
Arkadi | 0:2c66000e0791 | 110 | { 93, -21, -31 }, |
Arkadi | 0:2c66000e0791 | 111 | { 94, 13, -30 }, |
Arkadi | 0:2c66000e0791 | 112 | { 85, 43, -29 }, |
Arkadi | 0:2c66000e0791 | 113 | { 67, 70, -25 }, |
Arkadi | 0:2c66000e0791 | 114 | { 42, 90, -16 }, |
Arkadi | 0:2c66000e0791 | 115 | { 19, 91, -35 }, |
Arkadi | 0:2c66000e0791 | 116 | { 38, 72, -58 }, |
Arkadi | 0:2c66000e0791 | 117 | { 48, 45, -74 }, |
Arkadi | 0:2c66000e0791 | 118 | { 55, 20, -81 }, |
Arkadi | 0:2c66000e0791 | 119 | { 55, -20, -81 }, |
Arkadi | 0:2c66000e0791 | 120 | { 47, -54, -70 }, |
Arkadi | 0:2c66000e0791 | 121 | { 31, -76, -56 }, |
Arkadi | 0:2c66000e0791 | 122 | { 20, -87, -43 }, |
Arkadi | 0:2c66000e0791 | 123 | { -10, -88, -45 }, |
Arkadi | 0:2c66000e0791 | 124 | { -14, -73, -67 }, |
Arkadi | 0:2c66000e0791 | 125 | { -13, -50, -85 }, |
Arkadi | 0:2c66000e0791 | 126 | { -17, -14, -98 }, |
Arkadi | 0:2c66000e0791 | 127 | { -12, 17, -98 }, |
Arkadi | 0:2c66000e0791 | 128 | { -14, 50, -86 }, |
Arkadi | 0:2c66000e0791 | 129 | { -13, 72, -68 }, |
Arkadi | 0:2c66000e0791 | 130 | { -9, 89, -47 }, |
Arkadi | 0:2c66000e0791 | 131 | { -12, 99, -10 }, |
Arkadi | 0:2c66000e0791 | 132 | { -12, 99, -10 } |
Arkadi | 0:2c66000e0791 | 133 | }; |
Arkadi | 0:2c66000e0791 | 134 | |
Arkadi | 0:2c66000e0791 | 135 | |
Arkadi | 0:2c66000e0791 | 136 | |
Arkadi | 0:2c66000e0791 | 137 | // Magnetic sensor: |
Arkadi | 1:c54fc6087d0d | 138 | int16_t axesRaw[3]; // raw data from magnetic sensor |
Arkadi | 0:2c66000e0791 | 139 | float Mag_raw[3]= {1,0,0}; //Magnetometer |
Arkadi | 0:2c66000e0791 | 140 | float Mag[3]= {0,0,0}; //Magnetometer |
Arkadi | 0:2c66000e0791 | 141 | float Mag_Bias[3]= {0,0,0}; //Magnetometer bias values |
Arkadi | 0:2c66000e0791 | 142 | |
Arkadi | 0:2c66000e0791 | 143 | float MagOut[3]= {0,0,0}; //Magnetometer |
Arkadi | 0:2c66000e0791 | 144 | float LMagOut[3]= {0,0,0}; //Last Magnetometer reading |
Arkadi | 0:2c66000e0791 | 145 | float MagIn[3]= {0,0,0}; //Magnetometer |
Arkadi | 0:2c66000e0791 | 146 | float LMagIn[3]= {0,0,0}; //Last Magnetometer reading |
Arkadi | 0:2c66000e0791 | 147 | float AHPF=0.99; |
Arkadi | 0:2c66000e0791 | 148 | |
Arkadi | 0:2c66000e0791 | 149 | float Mag_Norm[3]= {0,0,0}; //Magnetometer normalized |
Arkadi | 0:2c66000e0791 | 150 | float Mag_ABS=1; // Vector size indication how far the magnet is |
Arkadi | 0:2c66000e0791 | 151 | float CosAngle=0; // cosin of the angle between the two vectors |
Arkadi | 0:2c66000e0791 | 152 | float LedPower=0; // variable to store LED Power |
Arkadi | 0:2c66000e0791 | 153 | uint8_t LedPower_Byte=0; // Byte varible to set led power |
Arkadi | 0:2c66000e0791 | 154 | |
Arkadi | 0:2c66000e0791 | 155 | // timer variables |
Arkadi | 0:2c66000e0791 | 156 | int sampleMillis=0; |
Arkadi | 0:2c66000e0791 | 157 | int timeMillis=0; |
Arkadi | 0:2c66000e0791 | 158 | |
Arkadi | 0:2c66000e0791 | 159 | /////////////// |
Arkadi | 0:2c66000e0791 | 160 | // Functions // |
Arkadi | 0:2c66000e0791 | 161 | /////////////// |
Arkadi | 1:c54fc6087d0d | 162 | // HPF filter: |
Arkadi | 1:c54fc6087d0d | 163 | void HighPassFilter(); |
Arkadi | 1:c54fc6087d0d | 164 | // Update Pixel array based on mag sensor |
Arkadi | 1:c54fc6087d0d | 165 | void Update_Pixels_Mag(); |
Arkadi | 0:2c66000e0791 | 166 | // move dot throught the strip |
Arkadi | 0:2c66000e0791 | 167 | void dotMove(rgb_color dotColor); |
Arkadi | 0:2c66000e0791 | 168 | // update strip colors |
Arkadi | 0:2c66000e0791 | 169 | void colorStrip(); |
Arkadi | 0:2c66000e0791 | 170 | |
Arkadi | 0:2c66000e0791 | 171 | //////////////////////// |
Arkadi | 0:2c66000e0791 | 172 | // Main Code Setup : // |
Arkadi | 0:2c66000e0791 | 173 | //////////////////////// |
Arkadi | 0:2c66000e0791 | 174 | int main() |
Arkadi | 0:2c66000e0791 | 175 | { |
Arkadi | 0:2c66000e0791 | 176 | // init timer |
Arkadi | 0:2c66000e0791 | 177 | timer.start(); |
Arkadi | 0:2c66000e0791 | 178 | |
Arkadi | 1:c54fc6087d0d | 179 | // init magnetometer - Rewrite library or add options to change magnetic full scale and sample rate |
Arkadi | 1:c54fc6087d0d | 180 | // read magnetic sensor ID |
Arkadi | 1:c54fc6087d0d | 181 | uint8_t id; |
Arkadi | 1:c54fc6087d0d | 182 | magnetometer->ReadID(&id); |
Arkadi | 1:c54fc6087d0d | 183 | pc.printf("LIS3MDL magnetometer = 0x%X\r\n", id); |
Arkadi | 0:2c66000e0791 | 184 | |
Arkadi | 0:2c66000e0791 | 185 | /////////////////////// |
Arkadi | 0:2c66000e0791 | 186 | // Main Code Loop : // |
Arkadi | 0:2c66000e0791 | 187 | /////////////////////// |
Arkadi | 0:2c66000e0791 | 188 | while(1) { |
Arkadi | 0:2c66000e0791 | 189 | timeMillis=timer.read_ms(); |
Arkadi | 0:2c66000e0791 | 190 | // check timer overflow // returnes int. |
Arkadi | 0:2c66000e0791 | 191 | if (timeMillis<0) { |
Arkadi | 0:2c66000e0791 | 192 | timer.reset(); |
Arkadi | 0:2c66000e0791 | 193 | timeMillis=timer.read_ms(); |
Arkadi | 0:2c66000e0791 | 194 | // reset variables |
Arkadi | 0:2c66000e0791 | 195 | sampleMillis=0; |
Arkadi | 0:2c66000e0791 | 196 | } |
Arkadi | 0:2c66000e0791 | 197 | |
Arkadi | 0:2c66000e0791 | 198 | // update leds based on magnetometer |
Arkadi | 0:2c66000e0791 | 199 | if (timeMillis-sampleMillis>SAMPLEDELAY) { |
Arkadi | 0:2c66000e0791 | 200 | sampleMillis=timeMillis; |
Arkadi | 0:2c66000e0791 | 201 | |
Arkadi | 0:2c66000e0791 | 202 | // Read magnetometer values |
Arkadi | 1:c54fc6087d0d | 203 | magnetometer->Get_M_AxesRaw(axesRaw); |
Arkadi | 1:c54fc6087d0d | 204 | // debbug messages |
Arkadi | 1:c54fc6087d0d | 205 | //pc.printf("LIS3MDL [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axesRaw[0], axesRaw[1], axesRaw[2]); |
Arkadi | 1:c54fc6087d0d | 206 | |
Arkadi | 1:c54fc6087d0d | 207 | // update float ariables |
Arkadi | 1:c54fc6087d0d | 208 | // axis adjustment to fit the previous setup, (visual aligment). |
Arkadi | 3:8cbf1e2122d4 | 209 | Mag_raw[0]=-axesRaw[1]; |
Arkadi | 1:c54fc6087d0d | 210 | Mag_raw[1]=axesRaw[0]; |
Arkadi | 3:8cbf1e2122d4 | 211 | Mag_raw[2]=-axesRaw[2]; |
Arkadi | 0:2c66000e0791 | 212 | |
Arkadi | 1:c54fc6087d0d | 213 | // bias samples and scale |
Arkadi | 0:2c66000e0791 | 214 | Mag[0]=Mag_raw[0]-Mag_Bias[0]; |
Arkadi | 0:2c66000e0791 | 215 | Mag[1]=Mag_raw[1]-Mag_Bias[1]; |
Arkadi | 0:2c66000e0791 | 216 | Mag[2]=Mag_raw[2]-Mag_Bias[2]; |
Arkadi | 0:2c66000e0791 | 217 | |
Arkadi | 0:2c66000e0791 | 218 | Mag_ABS=sqrt(Mag[0]*Mag[0]+Mag[1]*Mag[1]+Mag[2]*Mag[2]); |
Arkadi | 0:2c66000e0791 | 219 | Mag_Norm[0]=Mag[0]/Mag_ABS; |
Arkadi | 0:2c66000e0791 | 220 | Mag_Norm[1]=Mag[1]/Mag_ABS; |
Arkadi | 0:2c66000e0791 | 221 | Mag_Norm[2]=Mag[2]/Mag_ABS; |
Arkadi | 0:2c66000e0791 | 222 | |
Arkadi | 1:c54fc6087d0d | 223 | // HPF filter: |
Arkadi | 1:c54fc6087d0d | 224 | HighPassFilter(); |
Arkadi | 0:2c66000e0791 | 225 | |
Arkadi | 1:c54fc6087d0d | 226 | // Update Pixel array based on mag sensor |
Arkadi | 1:c54fc6087d0d | 227 | Update_Pixels_Mag(); |
Arkadi | 1:c54fc6087d0d | 228 | |
Arkadi | 0:2c66000e0791 | 229 | // Send the colors to the LED strip. |
Arkadi | 0:2c66000e0791 | 230 | ledStrip.write(colors, LED_COUNT); |
Arkadi | 1:c54fc6087d0d | 231 | |
Arkadi | 0:2c66000e0791 | 232 | // debug messages: |
Arkadi | 1:c54fc6087d0d | 233 | //pc.printf("DBG_1: mill: %d , MAG: %.2f , %.2f , %.2f \r\n",sampleMillis,Mag[0],Mag[1],Mag[2]); |
Arkadi | 1:c54fc6087d0d | 234 | //pc.printf("DBG_1: mill: %d , MAG: %.2f , %.2f , %.2f \r\n",sampleMillis,Mag_Norm[0],Mag_Norm[1],Mag_Norm[2]); |
Arkadi | 0:2c66000e0791 | 235 | }// end update pixels based on mag |
Arkadi | 0:2c66000e0791 | 236 | |
Arkadi | 0:2c66000e0791 | 237 | |
Arkadi | 0:2c66000e0791 | 238 | |
Arkadi | 0:2c66000e0791 | 239 | // pixels test |
Arkadi | 0:2c66000e0791 | 240 | if (0) { |
Arkadi | 0:2c66000e0791 | 241 | setColor=(rgb_color) { |
Arkadi | 0:2c66000e0791 | 242 | 125,125,0 |
Arkadi | 0:2c66000e0791 | 243 | }; |
Arkadi | 0:2c66000e0791 | 244 | dotMove(setColor); |
Arkadi | 0:2c66000e0791 | 245 | |
Arkadi | 0:2c66000e0791 | 246 | // Send the colors to the LED strip. |
Arkadi | 0:2c66000e0791 | 247 | ledStrip.write(colors, LED_COUNT); |
Arkadi | 0:2c66000e0791 | 248 | wait_ms(10); |
Arkadi | 0:2c66000e0791 | 249 | } |
Arkadi | 0:2c66000e0791 | 250 | }// end main loop |
Arkadi | 0:2c66000e0791 | 251 | }// end main |
Arkadi | 0:2c66000e0791 | 252 | |
Arkadi | 0:2c66000e0791 | 253 | /////////////// |
Arkadi | 0:2c66000e0791 | 254 | // Functions // |
Arkadi | 0:2c66000e0791 | 255 | /////////////// |
Arkadi | 1:c54fc6087d0d | 256 | // HPF filter: |
Arkadi | 1:c54fc6087d0d | 257 | void HighPassFilter() |
Arkadi | 1:c54fc6087d0d | 258 | { |
Arkadi | 1:c54fc6087d0d | 259 | LMagIn[0]=MagIn[0]; |
Arkadi | 1:c54fc6087d0d | 260 | LMagIn[1]=MagIn[1]; |
Arkadi | 1:c54fc6087d0d | 261 | LMagIn[2]=MagIn[2]; |
Arkadi | 1:c54fc6087d0d | 262 | LMagOut[0]=MagOut[0]; |
Arkadi | 1:c54fc6087d0d | 263 | LMagOut[1]=MagOut[1]; |
Arkadi | 1:c54fc6087d0d | 264 | LMagOut[2]=MagOut[2]; |
Arkadi | 1:c54fc6087d0d | 265 | // update reading |
Arkadi | 1:c54fc6087d0d | 266 | MagIn[0]=Mag_raw[0]; |
Arkadi | 1:c54fc6087d0d | 267 | MagIn[1]=Mag_raw[1]; |
Arkadi | 1:c54fc6087d0d | 268 | MagIn[2]=Mag_raw[2]; |
Arkadi | 1:c54fc6087d0d | 269 | // update filter |
Arkadi | 1:c54fc6087d0d | 270 | MagOut[0]=AHPF*(LMagOut[0]+MagIn[0]-LMagIn[0]); |
Arkadi | 1:c54fc6087d0d | 271 | MagOut[1]=AHPF*(LMagOut[1]+MagIn[1]-LMagIn[1]); |
Arkadi | 1:c54fc6087d0d | 272 | MagOut[2]=AHPF*(LMagOut[2]+MagIn[2]-LMagIn[2]); |
Arkadi | 1:c54fc6087d0d | 273 | |
Arkadi | 1:c54fc6087d0d | 274 | // Normalize vector and calculate ABS value |
Arkadi | 1:c54fc6087d0d | 275 | Mag_ABS=sqrt(MagOut[0]*MagOut[0]+MagOut[1]*MagOut[1]+MagOut[2]*MagOut[2]); |
Arkadi | 1:c54fc6087d0d | 276 | Mag_Norm[0]=MagOut[0]/Mag_ABS; |
Arkadi | 1:c54fc6087d0d | 277 | Mag_Norm[1]=MagOut[1]/Mag_ABS; |
Arkadi | 1:c54fc6087d0d | 278 | Mag_Norm[2]=MagOut[2]/Mag_ABS; |
Arkadi | 1:c54fc6087d0d | 279 | }// end high pass filter |
Arkadi | 1:c54fc6087d0d | 280 | |
Arkadi | 1:c54fc6087d0d | 281 | // Update Pixel array based on mag sensor |
Arkadi | 1:c54fc6087d0d | 282 | void Update_Pixels_Mag() |
Arkadi | 1:c54fc6087d0d | 283 | { |
Arkadi | 1:c54fc6087d0d | 284 | // Calculate angle between magnetic vector and LED vectors |
Arkadi | 1:c54fc6087d0d | 285 | for (uint16_t ii=0 ; ii<LED_COUNT ; ii++) { |
Arkadi | 1:c54fc6087d0d | 286 | Pixel_Vect_Float[0]=((float)Pixel_Vect[ii][0])/100; |
Arkadi | 1:c54fc6087d0d | 287 | Pixel_Vect_Float[1]=((float)Pixel_Vect[ii][1])/100; |
Arkadi | 1:c54fc6087d0d | 288 | Pixel_Vect_Float[2]=((float)Pixel_Vect[ii][2])/100; |
Arkadi | 1:c54fc6087d0d | 289 | CosAngle=Mag_Norm[0]*Pixel_Vect_Float[0] + Mag_Norm[1]*Pixel_Vect_Float[1] + Mag_Norm[2]*Pixel_Vect_Float[2]; |
Arkadi | 1:c54fc6087d0d | 290 | //LedPower=Mag_ABS*CosAngle*CosAngle*CosAngle*CosAngle*CosAngle; |
Arkadi | 1:c54fc6087d0d | 291 | LedPower=Mag_ABS*((float)pow(CosAngle,9)); |
Arkadi | 1:c54fc6087d0d | 292 | if (LedPower>=0) { |
Arkadi | 1:c54fc6087d0d | 293 | if (LedPower>255) LedPower=255; |
Arkadi | 1:c54fc6087d0d | 294 | LedPower_Byte=(uint8_t)(LedPower); |
Arkadi | 1:c54fc6087d0d | 295 | colors[ii] = (rgb_color) { |
Arkadi | 1:c54fc6087d0d | 296 | LedPower_Byte, 0, 0 |
Arkadi | 1:c54fc6087d0d | 297 | }; |
Arkadi | 1:c54fc6087d0d | 298 | } |
Arkadi | 1:c54fc6087d0d | 299 | if (LedPower<0) { |
Arkadi | 1:c54fc6087d0d | 300 | if (LedPower<-255) LedPower=-255; |
Arkadi | 1:c54fc6087d0d | 301 | LedPower_Byte=(uint8_t)(-LedPower); |
Arkadi | 1:c54fc6087d0d | 302 | colors[ii] = (rgb_color) { |
Arkadi | 1:c54fc6087d0d | 303 | 0, 0, LedPower_Byte |
Arkadi | 1:c54fc6087d0d | 304 | }; |
Arkadi | 1:c54fc6087d0d | 305 | } |
Arkadi | 1:c54fc6087d0d | 306 | } |
Arkadi | 1:c54fc6087d0d | 307 | }// end pixel update based on magnetic field |
Arkadi | 1:c54fc6087d0d | 308 | |
Arkadi | 1:c54fc6087d0d | 309 | |
Arkadi | 0:2c66000e0791 | 310 | // move dot throught the strip |
Arkadi | 0:2c66000e0791 | 311 | void dotMove(rgb_color dotColor) |
Arkadi | 0:2c66000e0791 | 312 | { |
Arkadi | 0:2c66000e0791 | 313 | static int pixelNum=0; |
Arkadi | 0:2c66000e0791 | 314 | colors[pixelNum]=dotColor; |
Arkadi | 0:2c66000e0791 | 315 | if (pixelNum==0) { |
Arkadi | 0:2c66000e0791 | 316 | colors[LED_COUNT-1]=(rgb_color) blackColor; |
Arkadi | 0:2c66000e0791 | 317 | } else { |
Arkadi | 0:2c66000e0791 | 318 | colors[pixelNum-1]=(rgb_color) blackColor; |
Arkadi | 0:2c66000e0791 | 319 | } |
Arkadi | 0:2c66000e0791 | 320 | pixelNum++; |
Arkadi | 0:2c66000e0791 | 321 | pixelNum=pixelNum%LED_COUNT; |
Arkadi | 0:2c66000e0791 | 322 | } |
Arkadi | 0:2c66000e0791 | 323 | |
Arkadi | 0:2c66000e0791 | 324 | // update strip colors |
Arkadi | 0:2c66000e0791 | 325 | void colorStrip() |
Arkadi | 0:2c66000e0791 | 326 | { |
Arkadi | 0:2c66000e0791 | 327 | // Update the colors array. |
Arkadi | 0:2c66000e0791 | 328 | uint8_t time = timer.read_ms() >> 3; |
Arkadi | 0:2c66000e0791 | 329 | for(uint32_t i = 0; i < LED_COUNT; i++) { |
Arkadi | 0:2c66000e0791 | 330 | uint8_t x = (time - 8*i)%255; |
Arkadi | 0:2c66000e0791 | 331 | colors[i] = (rgb_color) { |
Arkadi | 0:2c66000e0791 | 332 | x, 255 - x, x |
Arkadi | 0:2c66000e0791 | 333 | }; |
Arkadi | 0:2c66000e0791 | 334 | } |
Arkadi | 0:2c66000e0791 | 335 | } |