test 15/05/17

Dependencies:   C12832 mbed

Committer:
odu
Date:
Tue Jun 19 11:47:26 2018 +0000
Revision:
1:433aa8107296
Parent:
0:d35d9bdef147
for sharing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
odu 0:d35d9bdef147 1 #include "mbed.h"
odu 0:d35d9bdef147 2 #include "C12832.h"
odu 0:d35d9bdef147 3 #include "microteslameter.h"
odu 0:d35d9bdef147 4 #include "MLX90393.h"
odu 0:d35d9bdef147 5 #define FIELD_LIMIT 100
odu 0:d35d9bdef147 6
odu 0:d35d9bdef147 7 // Global variable:
odu 0:d35d9bdef147 8
odu 0:d35d9bdef147 9 PwmOut r (p23); //PWM output for the RBG led
odu 0:d35d9bdef147 10 PwmOut g (p24); //PWM output for the RBG led
odu 0:d35d9bdef147 11 PwmOut b (p25); //PWM output for the RBG led
odu 0:d35d9bdef147 12 AnalogIn p1(p19); // define analog input on p19 just for debug purpose
odu 0:d35d9bdef147 13 C12832 lcd(p5, p7, p6, p8, p11); //define pin for LCD screen
odu 0:d35d9bdef147 14 InterruptIn button(p14); // button used for zeroing - joystick
odu 0:d35d9bdef147 15 I2C i2c(p28, p27);
odu 1:433aa8107296 16 Serial pc(d+, d-);
odu 0:d35d9bdef147 17 Timeout to1; // timeer...
odu 0:d35d9bdef147 18 MLX90393 sensor(MLX90393::i2c_address,&i2c);
odu 0:d35d9bdef147 19
odu 0:d35d9bdef147 20 float CorrX,CorrY,CorrZ;
odu 0:d35d9bdef147 21 bool Led_on=false;
odu 0:d35d9bdef147 22 bool Blink_started=false;
odu 0:d35d9bdef147 23 bool CleanScreen=false;
odu 0:d35d9bdef147 24
odu 1:433aa8107296 25 USBSerial serial;
odu 0:d35d9bdef147 26
odu 0:d35d9bdef147 27 int main()
odu 0:d35d9bdef147 28 {
odu 0:d35d9bdef147 29 float X,Y,Z,modulus,T;
odu 0:d35d9bdef147 30 int content_buffer[63];
odu 0:d35d9bdef147 31 char read_buffer[11];
odu 0:d35d9bdef147 32
odu 1:433aa8107296 33 init();
odu 1:433aa8107296 34
odu 0:d35d9bdef147 35 lcd.cls(); // clean screen
odu 0:d35d9bdef147 36 button.rise(&startZeroing); //link button on the joystick with the start of zeroing
odu 0:d35d9bdef147 37
odu 0:d35d9bdef147 38 pc.printf("Microteslameter");
odu 1:433aa8107296 39 if(1)//activate deactivate memory dump
odu 0:d35d9bdef147 40 {
odu 0:d35d9bdef147 41 pc.printf("Memory dump at startup: \n");
odu 0:d35d9bdef147 42 for (int i = 0; i<63; i++) {
odu 0:d35d9bdef147 43 sensor.RR(read_buffer,i,0);
odu 0:d35d9bdef147 44 content_buffer[i] = (read_buffer[0]*65536)+(read_buffer[1]*256) + read_buffer[2];
odu 0:d35d9bdef147 45 }
odu 0:d35d9bdef147 46 for (int i = 0; i<63; i++) {
odu 0:d35d9bdef147 47 pc.printf("%i \n",content_buffer[i]);
odu 0:d35d9bdef147 48 }
odu 0:d35d9bdef147 49 }
odu 0:d35d9bdef147 50 //sensor.WR(read_buffer, 2, 2016, 0); //put DIF_FILT at max
odu 0:d35d9bdef147 51
odu 0:d35d9bdef147 52 while(1)
odu 0:d35d9bdef147 53 {
odu 0:d35d9bdef147 54 //X=p1.read()*200; //use potentiometer for debug
odu 0:d35d9bdef147 55 //Y=0;
odu 0:d35d9bdef147 56 //Z=0;
odu 0:d35d9bdef147 57
odu 0:d35d9bdef147 58 MeasureXYZT(&X,&Y,&Z,&T);
odu 0:d35d9bdef147 59 CorrectXYZ(&X,&Y,&Z); // correct for zeroing and convert LSB in uT
odu 0:d35d9bdef147 60 modulus=calculateModulus(X,Y,Z);
odu 0:d35d9bdef147 61 //--------------- interface----------------
odu 0:d35d9bdef147 62 setColor(normalize(modulus));
odu 0:d35d9bdef147 63 updateLCD(X,Y,Z,modulus);
odu 0:d35d9bdef147 64 pc.printf("T= %f,X= %f, Y= %f, Z= %f\n",T,X,Y,Z);
odu 0:d35d9bdef147 65 }
odu 0:d35d9bdef147 66 }
odu 0:d35d9bdef147 67
odu 0:d35d9bdef147 68
odu 0:d35d9bdef147 69 void init(void)
odu 0:d35d9bdef147 70 {
odu 0:d35d9bdef147 71 b=1; // RGB led off
odu 0:d35d9bdef147 72 r=1; // RGB led off
odu 0:d35d9bdef147 73 g=1; // RGB led off
odu 0:d35d9bdef147 74 i2c.frequency(7000);
odu 0:d35d9bdef147 75 }
odu 0:d35d9bdef147 76
odu 0:d35d9bdef147 77
odu 0:d35d9bdef147 78 void setColor(int intensity) // give intensity color from green 0% to red 100%
odu 0:d35d9bdef147 79 {
odu 0:d35d9bdef147 80 float duty_cycle;
odu 0:d35d9bdef147 81 if(intensity>=100) //if intensity if greater than 100 blink the led in red
odu 0:d35d9bdef147 82 {
odu 0:d35d9bdef147 83 if(Blink_started==false) to1.attach(&blink, 0.2); //attach timer to the blink function to start blinking the led, only if this was not done previously
odu 0:d35d9bdef147 84 }
odu 0:d35d9bdef147 85 else
odu 0:d35d9bdef147 86 {
odu 0:d35d9bdef147 87 to1.detach(); //detach timer to stop blinking
odu 0:d35d9bdef147 88 Blink_started=false;
odu 0:d35d9bdef147 89 }
odu 0:d35d9bdef147 90
odu 0:d35d9bdef147 91 duty_cycle=100-intensity;
odu 0:d35d9bdef147 92 duty_cycle=duty_cycle/100;
odu 0:d35d9bdef147 93 r=duty_cycle; //balance between red and green depending on intensity
odu 0:d35d9bdef147 94 g=1-duty_cycle;
odu 0:d35d9bdef147 95 }
odu 0:d35d9bdef147 96
odu 0:d35d9bdef147 97 void updateLCD(float X,float Y,float Z,float modulus)
odu 0:d35d9bdef147 98 {
odu 0:d35d9bdef147 99 if(CleanScreen==true) // clean screen in case the screen displayed something else
odu 0:d35d9bdef147 100 {
odu 0:d35d9bdef147 101 lcd.cls();
odu 0:d35d9bdef147 102 CleanScreen=false;
odu 0:d35d9bdef147 103 }
odu 0:d35d9bdef147 104 //lcd.setmode(XOR);
odu 0:d35d9bdef147 105 lcd.line(lcd.width()/2, 0, lcd.width()/2, lcd.height(), 1); // vertical line to separate field to norm...
odu 0:d35d9bdef147 106 lcd.locate(0,0);
odu 0:d35d9bdef147 107 lcd.printf("X= %7.1fuT",X);
odu 0:d35d9bdef147 108 lcd.locate(0,10);
odu 0:d35d9bdef147 109 lcd.printf("Y= %7.1fuT",Y);
odu 0:d35d9bdef147 110 lcd.locate(0,20);
odu 0:d35d9bdef147 111 lcd.printf("Z= %7.1fuT",Z);
odu 0:d35d9bdef147 112 lcd.locate((lcd.width()/2)+5,0);
odu 0:d35d9bdef147 113 lcd.printf("n= %7.1fuT",modulus);
odu 0:d35d9bdef147 114 if(modulus>FIELD_LIMIT)
odu 0:d35d9bdef147 115 {
odu 0:d35d9bdef147 116 lcd.locate((lcd.width()/2)+20,15);
odu 0:d35d9bdef147 117 lcd.printf("ALERT");
odu 0:d35d9bdef147 118 lcd.print_bm(bitmWarning,(lcd.width()/2)+2,15); // print warning sign
odu 0:d35d9bdef147 119 lcd.copy_to_lcd(); // update lcd
odu 0:d35d9bdef147 120 }
odu 0:d35d9bdef147 121 else
odu 0:d35d9bdef147 122 {
odu 0:d35d9bdef147 123 lcd.locate((lcd.width()/2)+2,15);
odu 0:d35d9bdef147 124 lcd.printf(" "); // clean the warning sign
odu 0:d35d9bdef147 125 }
odu 0:d35d9bdef147 126 wait(0.010); // needed?
odu 0:d35d9bdef147 127
odu 0:d35d9bdef147 128 }
odu 0:d35d9bdef147 129
odu 0:d35d9bdef147 130
odu 0:d35d9bdef147 131 float calculateModulus(float X, float Y, float Z)
odu 0:d35d9bdef147 132 {
odu 0:d35d9bdef147 133 float norm;
odu 0:d35d9bdef147 134 norm=X*X;
odu 0:d35d9bdef147 135 norm+=Y*Y;
odu 0:d35d9bdef147 136 norm+=Z*Z;
odu 0:d35d9bdef147 137 norm=sqrt(norm);
odu 0:d35d9bdef147 138 return norm;
odu 0:d35d9bdef147 139
odu 0:d35d9bdef147 140 }
odu 0:d35d9bdef147 141
odu 0:d35d9bdef147 142 float normalize(float field) //normalize the field versus the field limit to have something in percent of field limit
odu 0:d35d9bdef147 143 {
odu 0:d35d9bdef147 144 return (field/FIELD_LIMIT )*100;
odu 0:d35d9bdef147 145 }
odu 0:d35d9bdef147 146
odu 0:d35d9bdef147 147 void blink()
odu 0:d35d9bdef147 148 {
odu 0:d35d9bdef147 149 if(Led_on==false)
odu 0:d35d9bdef147 150 {
odu 0:d35d9bdef147 151 r=0;//led on in red
odu 0:d35d9bdef147 152 Led_on=true;
odu 0:d35d9bdef147 153 }
odu 0:d35d9bdef147 154 else
odu 0:d35d9bdef147 155 {
odu 0:d35d9bdef147 156 r=1; //led off
odu 0:d35d9bdef147 157 Led_on=false;
odu 0:d35d9bdef147 158 }
odu 0:d35d9bdef147 159 to1.attach(&blink, 0.2);
odu 0:d35d9bdef147 160 }
odu 0:d35d9bdef147 161
odu 0:d35d9bdef147 162 void MeasureXYZT(float *X,float *Y,float *Z, float *T)
odu 0:d35d9bdef147 163 {
odu 0:d35d9bdef147 164 char read_buffer[11];
odu 0:d35d9bdef147 165 sensor.SM(read_buffer, 15, 0); //start measurement
odu 0:d35d9bdef147 166 wait(0.2);// needed? according the AN worse case (big filter and low conversion time) the measurment take 198.5 ms
odu 0:d35d9bdef147 167 sensor.RM(read_buffer, 15, 0);//read measurement
odu 0:d35d9bdef147 168
odu 0:d35d9bdef147 169 // concatenate the two bytes
odu 0:d35d9bdef147 170 *T=read_buffer[1]*256+read_buffer[2];
odu 0:d35d9bdef147 171 *X=read_buffer[3]*256+read_buffer[4];
odu 0:d35d9bdef147 172 *Y=read_buffer[5]*256+read_buffer[6];
odu 0:d35d9bdef147 173 *Z=read_buffer[7]*256+read_buffer[8];
odu 0:d35d9bdef147 174
odu 0:d35d9bdef147 175 //deal with sign
odu 0:d35d9bdef147 176 if(*X>32768) *X-=65536;
odu 0:d35d9bdef147 177 if(*Y>32768) *Y-=65536;
odu 0:d35d9bdef147 178 if(*Z>32768) *Z-=65536;
odu 0:d35d9bdef147 179 }
odu 0:d35d9bdef147 180
odu 0:d35d9bdef147 181 void startZeroing()
odu 0:d35d9bdef147 182 {
odu 0:d35d9bdef147 183 float X,Y,Z,T;
odu 0:d35d9bdef147 184 int iteration=20;// to be defined
odu 0:d35d9bdef147 185 //char read_buffer[11];
odu 0:d35d9bdef147 186 CorrX=0;CorrY=0;CorrZ=0;
odu 0:d35d9bdef147 187 lcd.cls();
odu 0:d35d9bdef147 188 for(int i=0;i<iteration;i++)
odu 0:d35d9bdef147 189 {
odu 0:d35d9bdef147 190 MeasureXYZT(&X,&Y,&Z,&T);
odu 0:d35d9bdef147 191 CorrX+=X;
odu 0:d35d9bdef147 192 CorrY+=Y;
odu 0:d35d9bdef147 193 CorrZ+=Z;
odu 0:d35d9bdef147 194 lcd.locate(0,0);
odu 0:d35d9bdef147 195 lcd.printf("ZEROING progress: %i%%",i*100/iteration);
odu 0:d35d9bdef147 196 }
odu 0:d35d9bdef147 197 CorrX/=iteration;
odu 0:d35d9bdef147 198 CorrY/=iteration;
odu 0:d35d9bdef147 199 CorrZ/=iteration;
odu 0:d35d9bdef147 200 CleanScreen=true;
odu 0:d35d9bdef147 201 }
odu 0:d35d9bdef147 202
odu 0:d35d9bdef147 203
odu 0:d35d9bdef147 204
odu 0:d35d9bdef147 205 float getGainXY(int GAIN_SEL,int RES_XYZ)
odu 0:d35d9bdef147 206 {
odu 0:d35d9bdef147 207 return 2.576; //fix value for the moment... for RES_XYZ=3 and GAIN_SEL=4
odu 0:d35d9bdef147 208 }
odu 0:d35d9bdef147 209
odu 0:d35d9bdef147 210 float getGainZ(int GAIN_SEL,int RES_XYZ)
odu 0:d35d9bdef147 211 {
odu 0:d35d9bdef147 212 return 4.698; //fix value for the moment... for RES_XYZ=3 and GAIN_SEL=4
odu 0:d35d9bdef147 213 }
odu 0:d35d9bdef147 214
odu 0:d35d9bdef147 215 void CorrectXYZ(float *X,float *Y,float *Z) // correct for zeroing and convert LSB in uT
odu 0:d35d9bdef147 216 {
odu 0:d35d9bdef147 217 *X-=CorrX;
odu 0:d35d9bdef147 218 *Y-=CorrY;
odu 0:d35d9bdef147 219 *Z-=CorrZ;
odu 0:d35d9bdef147 220
odu 0:d35d9bdef147 221 *X= *X*getGainXY(0,0);
odu 0:d35d9bdef147 222 *Y= *Y*getGainXY(0,0);
odu 0:d35d9bdef147 223 *Z= *Z*getGainZ(0,0);
odu 0:d35d9bdef147 224 }