Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@3:cb420000788e, 2020-10-24 (annotated)
- Committer:
- Giamarchi
- Date:
- Sat Oct 24 14:15:22 2020 +0000
- Revision:
- 3:cb420000788e
- Parent:
- 2:065d789dbcbd
- Child:
- 4:34a8e94c6fd5
Analog Sensors uptodate
Who changed what in which revision?
| User | Revision | Line number | New 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 | 0:b8bade04f24f | 43 | void telegram_bot(); |
| eric11fr | 0:b8bade04f24f | 44 | void init_serial(void) |
| eric11fr | 0:b8bade04f24f | 45 | { |
| eric11fr | 0:b8bade04f24f | 46 | pc.baud(9600); |
| eric11fr | 0:b8bade04f24f | 47 | xbee.baud(38400); |
| eric11fr | 0:b8bade04f24f | 48 | // came.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 | 0:b8bade04f24f | 60 | //cam_spi_cs=0; |
| eric11fr | 0:b8bade04f24f | 61 | // sdcard_spi_cs=0; |
| eric11fr | 0:b8bade04f24f | 62 | //test mpu9250-IMU |
| eric11fr | 0:b8bade04f24f | 63 | pc.printf("\r\n/*----------------------------------------------*/\r\n"); |
| eric11fr | 0:b8bade04f24f | 64 | pc.printf("\t IMU9250 test\r\n"); |
| eric11fr | 0:b8bade04f24f | 65 | pc.printf("send \"s\" to stop\r\n"); |
| eric11fr | 0:b8bade04f24f | 66 | pc.printf("/*----------------------------------------------*/\r\n"); |
| eric11fr | 0:b8bade04f24f | 67 | activ_sensors = 1; // 3.3V power sensors board On |
| eric11fr | 0:b8bade04f24f | 68 | wait(2); |
| eric11fr | 0:b8bade04f24f | 69 | |
| eric11fr | 0:b8bade04f24f | 70 | //Set up I2C |
| eric11fr | 0:b8bade04f24f | 71 | i2c.frequency(400000); // use fast (400 kHz) I2C |
| eric11fr | 0:b8bade04f24f | 72 | t.start(); |
| eric11fr | 0:b8bade04f24f | 73 | // Read the WHO_AM_I register, this is a good test of communication |
| eric11fr | 0:b8bade04f24f | 74 | uint8_t whoami = mpu9250.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); // Read WHO_AM_I register for MPU-9250 |
| eric11fr | 0:b8bade04f24f | 75 | pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x71\n\r"); |
| eric11fr | 0:b8bade04f24f | 76 | if (whoami == 0x71) // WHO_AM_I should always be 0x68 |
| eric11fr | 0:b8bade04f24f | 77 | { |
| eric11fr | 0:b8bade04f24f | 78 | pc.printf("MPU9250 WHO_AM_I is 0x%x\n\r", whoami); |
| eric11fr | 0:b8bade04f24f | 79 | pc.printf("MPU9250 is online...\n\r"); |
| eric11fr | 0:b8bade04f24f | 80 | sprintf(buffer, "0x%x", whoami); |
| eric11fr | 0:b8bade04f24f | 81 | wait(1); |
| eric11fr | 0:b8bade04f24f | 82 | mpu9250.resetMPU9250(); // Reset registers to default in preparation for device calibration |
| eric11fr | 0:b8bade04f24f | 83 | // mpu9250.MPU9250SelfTest(SelfTest); // Start by performing self test and reporting values |
| eric11fr | 0:b8bade04f24f | 84 | mpu9250.calibrateMPU9250(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers |
| eric11fr | 0:b8bade04f24f | 85 | wait(2); |
| eric11fr | 0:b8bade04f24f | 86 | mpu9250.initMPU9250(); |
| eric11fr | 0:b8bade04f24f | 87 | pc.printf("MPU9250 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature |
| eric11fr | 0:b8bade04f24f | 88 | mpu9250.initAK8963(magCalibration); |
| eric11fr | 0:b8bade04f24f | 89 | pc.printf("AK8963 initialized for active data mode....\n\r"); // Initialize device for active mode read of magnetometer |
| eric11fr | 0:b8bade04f24f | 90 | pc.printf("Accelerometer full-scale range = %f g\n\r", 2.0f*(float)(1<<Ascale)); |
| eric11fr | 0:b8bade04f24f | 91 | pc.printf("Gyroscope full-scale range = %f deg/s\n\r", 250.0f*(float)(1<<Gscale)); |
| eric11fr | 0:b8bade04f24f | 92 | if(Mscale == 0) pc.printf("Magnetometer resolution = 14 bits\n\r"); |
| eric11fr | 0:b8bade04f24f | 93 | if(Mscale == 1) pc.printf("Magnetometer resolution = 16 bits\n\r"); |
| eric11fr | 0:b8bade04f24f | 94 | if(Mmode == 2) pc.printf("Magnetometer ODR = 8 Hz\n\r"); |
| eric11fr | 0:b8bade04f24f | 95 | if(Mmode == 6) pc.printf("Magnetometer ODR = 100 Hz\n\r"); |
| eric11fr | 0:b8bade04f24f | 96 | wait(1); |
| eric11fr | 0:b8bade04f24f | 97 | } |
| eric11fr | 0:b8bade04f24f | 98 | else |
| eric11fr | 0:b8bade04f24f | 99 | { |
| eric11fr | 0:b8bade04f24f | 100 | pc.printf("Could not connect to MPU9250: \n\r"); |
| eric11fr | 0:b8bade04f24f | 101 | pc.printf("%#x \n", whoami); |
| eric11fr | 0:b8bade04f24f | 102 | sprintf(buffer, "WHO_AM_I 0x%x", whoami); |
| eric11fr | 0:b8bade04f24f | 103 | } |
| eric11fr | 0:b8bade04f24f | 104 | mpu9250.getAres(); // Get accelerometer sensitivity |
| eric11fr | 0:b8bade04f24f | 105 | mpu9250.getGres(); // Get gyro sensitivity |
| eric11fr | 0:b8bade04f24f | 106 | mpu9250.getMres(); // Get magnetometer sensitivity |
| eric11fr | 0:b8bade04f24f | 107 | pc.printf("Accelerometer sensitivity is %f LSB/g \n\r", 1.0f/aRes); |
| eric11fr | 0:b8bade04f24f | 108 | pc.printf("Gyroscope sensitivity is %f LSB/deg/s \n\r", 1.0f/gRes); |
| eric11fr | 0:b8bade04f24f | 109 | pc.printf("Magnetometer sensitivity is %f LSB/G \n\r", 1.0f/mRes); |
| eric11fr | 0:b8bade04f24f | 110 | magbias[0] = +470.; // User environmental x-axis correction in milliGauss, should be automatically calculated |
| eric11fr | 0:b8bade04f24f | 111 | magbias[1] = +120.; // User environmental x-axis correction in milliGauss |
| eric11fr | 0:b8bade04f24f | 112 | magbias[2] = +125.; // User environmental x-axis correction in milliGauss |
| eric11fr | 0:b8bade04f24f | 113 | do |
| eric11fr | 0:b8bade04f24f | 114 | { |
| eric11fr | 0:b8bade04f24f | 115 | // If intPin goes high, all data registers have new data |
| eric11fr | 0:b8bade04f24f | 116 | if(mpu9250.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) |
| eric11fr | 0:b8bade04f24f | 117 | { // On interrupt, check if data ready interrupt |
| eric11fr | 0:b8bade04f24f | 118 | mpu9250.readAccelData(accelCount); // Read the x/y/z adc values |
| eric11fr | 0:b8bade04f24f | 119 | // Now we'll calculate the accleration value into actual g's |
| eric11fr | 0:b8bade04f24f | 120 | ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set |
| eric11fr | 0:b8bade04f24f | 121 | ay = (float)accelCount[1]*aRes - accelBias[1]; |
| eric11fr | 0:b8bade04f24f | 122 | az = (float)accelCount[2]*aRes - accelBias[2]; |
| eric11fr | 0:b8bade04f24f | 123 | mpu9250.readGyroData(gyroCount); // Read the x/y/z adc values |
| eric11fr | 0:b8bade04f24f | 124 | // Calculate the gyro value into actual degrees per second |
| eric11fr | 0:b8bade04f24f | 125 | gx = (float)gyroCount[0]*gRes - gyroBias[0]; // get actual gyro value, this depends on scale being set |
| eric11fr | 0:b8bade04f24f | 126 | gy = (float)gyroCount[1]*gRes - gyroBias[1]; |
| eric11fr | 0:b8bade04f24f | 127 | gz = (float)gyroCount[2]*gRes - gyroBias[2]; |
| eric11fr | 0:b8bade04f24f | 128 | mpu9250.readMagData(magCount); // Read the x/y/z adc values |
| eric11fr | 0:b8bade04f24f | 129 | // Calculate the magnetometer values in milliGauss |
| eric11fr | 0:b8bade04f24f | 130 | // Include factory calibration per data sheet and user environmental corrections |
| eric11fr | 0:b8bade04f24f | 131 | mx = (float)magCount[0]*mRes*magCalibration[0] - magbias[0]; // get actual magnetometer value, this depends on scale being set |
| eric11fr | 0:b8bade04f24f | 132 | my = (float)magCount[1]*mRes*magCalibration[1] - magbias[1]; |
| eric11fr | 0:b8bade04f24f | 133 | mz = (float)magCount[2]*mRes*magCalibration[2] - magbias[2]; |
| eric11fr | 0:b8bade04f24f | 134 | } |
| eric11fr | 0:b8bade04f24f | 135 | Now = t.read_us(); |
| eric11fr | 0:b8bade04f24f | 136 | deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update |
| eric11fr | 0:b8bade04f24f | 137 | lastUpdate = Now; |
| eric11fr | 0:b8bade04f24f | 138 | sum += deltat; |
| eric11fr | 0:b8bade04f24f | 139 | sumCount++; |
| eric11fr | 0:b8bade04f24f | 140 | // Pass gyro rate as rad/s |
| eric11fr | 0:b8bade04f24f | 141 | // mpu9250.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz); |
| eric11fr | 0:b8bade04f24f | 142 | mpu9250.MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz); |
| eric11fr | 0:b8bade04f24f | 143 | // Serial print and/or display at 0.5 s rate independent of data rates |
| eric11fr | 0:b8bade04f24f | 144 | delt_t = t.read_ms() - countA; |
| eric11fr | 0:b8bade04f24f | 145 | if (delt_t > 500) |
| eric11fr | 0:b8bade04f24f | 146 | { // update LCD once per half-second independent of read rate |
| eric11fr | 0:b8bade04f24f | 147 | pc.printf("ax = %f", 1000*ax); |
| eric11fr | 0:b8bade04f24f | 148 | pc.printf(" ay = %f", 1000*ay); |
| eric11fr | 0:b8bade04f24f | 149 | pc.printf(" az = %f mg\n\r", 1000*az); |
| eric11fr | 0:b8bade04f24f | 150 | pc.printf("gx = %f", gx); |
| eric11fr | 0:b8bade04f24f | 151 | pc.printf(" gy = %f", gy); |
| eric11fr | 0:b8bade04f24f | 152 | pc.printf(" gz = %f deg/s\n\r", gz); |
| eric11fr | 0:b8bade04f24f | 153 | pc.printf("gx = %f", mx); |
| eric11fr | 0:b8bade04f24f | 154 | pc.printf(" gy = %f", my); |
| eric11fr | 0:b8bade04f24f | 155 | pc.printf(" gz = %f mG\n\r", mz); |
| eric11fr | 0:b8bade04f24f | 156 | tempCount = mpu9250.readTempData(); // Read the adc values |
| eric11fr | 0:b8bade04f24f | 157 | temperature = ((float) tempCount) / 333.87f + 21.0f; // Temperature in degrees Centigrade |
| eric11fr | 0:b8bade04f24f | 158 | pc.printf(" temperature = %f C\n\r", temperature); |
| eric11fr | 0:b8bade04f24f | 159 | pc.printf("average rate = %f\n\r", (float) sumCount/sum); |
| eric11fr | 0:b8bade04f24f | 160 | countA = t.read_ms(); |
| eric11fr | 0:b8bade04f24f | 161 | if(countA > 1<<21) |
| eric11fr | 0:b8bade04f24f | 162 | { |
| eric11fr | 0:b8bade04f24f | 163 | t.start(); // start the timer over again if ~30 minutes has passed |
| eric11fr | 0:b8bade04f24f | 164 | countA = 0; |
| eric11fr | 0:b8bade04f24f | 165 | deltat= 0; |
| eric11fr | 0:b8bade04f24f | 166 | lastUpdate = t.read_us(); |
| eric11fr | 0:b8bade04f24f | 167 | } |
| eric11fr | 0:b8bade04f24f | 168 | sum = 0; |
| eric11fr | 0:b8bade04f24f | 169 | sumCount = 0; |
| eric11fr | 0:b8bade04f24f | 170 | } |
| eric11fr | 0:b8bade04f24f | 171 | if(pc.readable()) |
| eric11fr | 0:b8bade04f24f | 172 | stop=pc.getc(); |
| eric11fr | 0:b8bade04f24f | 173 | } while(stop!='s'); |
| eric11fr | 0:b8bade04f24f | 174 | //activ_sensors = 0; // 3.3V power sensors board Off |
| eric11fr | 0:b8bade04f24f | 175 | } |
| eric11fr | 0:b8bade04f24f | 176 | |
| eric11fr | 0:b8bade04f24f | 177 | void telegram_bot() |
| eric11fr | 0:b8bade04f24f | 178 | { |
| eric11fr | 0:b8bade04f24f | 179 | |
| eric11fr | 0:b8bade04f24f | 180 | |
| eric11fr | 0:b8bade04f24f | 181 | |
| eric11fr | 0:b8bade04f24f | 182 | /* main loop */ |
| eric11fr | 0:b8bade04f24f | 183 | // while (1) |
| eric11fr | 0:b8bade04f24f | 184 | {sdcard_spi_cs=1;//deactivate sdcard cs |
| eric11fr | 0:b8bade04f24f | 185 | cam_spi_cs=0; |
| eric11fr | 0:b8bade04f24f | 186 | |
| eric11fr | 0:b8bade04f24f | 187 | |
| eric11fr | 0:b8bade04f24f | 188 | uint32_t image_size; |
| eric11fr | 0:b8bade04f24f | 189 | uint32_t idx; |
| eric11fr | 0:b8bade04f24f | 190 | |
| eric11fr | 0:b8bade04f24f | 191 | |
| eric11fr | 0:b8bade04f24f | 192 | for(int i=0; i<3; i++) |
| eric11fr | 0:b8bade04f24f | 193 | { |
| eric11fr | 0:b8bade04f24f | 194 | image_size = g_arducam.CaptureImage(g_image_buffer,IMAGE_JPG_SIZE,&idx); |
| eric11fr | 0:b8bade04f24f | 195 | if( image_size == 0 ) |
| eric11fr | 0:b8bade04f24f | 196 | { |
| eric11fr | 0:b8bade04f24f | 197 | printf("Photo failure %d\r\n",image_size); |
| eric11fr | 0:b8bade04f24f | 198 | break; |
| eric11fr | 0:b8bade04f24f | 199 | } |
| eric11fr | 0:b8bade04f24f | 200 | else |
| eric11fr | 0:b8bade04f24f | 201 | pc.printf("taille image%d",image_size); |
| eric11fr | 0:b8bade04f24f | 202 | } |
| eric11fr | 0:b8bade04f24f | 203 | sdcard_spi_cs=0;//activate sdcard cs, deactivate cam |
| eric11fr | 0:b8bade04f24f | 204 | cam_spi_cs=1; |
| eric11fr | 0:b8bade04f24f | 205 | FILE *fp = fopen("/sd/mydir/image.jpg", "w"); |
| eric11fr | 0:b8bade04f24f | 206 | if(fp == NULL) { |
| eric11fr | 0:b8bade04f24f | 207 | error("Could not open file for write\n"); |
| eric11fr | 0:b8bade04f24f | 208 | } |
| eric11fr | 0:b8bade04f24f | 209 | |
| eric11fr | 0:b8bade04f24f | 210 | |
| eric11fr | 0:b8bade04f24f | 211 | for (uint32_t l=1;l<image_size+1;l++) |
| eric11fr | 0:b8bade04f24f | 212 | fprintf(fp, "%c",g_image_buffer[l]); |
| eric11fr | 0:b8bade04f24f | 213 | //pc.printf("%c",g_image_buffer[l]); |
| eric11fr | 0:b8bade04f24f | 214 | fclose(fp); |
| eric11fr | 0:b8bade04f24f | 215 | } |
| eric11fr | 0:b8bade04f24f | 216 | } |
| eric11fr | 0:b8bade04f24f | 217 | void test_camera(void) |
| eric11fr | 0:b8bade04f24f | 218 | { |
| eric11fr | 0:b8bade04f24f | 219 | sdcard_spi_cs=1;//deactivate sdcard cs |
| eric11fr | 0:b8bade04f24f | 220 | cam_spi_cs=0; |
| eric11fr | 0:b8bade04f24f | 221 | // pc.baud(9600); |
| eric11fr | 0:b8bade04f24f | 222 | if( g_arducam.Setup( OV_RESOLUTION_CIF/*OV_RESOLUTION_QQVGA*/,92, &cam_spi_cs, &camSPI, &camI2C) == false) |
| eric11fr | 0:b8bade04f24f | 223 | { |
| eric11fr | 0:b8bade04f24f | 224 | pc.printf("Arducam setup error \r\n"); |
| eric11fr | 0:b8bade04f24f | 225 | exit(0); |
| eric11fr | 0:b8bade04f24f | 226 | } |
| eric11fr | 0:b8bade04f24f | 227 | else |
| eric11fr | 0:b8bade04f24f | 228 | pc.printf("init_ok"); |
| eric11fr | 0:b8bade04f24f | 229 | /* start chatbot */ |
| eric11fr | 0:b8bade04f24f | 230 | telegram_bot(); |
| eric11fr | 0:b8bade04f24f | 231 | |
| eric11fr | 0:b8bade04f24f | 232 | } |
| eric11fr | 0:b8bade04f24f | 233 | |
| eric11fr | 0:b8bade04f24f | 234 | void test_gps(void) |
| eric11fr | 0:b8bade04f24f | 235 | {unsigned char tmp; |
| eric11fr | 0:b8bade04f24f | 236 | unsigned char stop=0; |
| eric11fr | 0:b8bade04f24f | 237 | // ser.baud(9600); |
| eric11fr | 0:b8bade04f24f | 238 | //GPS selection |
| eric11fr | 0:b8bade04f24f | 239 | activ_sensors = 1; // 3.3V power sensors board On |
| eric11fr | 0:b8bade04f24f | 240 | |
| eric11fr | 0:b8bade04f24f | 241 | init_uart(); |
| eric11fr | 0:b8bade04f24f | 242 | pc.printf("\r\n/*----------------------------------------------*/\r\n"); |
| eric11fr | 0:b8bade04f24f | 243 | pc.printf("\tGPS module test\r\n"); |
| eric11fr | 0:b8bade04f24f | 244 | pc.printf("send \"s\" to stop\r\n"); |
| eric11fr | 0:b8bade04f24f | 245 | pc.printf("/*----------------------------------------------*/\r\n"); |
| eric11fr | 0:b8bade04f24f | 246 | wait(2); |
| eric11fr | 0:b8bade04f24f | 247 | do |
| eric11fr | 0:b8bade04f24f | 248 | { |
| eric11fr | 0:b8bade04f24f | 249 | do |
| eric11fr | 0:b8bade04f24f | 250 | { |
| eric11fr | 0:b8bade04f24f | 251 | tmp= _getchar(); |
| eric11fr | 0:b8bade04f24f | 252 | pc.printf("%c",tmp); |
| eric11fr | 0:b8bade04f24f | 253 | }while(tmp!='\r'); |
| eric11fr | 0:b8bade04f24f | 254 | if(pc.readable()) |
| eric11fr | 0:b8bade04f24f | 255 | stop=pc.getc(); |
| eric11fr | 0:b8bade04f24f | 256 | } while(stop!='s'); |
| eric11fr | 0:b8bade04f24f | 257 | } |
| eric11fr | 0:b8bade04f24f | 258 | |
| eric11fr | 0:b8bade04f24f | 259 | void test_sdcard(void) |
| eric11fr | 0:b8bade04f24f | 260 | { |
| eric11fr | 0:b8bade04f24f | 261 | char buf[50]; |
| eric11fr | 0:b8bade04f24f | 262 | cam_spi_cs=1; //deactivate CS camera |
| eric11fr | 0:b8bade04f24f | 263 | |
| eric11fr | 0:b8bade04f24f | 264 | pc.printf("\r\n/*----------------------------------------------------*/\r\n"); |
| eric11fr | 0:b8bade04f24f | 265 | pc.printf("\t SDCard module test\r\n"); |
| eric11fr | 0:b8bade04f24f | 266 | pc.printf("/*----------------------------------------------------*/\r\n"); |
| eric11fr | 0:b8bade04f24f | 267 | pc.printf("Write some words in sdtest.txt on sd/mydir directory\r\n"); |
| eric11fr | 0:b8bade04f24f | 268 | mkdir("/sd/mydir", 0777); |
| eric11fr | 0:b8bade04f24f | 269 | FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); |
| eric11fr | 0:b8bade04f24f | 270 | if(fp == NULL) { |
| eric11fr | 0:b8bade04f24f | 271 | error("Could not open file for write\n"); |
| eric11fr | 0:b8bade04f24f | 272 | } |
| eric11fr | 0:b8bade04f24f | 273 | fprintf(fp, "Hello World, greats from Inisat!"); |
| eric11fr | 0:b8bade04f24f | 274 | fclose(fp); |
| eric11fr | 0:b8bade04f24f | 275 | FILE *fp1 = fopen("/sd/mydir/sdtest.txt", "r"); |
| eric11fr | 0:b8bade04f24f | 276 | if(fp1 == NULL) { |
| eric11fr | 0:b8bade04f24f | 277 | error("Could not open file for write\n"); |
| eric11fr | 0:b8bade04f24f | 278 | } |
| eric11fr | 0:b8bade04f24f | 279 | //fscanf(fp,"%s",buf); |
| eric11fr | 0:b8bade04f24f | 280 | fgets(buf,50,fp1); |
| eric11fr | 0:b8bade04f24f | 281 | wait(2); |
| eric11fr | 0:b8bade04f24f | 282 | pc.printf("read from sd/mydir directory : %s\r\n",buf); |
| eric11fr | 0:b8bade04f24f | 283 | fclose(fp1); |
| eric11fr | 0:b8bade04f24f | 284 | } |
| eric11fr | 0:b8bade04f24f | 285 | |
| eric11fr | 0:b8bade04f24f | 286 | void test_xbee_comm() |
| eric11fr | 0:b8bade04f24f | 287 | { |
| eric11fr | 0:b8bade04f24f | 288 | char buf[250]; |
| eric11fr | 0:b8bade04f24f | 289 | pc.printf("\r\n/*----------------------------------------------*/\r\n"); |
| eric11fr | 0:b8bade04f24f | 290 | pc.printf("\t xbee module test\r\n"); |
| eric11fr | 0:b8bade04f24f | 291 | pc.printf("/*----------------------------------------------*/\r\n"); |
| eric11fr | 0:b8bade04f24f | 292 | |
| eric11fr | 0:b8bade04f24f | 293 | xbee.printf("Hello from inisat\r\nTest receiving data from Ground Station : type a sentence and press enter to loopback\r\n"); |
| eric11fr | 0:b8bade04f24f | 294 | while(pc.getc()!='s') |
| eric11fr | 0:b8bade04f24f | 295 | { |
| eric11fr | 0:b8bade04f24f | 296 | xbee.printf("%s\r\n",pc.gets(buf,250)); // Echo |
| eric11fr | 0:b8bade04f24f | 297 | |
| eric11fr | 0:b8bade04f24f | 298 | } |
| eric11fr | 0:b8bade04f24f | 299 | while(xbee.getc()!='s') |
| eric11fr | 0:b8bade04f24f | 300 | pc.printf("%c\r\n",xbee.getc()); |
| eric11fr | 0:b8bade04f24f | 301 | } |
| eric11fr | 0:b8bade04f24f | 302 | |
| eric11fr | 0:b8bade04f24f | 303 | void test_analog_sensor(void) |
| eric11fr | 0:b8bade04f24f | 304 | { |
| eric11fr | 0:b8bade04f24f | 305 | unsigned char stop=0; |
| eric11fr | 0:b8bade04f24f | 306 | activ_sensors = 1; |
| eric11fr | 0:b8bade04f24f | 307 | pc.printf("\r\n/*----------------------------------------------*/\r\n"); |
| Giamarchi | 3:cb420000788e | 308 | pc.printf("\t Battery level, temperature sensor test\r\n"); |
| Giamarchi | 3:cb420000788e | 309 | pc.printf("\t Solar Panel 1 and 2 Currents test\r\n"); |
| eric11fr | 0:b8bade04f24f | 310 | pc.printf("send \"s\" to stop\r\n"); |
| eric11fr | 0:b8bade04f24f | 311 | pc.printf("/*----------------------------------------------*/\r\n"); |
| eric11fr | 0:b8bade04f24f | 312 | wait(2); |
| eric11fr | 0:b8bade04f24f | 313 | do |
| eric11fr | 0:b8bade04f24f | 314 | { |
| eric11fr | 0:b8bade04f24f | 315 | wait_ms(300); |
| eric11fr | 0:b8bade04f24f | 316 | pc.printf("battery: %.2f V\t\t",batin.read()*4.59f); // 3.3 * 13.9/10 |
| eric11fr | 0:b8bade04f24f | 317 | pc.printf("temperature: %.1f degC\r\n",(tempin.read()*3.3f-0.5f)/0.01f); |
| Giamarchi | 3:cb420000788e | 318 | pc.printf("solar panel 1: %.1f mA\t",(sp_1in.read()*3.3f*50)); |
| Giamarchi | 3:cb420000788e | 319 | pc.printf("and 2: %.1f mA\r\n",(sp_2in.read()*3.3f*50)); |
| eric11fr | 0:b8bade04f24f | 320 | if(pc.readable()) |
| eric11fr | 0:b8bade04f24f | 321 | stop=pc.getc(); |
| eric11fr | 0:b8bade04f24f | 322 | }while(stop!='s'); |
| eric11fr | 0:b8bade04f24f | 323 | } |
| eric11fr | 0:b8bade04f24f | 324 | void quit(void) |
| eric11fr | 0:b8bade04f24f | 325 | { |
| eric11fr | 0:b8bade04f24f | 326 | //do nothing |
| eric11fr | 0:b8bade04f24f | 327 | pc.printf("\r\nbye bye..."); |
| eric11fr | 0:b8bade04f24f | 328 | exit(0); |
| eric11fr | 0:b8bade04f24f | 329 | } |
| eric11fr | 2:065d789dbcbd | 330 | |
| eric11fr | 2:065d789dbcbd | 331 | void test_pan_tilt(void) |
| eric11fr | 2:065d789dbcbd | 332 | {unsigned char stop=0; |
| eric11fr | 2:065d789dbcbd | 333 | pc.printf("\r\n/*----------------------------------------------*/\r\n"); |
| eric11fr | 2:065d789dbcbd | 334 | pc.printf("\t Pan Tilt test\r\n"); |
| eric11fr | 2:065d789dbcbd | 335 | pc.printf("send \"s\" to stop\r\n"); |
| eric11fr | 2:065d789dbcbd | 336 | pc.printf("/*----------------------------------------------*/\r\n"); |
| eric11fr | 2:065d789dbcbd | 337 | wait(2); |
| eric11fr | 2:065d789dbcbd | 338 | do |
| eric11fr | 2:065d789dbcbd | 339 | { |
| eric11fr | 2:065d789dbcbd | 340 | for(float p=0.2; p<0.7; p += 0.1) |
| eric11fr | 2:065d789dbcbd | 341 | { |
| eric11fr | 2:065d789dbcbd | 342 | servo_pan = p; |
| eric11fr | 2:065d789dbcbd | 343 | servo_tilt=p; |
| eric11fr | 2:065d789dbcbd | 344 | wait(0.4); |
| eric11fr | 2:065d789dbcbd | 345 | } |
| eric11fr | 2:065d789dbcbd | 346 | for(float p=0.7; p>0.2; p -= 0.1) |
| eric11fr | 2:065d789dbcbd | 347 | { |
| eric11fr | 2:065d789dbcbd | 348 | servo_pan = p; |
| eric11fr | 2:065d789dbcbd | 349 | servo_tilt=p; |
| eric11fr | 2:065d789dbcbd | 350 | wait(0.4); |
| eric11fr | 2:065d789dbcbd | 351 | } |
| eric11fr | 2:065d789dbcbd | 352 | if(pc.readable()) |
| eric11fr | 2:065d789dbcbd | 353 | stop=pc.getc(); |
| eric11fr | 2:065d789dbcbd | 354 | }while(stop!='s'); |
| eric11fr | 2:065d789dbcbd | 355 | servo_pan = 0.5; |
| eric11fr | 2:065d789dbcbd | 356 | servo_tilt=0.5; |
| eric11fr | 2:065d789dbcbd | 357 | } |
| eric11fr | 2:065d789dbcbd | 358 | |
| eric11fr | 0:b8bade04f24f | 359 | int menu(void) |
| eric11fr | 0:b8bade04f24f | 360 | {int choice=0; |
| eric11fr | 0:b8bade04f24f | 361 | // menu tests |
| eric11fr | 0:b8bade04f24f | 362 | //pc.putc(27);//to clear terminal screen |
| eric11fr | 0:b8bade04f24f | 363 | //pc.printf("[2J"); |
| eric11fr | 0:b8bade04f24f | 364 | pc.printf("\r\n/*-------------------------------------------*/\r\n"); |
| eric11fr | 0:b8bade04f24f | 365 | pc.printf("\t1. Test IMU\r\n"); |
| eric11fr | 0:b8bade04f24f | 366 | pc.printf("\t2. Test SDCARD\r\n"); |
| eric11fr | 0:b8bade04f24f | 367 | pc.printf("\t3. Test GPS\r\n"); |
| eric11fr | 0:b8bade04f24f | 368 | pc.printf("\t4. Test CAMERA\r\n"); |
| Giamarchi | 3:cb420000788e | 369 | pc.printf("\t5. Test Analog Sensors\r\n"); |
| eric11fr | 0:b8bade04f24f | 370 | pc.printf("\t6. Test XBEE communication\r\n"); |
| eric11fr | 2:065d789dbcbd | 371 | pc.printf("\t7. Test pan/tilt plates \r\n"); |
| eric11fr | 2:065d789dbcbd | 372 | pc.printf("\t8. Quit\r\n"); |
| eric11fr | 0:b8bade04f24f | 373 | pc.printf("/*-------------------------------------------*/\r\n"); |
| eric11fr | 0:b8bade04f24f | 374 | do |
| eric11fr | 0:b8bade04f24f | 375 | { |
| eric11fr | 2:065d789dbcbd | 376 | pc.printf("Please enter your choice (1 to 8):"); |
| eric11fr | 0:b8bade04f24f | 377 | choice=pc.getc(); |
| eric11fr | 2:065d789dbcbd | 378 | }while(choice<0x31||choice>0x38); |
| eric11fr | 0:b8bade04f24f | 379 | return choice; |
| eric11fr | 0:b8bade04f24f | 380 | } |
| eric11fr | 0:b8bade04f24f | 381 | |
| eric11fr | 0:b8bade04f24f | 382 | int main() |
| eric11fr | 0:b8bade04f24f | 383 | { |
| eric11fr | 0:b8bade04f24f | 384 | float batest; |
| eric11fr | 0:b8bade04f24f | 385 | unsigned char start; |
| eric11fr | 0:b8bade04f24f | 386 | |
| eric11fr | 0:b8bade04f24f | 387 | int status=0; |
| eric11fr | 0:b8bade04f24f | 388 | //initialise serial communication |
| eric11fr | 0:b8bade04f24f | 389 | init_serial(); |
| eric11fr | 0:b8bade04f24f | 390 | //initialise inisat port |
| eric11fr | 0:b8bade04f24f | 391 | Init_Inisat(); |
| eric11fr | 0:b8bade04f24f | 392 | batest=batin.read()*4.59f; |
| eric11fr | 0:b8bade04f24f | 393 | pc.printf("battery: %.2f V\r\n",batest); |
| eric11fr | 0:b8bade04f24f | 394 | |
| eric11fr | 0:b8bade04f24f | 395 | /* if (batest < 3.3f) |
| eric11fr | 0:b8bade04f24f | 396 | { |
| eric11fr | 0:b8bade04f24f | 397 | pc.printf("low battery, please recharge\r\n"); |
| eric11fr | 0:b8bade04f24f | 398 | exit(0); |
| eric11fr | 0:b8bade04f24f | 399 | }*/ |
| eric11fr | 0:b8bade04f24f | 400 | pc.printf("Press \"S\" to Start\r\n"); |
| eric11fr | 0:b8bade04f24f | 401 | do |
| eric11fr | 0:b8bade04f24f | 402 | { |
| eric11fr | 0:b8bade04f24f | 403 | if(pc.readable()) |
| eric11fr | 0:b8bade04f24f | 404 | start=pc.getc(); |
| eric11fr | 0:b8bade04f24f | 405 | }while((start!='S') && (start != 's')); |
| eric11fr | 0:b8bade04f24f | 406 | |
| eric11fr | 0:b8bade04f24f | 407 | while(1) |
| eric11fr | 0:b8bade04f24f | 408 | { |
| eric11fr | 0:b8bade04f24f | 409 | switch(status) |
| eric11fr | 0:b8bade04f24f | 410 | { |
| eric11fr | 0:b8bade04f24f | 411 | case 0 : status=menu(); |
| eric11fr | 0:b8bade04f24f | 412 | break; |
| eric11fr | 0:b8bade04f24f | 413 | case '1' : test_imu(); |
| eric11fr | 0:b8bade04f24f | 414 | status=0; |
| eric11fr | 0:b8bade04f24f | 415 | break; |
| eric11fr | 0:b8bade04f24f | 416 | case '2' : test_sdcard(); |
| eric11fr | 0:b8bade04f24f | 417 | status=0; |
| eric11fr | 0:b8bade04f24f | 418 | break; |
| eric11fr | 0:b8bade04f24f | 419 | case '3' : test_gps(); |
| eric11fr | 0:b8bade04f24f | 420 | status=0; |
| eric11fr | 0:b8bade04f24f | 421 | break; |
| eric11fr | 0:b8bade04f24f | 422 | case '4' : test_camera(); |
| eric11fr | 0:b8bade04f24f | 423 | status=0; |
| eric11fr | 0:b8bade04f24f | 424 | break; |
| eric11fr | 0:b8bade04f24f | 425 | case '5' : test_analog_sensor(); |
| eric11fr | 0:b8bade04f24f | 426 | status=0; |
| eric11fr | 0:b8bade04f24f | 427 | break; |
| eric11fr | 0:b8bade04f24f | 428 | case '6' : test_xbee_comm(); |
| eric11fr | 0:b8bade04f24f | 429 | status=0; |
| eric11fr | 2:065d789dbcbd | 430 | break; |
| eric11fr | 2:065d789dbcbd | 431 | case '7' : test_pan_tilt(); |
| eric11fr | 2:065d789dbcbd | 432 | status=0; |
| eric11fr | 2:065d789dbcbd | 433 | break; |
| eric11fr | 0:b8bade04f24f | 434 | //case 7: all_test(); |
| eric11fr | 0:b8bade04f24f | 435 | // break; |
| eric11fr | 2:065d789dbcbd | 436 | case '8' : quit(); |
| eric11fr | 0:b8bade04f24f | 437 | break; |
| eric11fr | 0:b8bade04f24f | 438 | } |
| eric11fr | 0:b8bade04f24f | 439 | } |
| eric11fr | 0:b8bade04f24f | 440 | } |