v1

Dependencies:   mbed Servo

Committer:
eric11fr
Date:
Tue Oct 27 22:22:14 2020 +0000
Revision:
4:34a8e94c6fd5
Parent:
3:cb420000788e
test menu via xbee

Who changed what in which revision?

UserRevisionLine numberNew contents of line
eric11fr 0:b8bade04f24f 1 #include "mbed.h"
eric11fr 0:b8bade04f24f 2 #include "mpu9250.h"
eric11fr 0:b8bade04f24f 3 #include "SDFileSystem.h"
eric11fr 0:b8bade04f24f 4 #include "soft_uart.h"
eric11fr 0:b8bade04f24f 5 #include <string.h>
eric11fr 0:b8bade04f24f 6 #include "ArduCAM2640.h"
eric11fr 2:065d789dbcbd 7 #include "Servo/Servo.h"
eric11fr 0:b8bade04f24f 8 #define IMAGE_JPG_SIZE 16*1024
eric11fr 0:b8bade04f24f 9 #define MIN(x,y) x < y ? x : y
eric11fr 0:b8bade04f24f 10 float sum = 0;
eric11fr 0:b8bade04f24f 11 uint32_t sumCount = 0;
eric11fr 0:b8bade04f24f 12 char buffer[14];
eric11fr 0:b8bade04f24f 13 char buf[100];
eric11fr 0:b8bade04f24f 14 unsigned int framepos=0;
eric11fr 2:065d789dbcbd 15
eric11fr 2:065d789dbcbd 16 Servo servo_pan(PB_0);
eric11fr 2:065d789dbcbd 17 Servo servo_tilt(PB_1);
eric11fr 0:b8bade04f24f 18 ArduCAM2640 g_arducam;
eric11fr 0:b8bade04f24f 19 I2C camI2C(PB_7, PB_6);
eric11fr 0:b8bade04f24f 20 //I2C camI2C(PB_7,PB_6); //SDA/SCL
eric11fr 0:b8bade04f24f 21 SPI camSPI(PB_5,PB_4,PB_3);//MOSI/MISO/SCL
eric11fr 0:b8bade04f24f 22 DigitalOut cam_spi_cs(PA_8);// cs _camera PA_8
eric11fr 0:b8bade04f24f 23 DigitalOut sdcard_spi_cs(PA_11);// cs _sdcard PA_11
eric11fr 0:b8bade04f24f 24 MPU9250 mpu9250;
eric11fr 0:b8bade04f24f 25 Timer t;
eric11fr 0:b8bade04f24f 26 // serial port for USB, camera, GPS and XBEE
eric11fr 0:b8bade04f24f 27 Serial pc(USBTX, USBRX); // usb serial tx, rx
eric11fr 0:b8bade04f24f 28
eric11fr 0:b8bade04f24f 29 //Serial ser(pa3, pa4);//GPS softserial TX, RX
eric11fr 0:b8bade04f24f 30 Serial xbee(PA_9, PA_10); //xbee serial pin TX,RX
eric11fr 0:b8bade04f24f 31 //sdcard
eric11fr 0:b8bade04f24f 32 SDFileSystem sd(PB_5, PB_4, PB_3, PA_11, "sd"); //MOSI,MISO,SCK,CS the pinout on the mbed Cool Components workshop board
eric11fr 0:b8bade04f24f 33
eric11fr 0:b8bade04f24f 34 // activation 3.3V power supply on sensors board
eric11fr 2:065d789dbcbd 35 DigitalOut activ_sensors(PA_12); //
eric11fr 0:b8bade04f24f 36 // Analog pin sensors : battery and temperature
eric11fr 0:b8bade04f24f 37 AnalogIn batin(PA_0);//battery pin pA0
eric11fr 0:b8bade04f24f 38 AnalogIn tempin(PA_1);//temperature pin pa1
Giamarchi 3:cb420000788e 39 AnalogIn sp_1in(PA_6);//Solar Panel Current 1 pin PA6
Giamarchi 3:cb420000788e 40 AnalogIn sp_2in(PA_5);//Solar Panel Current 2 pin PA5
eric11fr 0:b8bade04f24f 41 uint8_t g_image_buffer[IMAGE_JPG_SIZE];
eric11fr 0:b8bade04f24f 42
eric11fr 4:34a8e94c6fd5 43 void get_image(void);
eric11fr 4:34a8e94c6fd5 44
eric11fr 0:b8bade04f24f 45 void init_serial(void)
eric11fr 0:b8bade04f24f 46 {
eric11fr 0:b8bade04f24f 47 pc.baud(9600);
eric11fr 0:b8bade04f24f 48 xbee.baud(38400);
eric11fr 0:b8bade04f24f 49 }
eric11fr 0:b8bade04f24f 50
eric11fr 0:b8bade04f24f 51 void Init_Inisat(void)
eric11fr 0:b8bade04f24f 52 {
eric11fr 0:b8bade04f24f 53 activ_sensors = 0; // 3.3V power sensors board Off
eric11fr 0:b8bade04f24f 54
eric11fr 0:b8bade04f24f 55 }
eric11fr 0:b8bade04f24f 56
eric11fr 0:b8bade04f24f 57 void test_imu(void)
eric11fr 0:b8bade04f24f 58 {
eric11fr 0:b8bade04f24f 59 unsigned char stop;
eric11fr 4:34a8e94c6fd5 60 //test mpu9250-IMU
eric11fr 0:b8bade04f24f 61 pc.printf("\r\n/*----------------------------------------------*/\r\n");
eric11fr 0:b8bade04f24f 62 pc.printf("\t IMU9250 test\r\n");
eric11fr 0:b8bade04f24f 63 pc.printf("send \"s\" to stop\r\n");
eric11fr 0:b8bade04f24f 64 pc.printf("/*----------------------------------------------*/\r\n");
eric11fr 4:34a8e94c6fd5 65 /*--------------------------------------------------------------*/
eric11fr 4:34a8e94c6fd5 66 xbee.printf("\r\n/*----------------------------------------------*/\r\n");
eric11fr 4:34a8e94c6fd5 67 xbee.printf("\t IMU9250 test\r\n");
eric11fr 4:34a8e94c6fd5 68 xbee.printf("send \"s\" to stop\r\n");
eric11fr 4:34a8e94c6fd5 69 xbee.printf("/*----------------------------------------------*/\r\n");
eric11fr 0:b8bade04f24f 70 activ_sensors = 1; // 3.3V power sensors board On
eric11fr 0:b8bade04f24f 71 wait(2);
eric11fr 0:b8bade04f24f 72
eric11fr 0:b8bade04f24f 73 //Set up I2C
eric11fr 0:b8bade04f24f 74 i2c.frequency(400000); // use fast (400 kHz) I2C
eric11fr 0:b8bade04f24f 75 t.start();
eric11fr 0:b8bade04f24f 76 // Read the WHO_AM_I register, this is a good test of communication
eric11fr 0:b8bade04f24f 77 uint8_t whoami = mpu9250.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); // Read WHO_AM_I register for MPU-9250
eric11fr 4:34a8e94c6fd5 78 pc.printf("I AM 0x%x\n\r", whoami);
eric11fr 4:34a8e94c6fd5 79 pc.printf("I SHOULD BE 0x71\n\r");
eric11fr 4:34a8e94c6fd5 80 xbee.printf("I AM 0x%x\n\r", whoami);
eric11fr 4:34a8e94c6fd5 81 xbee.printf("I SHOULD BE 0x71\n\r");
eric11fr 0:b8bade04f24f 82 if (whoami == 0x71) // WHO_AM_I should always be 0x68
eric11fr 0:b8bade04f24f 83 {
eric11fr 0:b8bade04f24f 84 pc.printf("MPU9250 WHO_AM_I is 0x%x\n\r", whoami);
eric11fr 0:b8bade04f24f 85 pc.printf("MPU9250 is online...\n\r");
eric11fr 4:34a8e94c6fd5 86 xbee.printf("MPU9250 WHO_AM_I is 0x%x\n\r", whoami);
eric11fr 4:34a8e94c6fd5 87 xbee.printf("MPU9250 is online...\n\r");
eric11fr 0:b8bade04f24f 88 sprintf(buffer, "0x%x", whoami);
eric11fr 0:b8bade04f24f 89 wait(1);
eric11fr 0:b8bade04f24f 90 mpu9250.resetMPU9250(); // Reset registers to default in preparation for device calibration
eric11fr 0:b8bade04f24f 91 // mpu9250.MPU9250SelfTest(SelfTest); // Start by performing self test and reporting values
eric11fr 0:b8bade04f24f 92 mpu9250.calibrateMPU9250(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers
eric11fr 0:b8bade04f24f 93 wait(2);
eric11fr 0:b8bade04f24f 94 mpu9250.initMPU9250();
eric11fr 4:34a8e94c6fd5 95 // pc.printf("MPU9250 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature
eric11fr 0:b8bade04f24f 96 mpu9250.initAK8963(magCalibration);
eric11fr 4:34a8e94c6fd5 97 // pc.printf("AK8963 initialized for active data mode....\n\r"); // Initialize device for active mode read of magnetometer
eric11fr 4:34a8e94c6fd5 98 // pc.printf("Accelerometer full-scale range = %f g\n\r", 2.0f*(float)(1<<Ascale));
eric11fr 4:34a8e94c6fd5 99 // pc.printf("Gyroscope full-scale range = %f deg/s\n\r", 250.0f*(float)(1<<Gscale));
eric11fr 4:34a8e94c6fd5 100 // if(Mscale == 0) pc.printf("Magnetometer resolution = 14 bits\n\r");
eric11fr 4:34a8e94c6fd5 101 // if(Mscale == 1) pc.printf("Magnetometer resolution = 16 bits\n\r");
eric11fr 4:34a8e94c6fd5 102 // if(Mmode == 2) pc.printf("Magnetometer ODR = 8 Hz\n\r");
eric11fr 4:34a8e94c6fd5 103 // if(Mmode == 6) pc.printf("Magnetometer ODR = 100 Hz\n\r");
eric11fr 0:b8bade04f24f 104 wait(1);
eric11fr 0:b8bade04f24f 105 }
eric11fr 0:b8bade04f24f 106 else
eric11fr 0:b8bade04f24f 107 {
eric11fr 0:b8bade04f24f 108 pc.printf("Could not connect to MPU9250: \n\r");
eric11fr 0:b8bade04f24f 109 pc.printf("%#x \n", whoami);
eric11fr 4:34a8e94c6fd5 110 xbee.printf("Could not connect to MPU9250: \n\r");
eric11fr 4:34a8e94c6fd5 111 xbee.printf("%#x \n", whoami);
eric11fr 0:b8bade04f24f 112 sprintf(buffer, "WHO_AM_I 0x%x", whoami);
eric11fr 4:34a8e94c6fd5 113 exit(0);
eric11fr 0:b8bade04f24f 114 }
eric11fr 0:b8bade04f24f 115 mpu9250.getAres(); // Get accelerometer sensitivity
eric11fr 0:b8bade04f24f 116 mpu9250.getGres(); // Get gyro sensitivity
eric11fr 0:b8bade04f24f 117 mpu9250.getMres(); // Get magnetometer sensitivity
eric11fr 4:34a8e94c6fd5 118 // pc.printf("Accelerometer sensitivity is %f LSB/g \n\r", 1.0f/aRes);
eric11fr 4:34a8e94c6fd5 119 // pc.printf("Gyroscope sensitivity is %f LSB/deg/s \n\r", 1.0f/gRes);
eric11fr 4:34a8e94c6fd5 120 // pc.printf("Magnetometer sensitivity is %f LSB/G \n\r", 1.0f/mRes);
eric11fr 0:b8bade04f24f 121 magbias[0] = +470.; // User environmental x-axis correction in milliGauss, should be automatically calculated
eric11fr 0:b8bade04f24f 122 magbias[1] = +120.; // User environmental x-axis correction in milliGauss
eric11fr 0:b8bade04f24f 123 magbias[2] = +125.; // User environmental x-axis correction in milliGauss
eric11fr 0:b8bade04f24f 124 do
eric11fr 0:b8bade04f24f 125 {
eric11fr 0:b8bade04f24f 126 // If intPin goes high, all data registers have new data
eric11fr 0:b8bade04f24f 127 if(mpu9250.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01)
eric11fr 0:b8bade04f24f 128 { // On interrupt, check if data ready interrupt
eric11fr 0:b8bade04f24f 129 mpu9250.readAccelData(accelCount); // Read the x/y/z adc values
eric11fr 0:b8bade04f24f 130 // Now we'll calculate the accleration value into actual g's
eric11fr 0:b8bade04f24f 131 ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set
eric11fr 0:b8bade04f24f 132 ay = (float)accelCount[1]*aRes - accelBias[1];
eric11fr 0:b8bade04f24f 133 az = (float)accelCount[2]*aRes - accelBias[2];
eric11fr 0:b8bade04f24f 134 mpu9250.readGyroData(gyroCount); // Read the x/y/z adc values
eric11fr 0:b8bade04f24f 135 // Calculate the gyro value into actual degrees per second
eric11fr 0:b8bade04f24f 136 gx = (float)gyroCount[0]*gRes - gyroBias[0]; // get actual gyro value, this depends on scale being set
eric11fr 0:b8bade04f24f 137 gy = (float)gyroCount[1]*gRes - gyroBias[1];
eric11fr 0:b8bade04f24f 138 gz = (float)gyroCount[2]*gRes - gyroBias[2];
eric11fr 0:b8bade04f24f 139 mpu9250.readMagData(magCount); // Read the x/y/z adc values
eric11fr 0:b8bade04f24f 140 // Calculate the magnetometer values in milliGauss
eric11fr 0:b8bade04f24f 141 // Include factory calibration per data sheet and user environmental corrections
eric11fr 0:b8bade04f24f 142 mx = (float)magCount[0]*mRes*magCalibration[0] - magbias[0]; // get actual magnetometer value, this depends on scale being set
eric11fr 0:b8bade04f24f 143 my = (float)magCount[1]*mRes*magCalibration[1] - magbias[1];
eric11fr 0:b8bade04f24f 144 mz = (float)magCount[2]*mRes*magCalibration[2] - magbias[2];
eric11fr 0:b8bade04f24f 145 }
eric11fr 0:b8bade04f24f 146 Now = t.read_us();
eric11fr 0:b8bade04f24f 147 deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update
eric11fr 0:b8bade04f24f 148 lastUpdate = Now;
eric11fr 0:b8bade04f24f 149 sum += deltat;
eric11fr 0:b8bade04f24f 150 sumCount++;
eric11fr 0:b8bade04f24f 151 // Pass gyro rate as rad/s
eric11fr 0:b8bade04f24f 152 // mpu9250.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz);
eric11fr 0:b8bade04f24f 153 mpu9250.MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz);
eric11fr 0:b8bade04f24f 154 // Serial print and/or display at 0.5 s rate independent of data rates
eric11fr 0:b8bade04f24f 155 delt_t = t.read_ms() - countA;
eric11fr 4:34a8e94c6fd5 156 if (delt_t > 1000)
eric11fr 0:b8bade04f24f 157 { // update LCD once per half-second independent of read rate
eric11fr 0:b8bade04f24f 158 pc.printf("ax = %f", 1000*ax);
eric11fr 0:b8bade04f24f 159 pc.printf(" ay = %f", 1000*ay);
eric11fr 0:b8bade04f24f 160 pc.printf(" az = %f mg\n\r", 1000*az);
eric11fr 0:b8bade04f24f 161 pc.printf("gx = %f", gx);
eric11fr 0:b8bade04f24f 162 pc.printf(" gy = %f", gy);
eric11fr 0:b8bade04f24f 163 pc.printf(" gz = %f deg/s\n\r", gz);
eric11fr 0:b8bade04f24f 164 pc.printf("gx = %f", mx);
eric11fr 0:b8bade04f24f 165 pc.printf(" gy = %f", my);
eric11fr 0:b8bade04f24f 166 pc.printf(" gz = %f mG\n\r", mz);
eric11fr 4:34a8e94c6fd5 167 /*---------------------------------------------------*/
eric11fr 4:34a8e94c6fd5 168 xbee.printf("ax = %f", 1000*ax);
eric11fr 4:34a8e94c6fd5 169 xbee.printf(" ay = %f", 1000*ay);
eric11fr 4:34a8e94c6fd5 170 xbee.printf(" az = %f mg\n\r", 1000*az);
eric11fr 4:34a8e94c6fd5 171 xbee.printf("gx = %f", gx);
eric11fr 4:34a8e94c6fd5 172 xbee.printf(" gy = %f", gy);
eric11fr 4:34a8e94c6fd5 173 xbee.printf(" gz = %f deg/s\n\r", gz);
eric11fr 4:34a8e94c6fd5 174 xbee.printf("gx = %f", mx);
eric11fr 4:34a8e94c6fd5 175 xbee.printf(" gy = %f", my);
eric11fr 4:34a8e94c6fd5 176 xbee.printf(" gz = %f mG\n\r", mz);
eric11fr 0:b8bade04f24f 177 tempCount = mpu9250.readTempData(); // Read the adc values
eric11fr 0:b8bade04f24f 178 temperature = ((float) tempCount) / 333.87f + 21.0f; // Temperature in degrees Centigrade
eric11fr 0:b8bade04f24f 179 pc.printf(" temperature = %f C\n\r", temperature);
eric11fr 0:b8bade04f24f 180 pc.printf("average rate = %f\n\r", (float) sumCount/sum);
eric11fr 4:34a8e94c6fd5 181 xbee.printf(" temperature = %f C\n\r", temperature);
eric11fr 4:34a8e94c6fd5 182 xbee.printf("average rate = %f\n\r", (float) sumCount/sum);
eric11fr 0:b8bade04f24f 183 countA = t.read_ms();
eric11fr 0:b8bade04f24f 184 if(countA > 1<<21)
eric11fr 0:b8bade04f24f 185 {
eric11fr 0:b8bade04f24f 186 t.start(); // start the timer over again if ~30 minutes has passed
eric11fr 0:b8bade04f24f 187 countA = 0;
eric11fr 0:b8bade04f24f 188 deltat= 0;
eric11fr 0:b8bade04f24f 189 lastUpdate = t.read_us();
eric11fr 0:b8bade04f24f 190 }
eric11fr 0:b8bade04f24f 191 sum = 0;
eric11fr 0:b8bade04f24f 192 sumCount = 0;
eric11fr 0:b8bade04f24f 193 }
eric11fr 0:b8bade04f24f 194 if(pc.readable())
eric11fr 0:b8bade04f24f 195 stop=pc.getc();
eric11fr 4:34a8e94c6fd5 196 if(xbee.readable())
eric11fr 4:34a8e94c6fd5 197 stop=xbee.getc();
eric11fr 4:34a8e94c6fd5 198 } while(stop!='s'&& (stop != 'S'));
eric11fr 0:b8bade04f24f 199 //activ_sensors = 0; // 3.3V power sensors board Off
eric11fr 0:b8bade04f24f 200 }
eric11fr 0:b8bade04f24f 201
eric11fr 4:34a8e94c6fd5 202 void get_image(void)
eric11fr 4:34a8e94c6fd5 203 {
eric11fr 0:b8bade04f24f 204 /* main loop */
eric11fr 0:b8bade04f24f 205 // while (1)
eric11fr 0:b8bade04f24f 206 {sdcard_spi_cs=1;//deactivate sdcard cs
eric11fr 0:b8bade04f24f 207 cam_spi_cs=0;
eric11fr 0:b8bade04f24f 208
eric11fr 0:b8bade04f24f 209
eric11fr 0:b8bade04f24f 210 uint32_t image_size;
eric11fr 0:b8bade04f24f 211 uint32_t idx;
eric11fr 0:b8bade04f24f 212
eric11fr 0:b8bade04f24f 213
eric11fr 0:b8bade04f24f 214 for(int i=0; i<3; i++)
eric11fr 0:b8bade04f24f 215 {
eric11fr 0:b8bade04f24f 216 image_size = g_arducam.CaptureImage(g_image_buffer,IMAGE_JPG_SIZE,&idx);
eric11fr 0:b8bade04f24f 217 if( image_size == 0 )
eric11fr 0:b8bade04f24f 218 {
eric11fr 4:34a8e94c6fd5 219 pc.printf("Photo failure %d\r\n",image_size);
eric11fr 4:34a8e94c6fd5 220 xbee.printf("Photo failure %d\r\n",image_size);
eric11fr 0:b8bade04f24f 221 break;
eric11fr 0:b8bade04f24f 222 }
eric11fr 0:b8bade04f24f 223 else
eric11fr 4:34a8e94c6fd5 224 {
eric11fr 4:34a8e94c6fd5 225 pc.printf("taille image%d",image_size);
eric11fr 4:34a8e94c6fd5 226 xbee.printf("taille image%d",image_size);
eric11fr 4:34a8e94c6fd5 227 }
eric11fr 0:b8bade04f24f 228 }
eric11fr 0:b8bade04f24f 229 sdcard_spi_cs=0;//activate sdcard cs, deactivate cam
eric11fr 0:b8bade04f24f 230 cam_spi_cs=1;
eric11fr 0:b8bade04f24f 231 FILE *fp = fopen("/sd/mydir/image.jpg", "w");
eric11fr 0:b8bade04f24f 232 if(fp == NULL) {
eric11fr 0:b8bade04f24f 233 error("Could not open file for write\n");
eric11fr 0:b8bade04f24f 234 }
eric11fr 0:b8bade04f24f 235
eric11fr 0:b8bade04f24f 236
eric11fr 0:b8bade04f24f 237 for (uint32_t l=1;l<image_size+1;l++)
eric11fr 0:b8bade04f24f 238 fprintf(fp, "%c",g_image_buffer[l]);
eric11fr 0:b8bade04f24f 239 //pc.printf("%c",g_image_buffer[l]);
eric11fr 0:b8bade04f24f 240 fclose(fp);
eric11fr 0:b8bade04f24f 241 }
eric11fr 0:b8bade04f24f 242 }
eric11fr 0:b8bade04f24f 243 void test_camera(void)
eric11fr 0:b8bade04f24f 244 {
eric11fr 0:b8bade04f24f 245 sdcard_spi_cs=1;//deactivate sdcard cs
eric11fr 0:b8bade04f24f 246 cam_spi_cs=0;
eric11fr 0:b8bade04f24f 247 if( g_arducam.Setup( OV_RESOLUTION_CIF/*OV_RESOLUTION_QQVGA*/,92, &cam_spi_cs, &camSPI, &camI2C) == false)
eric11fr 0:b8bade04f24f 248 {
eric11fr 0:b8bade04f24f 249 pc.printf("Arducam setup error \r\n");
eric11fr 4:34a8e94c6fd5 250 xbee.printf("Arducam setup error \r\n");
eric11fr 0:b8bade04f24f 251 exit(0);
eric11fr 0:b8bade04f24f 252 }
eric11fr 0:b8bade04f24f 253 else
eric11fr 0:b8bade04f24f 254 pc.printf("init_ok");
eric11fr 4:34a8e94c6fd5 255 xbee.printf("init_ok");
eric11fr 4:34a8e94c6fd5 256 get_image();
eric11fr 0:b8bade04f24f 257
eric11fr 0:b8bade04f24f 258 }
eric11fr 0:b8bade04f24f 259
eric11fr 0:b8bade04f24f 260 void test_gps(void)
eric11fr 0:b8bade04f24f 261 {unsigned char tmp;
eric11fr 0:b8bade04f24f 262 unsigned char stop=0;
eric11fr 0:b8bade04f24f 263 //GPS selection
eric11fr 0:b8bade04f24f 264 activ_sensors = 1; // 3.3V power sensors board On
eric11fr 0:b8bade04f24f 265
eric11fr 0:b8bade04f24f 266 init_uart();
eric11fr 0:b8bade04f24f 267 pc.printf("\r\n/*----------------------------------------------*/\r\n");
eric11fr 0:b8bade04f24f 268 pc.printf("\tGPS module test\r\n");
eric11fr 0:b8bade04f24f 269 pc.printf("send \"s\" to stop\r\n");
eric11fr 0:b8bade04f24f 270 pc.printf("/*----------------------------------------------*/\r\n");
eric11fr 4:34a8e94c6fd5 271 /*----------------------------------------------------------------*/
eric11fr 4:34a8e94c6fd5 272 xbee.printf("\r\n/*----------------------------------------------*/\r\n");
eric11fr 4:34a8e94c6fd5 273 xbee.printf("\tGPS module test\r\n");
eric11fr 4:34a8e94c6fd5 274 xbee.printf("send \"s\" to stop\r\n");
eric11fr 4:34a8e94c6fd5 275 xbee.printf("/*----------------------------------------------*/\r\n");
eric11fr 0:b8bade04f24f 276 wait(2);
eric11fr 0:b8bade04f24f 277 do
eric11fr 0:b8bade04f24f 278 {
eric11fr 0:b8bade04f24f 279 do
eric11fr 0:b8bade04f24f 280 {
eric11fr 0:b8bade04f24f 281 tmp= _getchar();
eric11fr 0:b8bade04f24f 282 pc.printf("%c",tmp);
eric11fr 4:34a8e94c6fd5 283 xbee.printf("%c",tmp);
eric11fr 0:b8bade04f24f 284 }while(tmp!='\r');
eric11fr 0:b8bade04f24f 285 if(pc.readable())
eric11fr 0:b8bade04f24f 286 stop=pc.getc();
eric11fr 4:34a8e94c6fd5 287 if(xbee.readable())
eric11fr 4:34a8e94c6fd5 288 stop=xbee.getc();
eric11fr 4:34a8e94c6fd5 289 } while(stop!='s'&& (stop != 'S'));
eric11fr 0:b8bade04f24f 290 }
eric11fr 0:b8bade04f24f 291
eric11fr 0:b8bade04f24f 292 void test_sdcard(void)
eric11fr 0:b8bade04f24f 293 {
eric11fr 0:b8bade04f24f 294 char buf[50];
eric11fr 0:b8bade04f24f 295 cam_spi_cs=1; //deactivate CS camera
eric11fr 0:b8bade04f24f 296
eric11fr 0:b8bade04f24f 297 pc.printf("\r\n/*----------------------------------------------------*/\r\n");
eric11fr 0:b8bade04f24f 298 pc.printf("\t SDCard module test\r\n");
eric11fr 0:b8bade04f24f 299 pc.printf("/*----------------------------------------------------*/\r\n");
eric11fr 0:b8bade04f24f 300 pc.printf("Write some words in sdtest.txt on sd/mydir directory\r\n");
eric11fr 4:34a8e94c6fd5 301 /*----------------------------------------------------------------*/
eric11fr 4:34a8e94c6fd5 302 xbee.printf("\r\n/*----------------------------------------------------*/\r\n");
eric11fr 4:34a8e94c6fd5 303 xbee.printf("\t SDCard module test\r\n");
eric11fr 4:34a8e94c6fd5 304 xbee.printf("/*----------------------------------------------------*/\r\n");
eric11fr 4:34a8e94c6fd5 305 xbee.printf("Write some words in sdtest.txt on sd/mydir directory\r\n");
eric11fr 0:b8bade04f24f 306 mkdir("/sd/mydir", 0777);
eric11fr 0:b8bade04f24f 307 FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
eric11fr 0:b8bade04f24f 308 if(fp == NULL) {
eric11fr 0:b8bade04f24f 309 error("Could not open file for write\n");
eric11fr 0:b8bade04f24f 310 }
eric11fr 0:b8bade04f24f 311 fprintf(fp, "Hello World, greats from Inisat!");
eric11fr 0:b8bade04f24f 312 fclose(fp);
eric11fr 0:b8bade04f24f 313 FILE *fp1 = fopen("/sd/mydir/sdtest.txt", "r");
eric11fr 0:b8bade04f24f 314 if(fp1 == NULL) {
eric11fr 0:b8bade04f24f 315 error("Could not open file for write\n");
eric11fr 0:b8bade04f24f 316 }
eric11fr 0:b8bade04f24f 317 //fscanf(fp,"%s",buf);
eric11fr 0:b8bade04f24f 318 fgets(buf,50,fp1);
eric11fr 0:b8bade04f24f 319 wait(2);
eric11fr 0:b8bade04f24f 320 pc.printf("read from sd/mydir directory : %s\r\n",buf);
eric11fr 4:34a8e94c6fd5 321 xbee.printf("read from sd/mydir directory : %s\r\n",buf);
eric11fr 0:b8bade04f24f 322 fclose(fp1);
eric11fr 0:b8bade04f24f 323 }
eric11fr 0:b8bade04f24f 324
eric11fr 4:34a8e94c6fd5 325 //void test_xbee_comm()
eric11fr 4:34a8e94c6fd5 326 //{
eric11fr 4:34a8e94c6fd5 327 // char buf[250];
eric11fr 4:34a8e94c6fd5 328 // pc.printf("\r\n/*----------------------------------------------*/\r\n");
eric11fr 4:34a8e94c6fd5 329 // pc.printf("\t xbee module test\r\n");
eric11fr 4:34a8e94c6fd5 330 // pc.printf("/*----------------------------------------------*/\r\n");
eric11fr 4:34a8e94c6fd5 331 // xbee.printf("Hello from inisat\r\nTest receiving data from Ground Station : type a sentence and press enter to loopback\r\n");
eric11fr 4:34a8e94c6fd5 332 // while(pc.getc()!='s')
eric11fr 4:34a8e94c6fd5 333 // {
eric11fr 4:34a8e94c6fd5 334 // xbee.printf("%s\r\n",pc.gets(buf,250)); // Echo
eric11fr 0:b8bade04f24f 335
eric11fr 4:34a8e94c6fd5 336 // }
eric11fr 4:34a8e94c6fd5 337 // while(xbee.getc()!='s')
eric11fr 4:34a8e94c6fd5 338 // pc.printf("%c\r\n",xbee.getc());
eric11fr 4:34a8e94c6fd5 339 //}
eric11fr 0:b8bade04f24f 340
eric11fr 0:b8bade04f24f 341 void test_analog_sensor(void)
eric11fr 0:b8bade04f24f 342 {
eric11fr 0:b8bade04f24f 343 unsigned char stop=0;
eric11fr 0:b8bade04f24f 344 activ_sensors = 1;
eric11fr 0:b8bade04f24f 345 pc.printf("\r\n/*----------------------------------------------*/\r\n");
Giamarchi 3:cb420000788e 346 pc.printf("\t Battery level, temperature sensor test\r\n");
Giamarchi 3:cb420000788e 347 pc.printf("\t Solar Panel 1 and 2 Currents test\r\n");
eric11fr 0:b8bade04f24f 348 pc.printf("send \"s\" to stop\r\n");
eric11fr 0:b8bade04f24f 349 pc.printf("/*----------------------------------------------*/\r\n");
eric11fr 4:34a8e94c6fd5 350 /*-------------------------------------------------------------------*/
eric11fr 4:34a8e94c6fd5 351 xbee.printf("\r\n/*----------------------------------------------*/\r\n");
eric11fr 4:34a8e94c6fd5 352 xbee.printf("\t Battery level, temperature sensor test\r\n");
eric11fr 4:34a8e94c6fd5 353 xbee.printf("\t Solar Panel 1 and 2 Currents test\r\n");
eric11fr 4:34a8e94c6fd5 354 xbee.printf("send \"s\" to stop\r\n");
eric11fr 4:34a8e94c6fd5 355 xbee.printf("/*----------------------------------------------*/\r\n");
eric11fr 0:b8bade04f24f 356 wait(2);
eric11fr 0:b8bade04f24f 357 do
eric11fr 0:b8bade04f24f 358 {
eric11fr 0:b8bade04f24f 359 wait_ms(300);
eric11fr 0:b8bade04f24f 360 pc.printf("battery: %.2f V\t\t",batin.read()*4.59f); // 3.3 * 13.9/10
eric11fr 0:b8bade04f24f 361 pc.printf("temperature: %.1f degC\r\n",(tempin.read()*3.3f-0.5f)/0.01f);
Giamarchi 3:cb420000788e 362 pc.printf("solar panel 1: %.1f mA\t",(sp_1in.read()*3.3f*50));
Giamarchi 3:cb420000788e 363 pc.printf("and 2: %.1f mA\r\n",(sp_2in.read()*3.3f*50));
eric11fr 4:34a8e94c6fd5 364 /*--------------------------------------------------------------*/
eric11fr 4:34a8e94c6fd5 365 xbee.printf("battery: %.2f V\t\t",batin.read()*4.59f); // 3.3 * 13.9/10
eric11fr 4:34a8e94c6fd5 366 xbee.printf("temperature: %.1f degC\r\n",(tempin.read()*3.3f-0.5f)/0.01f);
eric11fr 4:34a8e94c6fd5 367 xbee.printf("solar panel 1: %.1f mA\t",(sp_1in.read()*3.3f*50));
eric11fr 4:34a8e94c6fd5 368 xbee.printf("and 2: %.1f mA\r\n",(sp_2in.read()*3.3f*50));
eric11fr 0:b8bade04f24f 369 if(pc.readable())
eric11fr 0:b8bade04f24f 370 stop=pc.getc();
eric11fr 4:34a8e94c6fd5 371 if(xbee.readable())
eric11fr 4:34a8e94c6fd5 372 stop=xbee.getc();
eric11fr 4:34a8e94c6fd5 373 }while(stop!='s'&& (stop != 'S'));
eric11fr 0:b8bade04f24f 374 }
eric11fr 4:34a8e94c6fd5 375 /*void quit(void)
eric11fr 0:b8bade04f24f 376 {
eric11fr 0:b8bade04f24f 377 //do nothing
eric11fr 0:b8bade04f24f 378 pc.printf("\r\nbye bye...");
eric11fr 0:b8bade04f24f 379 exit(0);
eric11fr 4:34a8e94c6fd5 380 }*/
eric11fr 2:065d789dbcbd 381
eric11fr 2:065d789dbcbd 382 void test_pan_tilt(void)
eric11fr 4:34a8e94c6fd5 383 {
eric11fr 4:34a8e94c6fd5 384 unsigned char stop=0;
eric11fr 2:065d789dbcbd 385 pc.printf("\r\n/*----------------------------------------------*/\r\n");
eric11fr 4:34a8e94c6fd5 386 pc.printf("\t Pan Tilt stage test\r\n");
eric11fr 2:065d789dbcbd 387 pc.printf("send \"s\" to stop\r\n");
eric11fr 2:065d789dbcbd 388 pc.printf("/*----------------------------------------------*/\r\n");
eric11fr 4:34a8e94c6fd5 389 xbee.printf("\r\n/*----------------------------------------------*/\r\n");
eric11fr 4:34a8e94c6fd5 390 xbee.printf("\t Pan Tilt stage test\r\n");
eric11fr 4:34a8e94c6fd5 391 xbee.printf("send \"s\" to stop\r\n");
eric11fr 4:34a8e94c6fd5 392 xbee.printf("/*----------------------------------------------*/\r\n");
eric11fr 4:34a8e94c6fd5 393 wait(1);
eric11fr 2:065d789dbcbd 394 do
eric11fr 2:065d789dbcbd 395 {
eric11fr 2:065d789dbcbd 396 for(float p=0.2; p<0.7; p += 0.1)
eric11fr 2:065d789dbcbd 397 {
eric11fr 2:065d789dbcbd 398 servo_pan = p;
eric11fr 4:34a8e94c6fd5 399 servo_tilt= p;
eric11fr 2:065d789dbcbd 400 wait(0.4);
eric11fr 2:065d789dbcbd 401 }
eric11fr 2:065d789dbcbd 402 for(float p=0.7; p>0.2; p -= 0.1)
eric11fr 2:065d789dbcbd 403 {
eric11fr 2:065d789dbcbd 404 servo_pan = p;
eric11fr 4:34a8e94c6fd5 405 servo_tilt= p;
eric11fr 2:065d789dbcbd 406 wait(0.4);
eric11fr 2:065d789dbcbd 407 }
eric11fr 2:065d789dbcbd 408 if(pc.readable())
eric11fr 2:065d789dbcbd 409 stop=pc.getc();
eric11fr 4:34a8e94c6fd5 410 if(xbee.readable())
eric11fr 4:34a8e94c6fd5 411 stop=xbee.getc();
eric11fr 4:34a8e94c6fd5 412 }while(stop!='s'&& (stop != 'S'));
eric11fr 4:34a8e94c6fd5 413 servo_pan = 0.5;
eric11fr 2:065d789dbcbd 414 servo_tilt=0.5;
eric11fr 2:065d789dbcbd 415 }
eric11fr 2:065d789dbcbd 416
eric11fr 0:b8bade04f24f 417 int menu(void)
eric11fr 0:b8bade04f24f 418 {int choice=0;
eric11fr 0:b8bade04f24f 419 // menu tests
eric11fr 0:b8bade04f24f 420 pc.printf("\r\n/*-------------------------------------------*/\r\n");
eric11fr 0:b8bade04f24f 421 pc.printf("\t1. Test IMU\r\n");
eric11fr 0:b8bade04f24f 422 pc.printf("\t2. Test SDCARD\r\n");
eric11fr 0:b8bade04f24f 423 pc.printf("\t3. Test GPS\r\n");
eric11fr 0:b8bade04f24f 424 pc.printf("\t4. Test CAMERA\r\n");
Giamarchi 3:cb420000788e 425 pc.printf("\t5. Test Analog Sensors\r\n");
eric11fr 4:34a8e94c6fd5 426 pc.printf("\t6. Test pan/tilt plates \r\n");
eric11fr 0:b8bade04f24f 427 pc.printf("/*-------------------------------------------*/\r\n");
eric11fr 4:34a8e94c6fd5 428 /*---------------------------------------------------------------------*/
eric11fr 4:34a8e94c6fd5 429 xbee.printf("\r\n/*-------------------------------------------*/\r\n");
eric11fr 4:34a8e94c6fd5 430 xbee.printf("\t1. Test IMU\r\n");
eric11fr 4:34a8e94c6fd5 431 xbee.printf("\t2. Test SDCARD\r\n");
eric11fr 4:34a8e94c6fd5 432 xbee.printf("\t3. Test GPS\r\n");
eric11fr 4:34a8e94c6fd5 433 xbee.printf("\t4. Test CAMERA\r\n");
eric11fr 4:34a8e94c6fd5 434 xbee.printf("\t5. Test Analog Sensors\r\n");
eric11fr 4:34a8e94c6fd5 435 xbee.printf("\t6. Test pan/tilt plates \r\n");
eric11fr 4:34a8e94c6fd5 436 xbee.printf("/*-------------------------------------------*/\r\n");
eric11fr 0:b8bade04f24f 437 do
eric11fr 0:b8bade04f24f 438 {
eric11fr 4:34a8e94c6fd5 439 pc.printf("Please enter your choice (1 to 6):");
eric11fr 4:34a8e94c6fd5 440 xbee.printf("Please enter your choice (1 to 6):");
eric11fr 4:34a8e94c6fd5 441 if (pc.readable())
eric11fr 4:34a8e94c6fd5 442 choice=pc.getc();
eric11fr 4:34a8e94c6fd5 443 if (xbee.readable())
eric11fr 4:34a8e94c6fd5 444 choice=xbee.getc();
eric11fr 4:34a8e94c6fd5 445 }while(choice<0x31||choice>0x36);
eric11fr 0:b8bade04f24f 446 return choice;
eric11fr 0:b8bade04f24f 447 }
eric11fr 0:b8bade04f24f 448
eric11fr 0:b8bade04f24f 449 int main()
eric11fr 0:b8bade04f24f 450 {
eric11fr 4:34a8e94c6fd5 451 float batest;
eric11fr 4:34a8e94c6fd5 452 unsigned char start;
eric11fr 0:b8bade04f24f 453 int status=0;
eric11fr 4:34a8e94c6fd5 454 // NVIC_SystemReset();
eric11fr 0:b8bade04f24f 455 //initialise serial communication
eric11fr 0:b8bade04f24f 456 init_serial();
eric11fr 0:b8bade04f24f 457 //initialise inisat port
eric11fr 0:b8bade04f24f 458 Init_Inisat();
eric11fr 0:b8bade04f24f 459 batest=batin.read()*4.59f;
eric11fr 0:b8bade04f24f 460 pc.printf("battery: %.2f V\r\n",batest);
eric11fr 4:34a8e94c6fd5 461 xbee.printf("battery: %.2f V \r\n",batest);
eric11fr 4:34a8e94c6fd5 462 if (batest < 3.3f)
eric11fr 0:b8bade04f24f 463 {
eric11fr 0:b8bade04f24f 464 pc.printf("low battery, please recharge\r\n");
eric11fr 4:34a8e94c6fd5 465 xbee.printf("low battery, please recharge \r\n");
eric11fr 0:b8bade04f24f 466 exit(0);
eric11fr 4:34a8e94c6fd5 467 }
eric11fr 0:b8bade04f24f 468 pc.printf("Press \"S\" to Start\r\n");
eric11fr 4:34a8e94c6fd5 469 xbee.printf("Press \"S\" or \"s\" to Start\r\n");
eric11fr 0:b8bade04f24f 470 do
eric11fr 0:b8bade04f24f 471 {
eric11fr 0:b8bade04f24f 472 if(pc.readable())
eric11fr 0:b8bade04f24f 473 start=pc.getc();
eric11fr 4:34a8e94c6fd5 474 if(xbee.readable())
eric11fr 4:34a8e94c6fd5 475 start=xbee.getc();
eric11fr 0:b8bade04f24f 476 }while((start!='S') && (start != 's'));
eric11fr 0:b8bade04f24f 477
eric11fr 0:b8bade04f24f 478 while(1)
eric11fr 0:b8bade04f24f 479 {
eric11fr 0:b8bade04f24f 480 switch(status)
eric11fr 0:b8bade04f24f 481 {
eric11fr 0:b8bade04f24f 482 case 0 : status=menu();
eric11fr 0:b8bade04f24f 483 break;
eric11fr 0:b8bade04f24f 484 case '1' : test_imu();
eric11fr 0:b8bade04f24f 485 status=0;
eric11fr 0:b8bade04f24f 486 break;
eric11fr 0:b8bade04f24f 487 case '2' : test_sdcard();
eric11fr 0:b8bade04f24f 488 status=0;
eric11fr 0:b8bade04f24f 489 break;
eric11fr 0:b8bade04f24f 490 case '3' : test_gps();
eric11fr 0:b8bade04f24f 491 status=0;
eric11fr 0:b8bade04f24f 492 break;
eric11fr 0:b8bade04f24f 493 case '4' : test_camera();
eric11fr 0:b8bade04f24f 494 status=0;
eric11fr 0:b8bade04f24f 495 break;
eric11fr 0:b8bade04f24f 496 case '5' : test_analog_sensor();
eric11fr 0:b8bade04f24f 497 status=0;
eric11fr 0:b8bade04f24f 498 break;
eric11fr 4:34a8e94c6fd5 499 // case '6' : test_xbee_comm();
eric11fr 4:34a8e94c6fd5 500 // status=0;
eric11fr 4:34a8e94c6fd5 501 // break;
eric11fr 4:34a8e94c6fd5 502 case '6' : test_pan_tilt();
eric11fr 0:b8bade04f24f 503 status=0;
eric11fr 2:065d789dbcbd 504 break;
eric11fr 4:34a8e94c6fd5 505 default : break;
eric11fr 0:b8bade04f24f 506 }
eric11fr 0:b8bade04f24f 507 }
eric11fr 0:b8bade04f24f 508 }