![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
NSYS for car records vibration data to sd card via wireless command
Dependencies: mbed SDFileSystem
main.cpp@0:36c81e7b1032, 2020-10-07 (annotated)
- Committer:
- dyson_rey
- Date:
- Wed Oct 07 05:00:46 2020 +0000
- Revision:
- 0:36c81e7b1032
NSYS for car; first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dyson_rey | 0:36c81e7b1032 | 1 | /*========================================= |
dyson_rey | 0:36c81e7b1032 | 2 | Title:NSYS for CAR |
dyson_rey | 0:36c81e7b1032 | 3 | |
dyson_rey | 0:36c81e7b1032 | 4 | Revision:1.0 |
dyson_rey | 0:36c81e7b1032 | 5 | |
dyson_rey | 0:36c81e7b1032 | 6 | Editor:Dyson |
dyson_rey | 0:36c81e7b1032 | 7 | |
dyson_rey | 0:36c81e7b1032 | 8 | For GR-PEACH |
dyson_rey | 0:36c81e7b1032 | 9 | Compile using Mbed Online Compiler (revision:130) |
dyson_rey | 0:36c81e7b1032 | 10 | |
dyson_rey | 0:36c81e7b1032 | 11 | THIS IS THE FINAL PRODUCT,in xb mode |
dyson_rey | 0:36c81e7b1032 | 12 | |
dyson_rey | 0:36c81e7b1032 | 13 | reminder:for frontier 3.1 |
dyson_rey | 0:36c81e7b1032 | 14 | Last Modified:2019/12/12 |
dyson_rey | 0:36c81e7b1032 | 15 | ==========================================*/ |
dyson_rey | 0:36c81e7b1032 | 16 | #include "mbed.h"//for mbed |
dyson_rey | 0:36c81e7b1032 | 17 | #include "SDFileSystem.h"//for SD |
dyson_rey | 0:36c81e7b1032 | 18 | |
dyson_rey | 0:36c81e7b1032 | 19 | #define _SDPRESENT 0//LOW for no SD card |
dyson_rey | 0:36c81e7b1032 | 20 | #define _SDNOTPRESENT 1//HIGH for SD card present |
dyson_rey | 0:36c81e7b1032 | 21 | #define _ENABLE 0//LOW for ADC enable |
dyson_rey | 0:36c81e7b1032 | 22 | #define _DISABLE 1//HIGH for ADC disable |
dyson_rey | 0:36c81e7b1032 | 23 | #define _KILLCOUNT 177// for 10 mins |
dyson_rey | 0:36c81e7b1032 | 24 | |
dyson_rey | 0:36c81e7b1032 | 25 | //for teraterm color logging |
dyson_rey | 0:36c81e7b1032 | 26 | //use in pc.printf("%shoge%s",_RED,_END) |
dyson_rey | 0:36c81e7b1032 | 27 | #define _RED "\e[31m" |
dyson_rey | 0:36c81e7b1032 | 28 | #define _GREEN "\e[32m" |
dyson_rey | 0:36c81e7b1032 | 29 | #define _YELLOW "\e[33m" |
dyson_rey | 0:36c81e7b1032 | 30 | #define _END "\e[m" |
dyson_rey | 0:36c81e7b1032 | 31 | |
dyson_rey | 0:36c81e7b1032 | 32 | //functions |
dyson_rey | 0:36c81e7b1032 | 33 | bool gps_read(bool fix,bool write); |
dyson_rey | 0:36c81e7b1032 | 34 | void data_init(void); |
dyson_rey | 0:36c81e7b1032 | 35 | void end_proc(void); |
dyson_rey | 0:36c81e7b1032 | 36 | int pot_read(void); |
dyson_rey | 0:36c81e7b1032 | 37 | bool sd_init(void); |
dyson_rey | 0:36c81e7b1032 | 38 | void adc_init(void); |
dyson_rey | 0:36c81e7b1032 | 39 | void adc_work(void); |
dyson_rey | 0:36c81e7b1032 | 40 | inline void pps_rise(void); |
dyson_rey | 0:36c81e7b1032 | 41 | void rx(void); |
dyson_rey | 0:36c81e7b1032 | 42 | void report(void); |
dyson_rey | 0:36c81e7b1032 | 43 | void sd_read(void); |
dyson_rey | 0:36c81e7b1032 | 44 | |
dyson_rey | 0:36c81e7b1032 | 45 | //Serial |
dyson_rey | 0:36c81e7b1032 | 46 | Serial pc(USBTX, USBRX); |
dyson_rey | 0:36c81e7b1032 | 47 | Serial xb(P7_4, P7_5); |
dyson_rey | 0:36c81e7b1032 | 48 | |
dyson_rey | 0:36c81e7b1032 | 49 | bool start=false;//turn true to start |
dyson_rey | 0:36c81e7b1032 | 50 | bool status[4];//ready,gps lock,measuring,error |
dyson_rey | 0:36c81e7b1032 | 51 | |
dyson_rey | 0:36c81e7b1032 | 52 | //SD |
dyson_rey | 0:36c81e7b1032 | 53 | FILE *fp;//file pointer |
dyson_rey | 0:36c81e7b1032 | 54 | //SDFileSystem sd(P5_2, P5_3, P5_0, P5_1, "sd");//SD reader on frontier 4.0 |
dyson_rey | 0:36c81e7b1032 | 55 | SDFileSystem sd(P8_5, P8_6, P8_3, P8_4, "sd");//SD reader on peach |
dyson_rey | 0:36c81e7b1032 | 56 | //DigitalIn sd_detect(P1_2);//SD detection pin |
dyson_rey | 0:36c81e7b1032 | 57 | char filename[256];//global for using pot_num and start_time |
dyson_rey | 0:36c81e7b1032 | 58 | |
dyson_rey | 0:36c81e7b1032 | 59 | //CONTROL |
dyson_rey | 0:36c81e7b1032 | 60 | // DigitalIn one(P4_4); |
dyson_rey | 0:36c81e7b1032 | 61 | // DigitalIn two(P4_6); |
dyson_rey | 0:36c81e7b1032 | 62 | // DigitalIn four(P4_5); |
dyson_rey | 0:36c81e7b1032 | 63 | // DigitalIn eight(P4_7); |
dyson_rey | 0:36c81e7b1032 | 64 | int pot_num; |
dyson_rey | 0:36c81e7b1032 | 65 | |
dyson_rey | 0:36c81e7b1032 | 66 | InterruptIn pb(P6_0);//user switch on peach |
dyson_rey | 0:36c81e7b1032 | 67 | bool switch_pressed = false;//use this to write alternative routines |
dyson_rey | 0:36c81e7b1032 | 68 | void skip_switch(void) { switch_pressed = true; } |
dyson_rey | 0:36c81e7b1032 | 69 | |
dyson_rey | 0:36c81e7b1032 | 70 | //GPS |
dyson_rey | 0:36c81e7b1032 | 71 | Serial gps(P8_13, P8_11); |
dyson_rey | 0:36c81e7b1032 | 72 | InterruptIn PPS(P2_15); |
dyson_rey | 0:36c81e7b1032 | 73 | volatile float t_pulse;//micom time when PPS rised |
dyson_rey | 0:36c81e7b1032 | 74 | double gps_time; |
dyson_rey | 0:36c81e7b1032 | 75 | double start_time=-1; |
dyson_rey | 0:36c81e7b1032 | 76 | |
dyson_rey | 0:36c81e7b1032 | 77 | //TIMERS |
dyson_rey | 0:36c81e7b1032 | 78 | Timer utime; |
dyson_rey | 0:36c81e7b1032 | 79 | Ticker adc_ticker; |
dyson_rey | 0:36c81e7b1032 | 80 | Ticker stat_ticker; |
dyson_rey | 0:36c81e7b1032 | 81 | |
dyson_rey | 0:36c81e7b1032 | 82 | //ADC |
dyson_rey | 0:36c81e7b1032 | 83 | SPI adc(P10_14, P10_15, P10_12);//mosi,miso,sck |
dyson_rey | 0:36c81e7b1032 | 84 | //DigitalOut cs_adc(P1_3);//Frontier 4.0 |
dyson_rey | 0:36c81e7b1032 | 85 | DigitalOut cs_adc(P8_15);//Frontier 2.0 |
dyson_rey | 0:36c81e7b1032 | 86 | volatile int rch[3] = {}; |
dyson_rey | 0:36c81e7b1032 | 87 | |
dyson_rey | 0:36c81e7b1032 | 88 | //LED |
dyson_rey | 0:36c81e7b1032 | 89 | DigitalOut led_rgb[] = {(LED1), (LED2), (LED3)}; |
dyson_rey | 0:36c81e7b1032 | 90 | |
dyson_rey | 0:36c81e7b1032 | 91 | //BUFFERS |
dyson_rey | 0:36c81e7b1032 | 92 | #define BUFF_COUNT 3 |
dyson_rey | 0:36c81e7b1032 | 93 | #define BUFF_DEPTH 4096 |
dyson_rey | 0:36c81e7b1032 | 94 | int buff_depth = 0; |
dyson_rey | 0:36c81e7b1032 | 95 | int buff_index = 0; |
dyson_rey | 0:36c81e7b1032 | 96 | int cur_buff = 0; |
dyson_rey | 0:36c81e7b1032 | 97 | bool write_ok[BUFF_COUNT]; |
dyson_rey | 0:36c81e7b1032 | 98 | |
dyson_rey | 0:36c81e7b1032 | 99 | int kill = 0; |
dyson_rey | 0:36c81e7b1032 | 100 | |
dyson_rey | 0:36c81e7b1032 | 101 | int adcsel = 0; |
dyson_rey | 0:36c81e7b1032 | 102 | |
dyson_rey | 0:36c81e7b1032 | 103 | typedef struct Logdata_t { |
dyson_rey | 0:36c81e7b1032 | 104 | int axis; |
dyson_rey | 0:36c81e7b1032 | 105 | int acc; |
dyson_rey | 0:36c81e7b1032 | 106 | float t_acc; |
dyson_rey | 0:36c81e7b1032 | 107 | float t_pulse; |
dyson_rey | 0:36c81e7b1032 | 108 | } Logdata; |
dyson_rey | 0:36c81e7b1032 | 109 | Logdata data[BUFF_COUNT][BUFF_DEPTH]; |
dyson_rey | 0:36c81e7b1032 | 110 | |
dyson_rey | 0:36c81e7b1032 | 111 | bool gps_read(bool fix,bool write) { //fix 0=no,1 = locked;write 0=no,1 =yes |
dyson_rey | 0:36c81e7b1032 | 112 | float lat, lng; |
dyson_rey | 0:36c81e7b1032 | 113 | int stat=0; |
dyson_rey | 0:36c81e7b1032 | 114 | char ns, ew; |
dyson_rey | 0:36c81e7b1032 | 115 | while (1) { |
dyson_rey | 0:36c81e7b1032 | 116 | int gps_num = 0; |
dyson_rey | 0:36c81e7b1032 | 117 | char gps_data[256]; |
dyson_rey | 0:36c81e7b1032 | 118 | memset(gps_data, '\0' , 256); |
dyson_rey | 0:36c81e7b1032 | 119 | |
dyson_rey | 0:36c81e7b1032 | 120 | while (gps.getc() != '$') {}//wait until $ |
dyson_rey | 0:36c81e7b1032 | 121 | while ( (gps_data[gps_num] = gps.getc() ) != '\r') {//get string until new line |
dyson_rey | 0:36c81e7b1032 | 122 | gps_num++; |
dyson_rey | 0:36c81e7b1032 | 123 | if (gps_num == 125) { |
dyson_rey | 0:36c81e7b1032 | 124 | //status[3]=1; |
dyson_rey | 0:36c81e7b1032 | 125 | break; |
dyson_rey | 0:36c81e7b1032 | 126 | } |
dyson_rey | 0:36c81e7b1032 | 127 | } |
dyson_rey | 0:36c81e7b1032 | 128 | gps_data[gps_num] = '\0'; |
dyson_rey | 0:36c81e7b1032 | 129 | |
dyson_rey | 0:36c81e7b1032 | 130 | if (sscanf(gps_data, "GPGGA,%lf,%f,%c,%f,%c,%d", &gps_time, &lat, &ns, &lng, &ew, &stat) >= 1) {//if it is GPGGA sentence |
dyson_rey | 0:36c81e7b1032 | 131 | gps_time += 90000;//turn in to JST |
dyson_rey | 0:36c81e7b1032 | 132 | //pc.printf("%lf\r\n",gps_time); |
dyson_rey | 0:36c81e7b1032 | 133 | if(!start && start_time>0){//if has not started yet and has valid start_time |
dyson_rey | 0:36c81e7b1032 | 134 | if(start_time>gps_time){ |
dyson_rey | 0:36c81e7b1032 | 135 | return false; |
dyson_rey | 0:36c81e7b1032 | 136 | }else if(start_time==gps_time){ |
dyson_rey | 0:36c81e7b1032 | 137 | fp = fopen(filename, "a"); |
dyson_rey | 0:36c81e7b1032 | 138 | if (fp == NULL) { |
dyson_rey | 0:36c81e7b1032 | 139 | return false; |
dyson_rey | 0:36c81e7b1032 | 140 | }else{ |
dyson_rey | 0:36c81e7b1032 | 141 | fprintf(fp, "gpstime,%lf,lat,%f,lng,%f\r\n", gps_time, lat, lng); |
dyson_rey | 0:36c81e7b1032 | 142 | fclose(fp); |
dyson_rey | 0:36c81e7b1032 | 143 | start = true; |
dyson_rey | 0:36c81e7b1032 | 144 | return true; |
dyson_rey | 0:36c81e7b1032 | 145 | } |
dyson_rey | 0:36c81e7b1032 | 146 | }else if(start_time<gps_time){ |
dyson_rey | 0:36c81e7b1032 | 147 | status[3]=1; |
dyson_rey | 0:36c81e7b1032 | 148 | report(); |
dyson_rey | 0:36c81e7b1032 | 149 | return false; |
dyson_rey | 0:36c81e7b1032 | 150 | } |
dyson_rey | 0:36c81e7b1032 | 151 | } |
dyson_rey | 0:36c81e7b1032 | 152 | //reach here if not yet started |
dyson_rey | 0:36c81e7b1032 | 153 | if (!fix || stat > 0) {//if no need to fix or has a valid lock |
dyson_rey | 0:36c81e7b1032 | 154 | if(write){//need to write |
dyson_rey | 0:36c81e7b1032 | 155 | fp = fopen(filename, "a"); |
dyson_rey | 0:36c81e7b1032 | 156 | fprintf(fp, "gpstime,%lf,lat,%f,lng,%f\r\n", gps_time, lat, lng); |
dyson_rey | 0:36c81e7b1032 | 157 | fclose(fp); |
dyson_rey | 0:36c81e7b1032 | 158 | return true; |
dyson_rey | 0:36c81e7b1032 | 159 | } |
dyson_rey | 0:36c81e7b1032 | 160 | if (stat > 0) {//has lock |
dyson_rey | 0:36c81e7b1032 | 161 | status[1] = 1; |
dyson_rey | 0:36c81e7b1032 | 162 | report(); |
dyson_rey | 0:36c81e7b1032 | 163 | return true; |
dyson_rey | 0:36c81e7b1032 | 164 | } else { |
dyson_rey | 0:36c81e7b1032 | 165 | status[1] = 0; |
dyson_rey | 0:36c81e7b1032 | 166 | report(); |
dyson_rey | 0:36c81e7b1032 | 167 | return false; |
dyson_rey | 0:36c81e7b1032 | 168 | } |
dyson_rey | 0:36c81e7b1032 | 169 | } else {//if need to write and has no valid lock |
dyson_rey | 0:36c81e7b1032 | 170 | continue; |
dyson_rey | 0:36c81e7b1032 | 171 | } |
dyson_rey | 0:36c81e7b1032 | 172 | } |
dyson_rey | 0:36c81e7b1032 | 173 | } |
dyson_rey | 0:36c81e7b1032 | 174 | } |
dyson_rey | 0:36c81e7b1032 | 175 | |
dyson_rey | 0:36c81e7b1032 | 176 | void data_init(void) {//prepare datum |
dyson_rey | 0:36c81e7b1032 | 177 | //pot_read(); |
dyson_rey | 0:36c81e7b1032 | 178 | sd_read(); |
dyson_rey | 0:36c81e7b1032 | 179 | status[0] = 1; |
dyson_rey | 0:36c81e7b1032 | 180 | start = false; |
dyson_rey | 0:36c81e7b1032 | 181 | buff_depth = 0; |
dyson_rey | 0:36c81e7b1032 | 182 | buff_index = 0; |
dyson_rey | 0:36c81e7b1032 | 183 | cur_buff = 0; |
dyson_rey | 0:36c81e7b1032 | 184 | memset(&data, '\0' , sizeof(data)); |
dyson_rey | 0:36c81e7b1032 | 185 | memset(&write_ok, '\0' , sizeof(write_ok)); |
dyson_rey | 0:36c81e7b1032 | 186 | } |
dyson_rey | 0:36c81e7b1032 | 187 | |
dyson_rey | 0:36c81e7b1032 | 188 | void end_proc() {//reet it self |
dyson_rey | 0:36c81e7b1032 | 189 | __disable_irq(); |
dyson_rey | 0:36c81e7b1032 | 190 | status[0] = status[1] = status[2] = 0; |
dyson_rey | 0:36c81e7b1032 | 191 | report(); |
dyson_rey | 0:36c81e7b1032 | 192 | wait(0.1); |
dyson_rey | 0:36c81e7b1032 | 193 | __enable_irq(); |
dyson_rey | 0:36c81e7b1032 | 194 | NVIC_SystemReset(); |
dyson_rey | 0:36c81e7b1032 | 195 | } |
dyson_rey | 0:36c81e7b1032 | 196 | |
dyson_rey | 0:36c81e7b1032 | 197 | // int pot_read(void) {//read pot value |
dyson_rey | 0:36c81e7b1032 | 198 | // pot_num += one.read() + 2 * two.read() + 4 * four.read() + 8 * eight.read(); |
dyson_rey | 0:36c81e7b1032 | 199 | // return pot_num; |
dyson_rey | 0:36c81e7b1032 | 200 | // } |
dyson_rey | 0:36c81e7b1032 | 201 | |
dyson_rey | 0:36c81e7b1032 | 202 | void sd_read(void) {//check for inout directory |
dyson_rey | 0:36c81e7b1032 | 203 | DIR *d; |
dyson_rey | 0:36c81e7b1032 | 204 | char searchname[32]; |
dyson_rey | 0:36c81e7b1032 | 205 | for(int i=0;i<12;i++){ |
dyson_rey | 0:36c81e7b1032 | 206 | sprintf(searchname, "/sd/%d",i); |
dyson_rey | 0:36c81e7b1032 | 207 | d = opendir(searchname); |
dyson_rey | 0:36c81e7b1032 | 208 | if ( d != NULL ){//if there is file |
dyson_rey | 0:36c81e7b1032 | 209 | pot_num=i; |
dyson_rey | 0:36c81e7b1032 | 210 | //pc.printf("found file\r\n"); |
dyson_rey | 0:36c81e7b1032 | 211 | return; |
dyson_rey | 0:36c81e7b1032 | 212 | }else{//if there isnt |
dyson_rey | 0:36c81e7b1032 | 213 | //pc.printf("no file\r\n"); |
dyson_rey | 0:36c81e7b1032 | 214 | continue; |
dyson_rey | 0:36c81e7b1032 | 215 | } |
dyson_rey | 0:36c81e7b1032 | 216 | } |
dyson_rey | 0:36c81e7b1032 | 217 | //pc.printf("out\r\n"); |
dyson_rey | 0:36c81e7b1032 | 218 | } |
dyson_rey | 0:36c81e7b1032 | 219 | |
dyson_rey | 0:36c81e7b1032 | 220 | bool sd_init(void) {//determine file name and file pointer. must lock gps beforehand |
dyson_rey | 0:36c81e7b1032 | 221 | mkdir("/sd/wsys", 0777); |
dyson_rey | 0:36c81e7b1032 | 222 | sprintf(filename, "/sd/wsys/%d-%.0lf.csv",pot_num, start_time); |
dyson_rey | 0:36c81e7b1032 | 223 | fp = fopen(filename, "a"); |
dyson_rey | 0:36c81e7b1032 | 224 | |
dyson_rey | 0:36c81e7b1032 | 225 | if (fp == NULL) { |
dyson_rey | 0:36c81e7b1032 | 226 | status[3] = 1; |
dyson_rey | 0:36c81e7b1032 | 227 | report(); |
dyson_rey | 0:36c81e7b1032 | 228 | return false; |
dyson_rey | 0:36c81e7b1032 | 229 | } else { |
dyson_rey | 0:36c81e7b1032 | 230 | fclose(fp); |
dyson_rey | 0:36c81e7b1032 | 231 | return true; |
dyson_rey | 0:36c81e7b1032 | 232 | } |
dyson_rey | 0:36c81e7b1032 | 233 | } |
dyson_rey | 0:36c81e7b1032 | 234 | |
dyson_rey | 0:36c81e7b1032 | 235 | void adc_init(void) { |
dyson_rey | 0:36c81e7b1032 | 236 | cs_adc = _DISABLE; // ADC chip select off |
dyson_rey | 0:36c81e7b1032 | 237 | adc.format(8, 1); //// SPI Init //// |
dyson_rey | 0:36c81e7b1032 | 238 | adc.frequency(750000); |
dyson_rey | 0:36c81e7b1032 | 239 | cs_adc = _ENABLE; // ADC chip select on |
dyson_rey | 0:36c81e7b1032 | 240 | wait_ms(112); //55ms:NG 56ms:OK |
dyson_rey | 0:36c81e7b1032 | 241 | adc.write(0x06);//reset |
dyson_rey | 0:36c81e7b1032 | 242 | wait_us(32); //15us:NG 16us:OK |
dyson_rey | 0:36c81e7b1032 | 243 | adc.write(0x43);// write register 00h-03h |
dyson_rey | 0:36c81e7b1032 | 244 | //adc.write(0xa1);// AIN2-GND GAIN:1 PGA:off |
dyson_rey | 0:36c81e7b1032 | 245 | adc.write(0x81);// AIN0-GND GAIN:1 PGA:off |
dyson_rey | 0:36c81e7b1032 | 246 | //adc.write(0xc0);// 1000sps nomalmode single temp:off burn:off |
dyson_rey | 0:36c81e7b1032 | 247 | //adc.write(0xc1);// 1000sps nomalmode single temp:off burn:on |
dyson_rey | 0:36c81e7b1032 | 248 | adc.write(0xd0);// 2000sps turbomode single temp:off burn:off |
dyson_rey | 0:36c81e7b1032 | 249 | adc.write(0xc0);// ref:AVDD |
dyson_rey | 0:36c81e7b1032 | 250 | adc.write(0x00);// DOUT/DRDY |
dyson_rey | 0:36c81e7b1032 | 251 | adc.write(0x08);// START |
dyson_rey | 0:36c81e7b1032 | 252 | cs_adc = _DISABLE; // ADC chip select off |
dyson_rey | 0:36c81e7b1032 | 253 | } |
dyson_rey | 0:36c81e7b1032 | 254 | |
dyson_rey | 0:36c81e7b1032 | 255 | void adc_work(void) { |
dyson_rey | 0:36c81e7b1032 | 256 | cs_adc = _ENABLE; |
dyson_rey | 0:36c81e7b1032 | 257 | adc.write(0x10); |
dyson_rey | 0:36c81e7b1032 | 258 | for (int j = 0; j < 3; j++) { |
dyson_rey | 0:36c81e7b1032 | 259 | rch[j] = adc.write(0xff); |
dyson_rey | 0:36c81e7b1032 | 260 | } |
dyson_rey | 0:36c81e7b1032 | 261 | |
dyson_rey | 0:36c81e7b1032 | 262 | data[buff_index][buff_depth].axis = adcsel; |
dyson_rey | 0:36c81e7b1032 | 263 | data[buff_index][buff_depth].acc = (int)((rch[0] << 16) | (rch[1] << 8) | rch[2]); |
dyson_rey | 0:36c81e7b1032 | 264 | data[buff_index][buff_depth].t_acc = utime.read(); |
dyson_rey | 0:36c81e7b1032 | 265 | data[buff_index][buff_depth].t_pulse = t_pulse; |
dyson_rey | 0:36c81e7b1032 | 266 | buff_depth++; |
dyson_rey | 0:36c81e7b1032 | 267 | |
dyson_rey | 0:36c81e7b1032 | 268 | adc.write(0x40);// write register 00h |
dyson_rey | 0:36c81e7b1032 | 269 | switch(adcsel){ |
dyson_rey | 0:36c81e7b1032 | 270 | case 0: |
dyson_rey | 0:36c81e7b1032 | 271 | adc.write(0x91);// AIN1-GND GAIN:1 PGA:off |
dyson_rey | 0:36c81e7b1032 | 272 | adcsel=1; |
dyson_rey | 0:36c81e7b1032 | 273 | break; |
dyson_rey | 0:36c81e7b1032 | 274 | |
dyson_rey | 0:36c81e7b1032 | 275 | case 1: |
dyson_rey | 0:36c81e7b1032 | 276 | adc.write(0xa1);// AIN2-GND GAIN:1 PGA:off |
dyson_rey | 0:36c81e7b1032 | 277 | adcsel=2; |
dyson_rey | 0:36c81e7b1032 | 278 | break; |
dyson_rey | 0:36c81e7b1032 | 279 | |
dyson_rey | 0:36c81e7b1032 | 280 | case 2: |
dyson_rey | 0:36c81e7b1032 | 281 | adc.write(0xb1);// AIN3-GND GAIN:1 PGA:off |
dyson_rey | 0:36c81e7b1032 | 282 | adcsel=3; |
dyson_rey | 0:36c81e7b1032 | 283 | break; |
dyson_rey | 0:36c81e7b1032 | 284 | |
dyson_rey | 0:36c81e7b1032 | 285 | case 3: |
dyson_rey | 0:36c81e7b1032 | 286 | adc.write(0x81);// AIN0-GND GAIN:1 PGA:off |
dyson_rey | 0:36c81e7b1032 | 287 | adcsel=0; |
dyson_rey | 0:36c81e7b1032 | 288 | break; |
dyson_rey | 0:36c81e7b1032 | 289 | } |
dyson_rey | 0:36c81e7b1032 | 290 | |
dyson_rey | 0:36c81e7b1032 | 291 | adc.write(0x08); //start |
dyson_rey | 0:36c81e7b1032 | 292 | cs_adc = _DISABLE; // ADC chip select off |
dyson_rey | 0:36c81e7b1032 | 293 | |
dyson_rey | 0:36c81e7b1032 | 294 | if (buff_depth == BUFF_DEPTH) { |
dyson_rey | 0:36c81e7b1032 | 295 | buff_index++; |
dyson_rey | 0:36c81e7b1032 | 296 | if (buff_index == BUFF_COUNT) { |
dyson_rey | 0:36c81e7b1032 | 297 | buff_index = 0; |
dyson_rey | 0:36c81e7b1032 | 298 | } |
dyson_rey | 0:36c81e7b1032 | 299 | buff_depth = 0; |
dyson_rey | 0:36c81e7b1032 | 300 | write_ok[buff_index] = true; |
dyson_rey | 0:36c81e7b1032 | 301 | } |
dyson_rey | 0:36c81e7b1032 | 302 | } |
dyson_rey | 0:36c81e7b1032 | 303 | /* |
dyson_rey | 0:36c81e7b1032 | 304 | command pin axis |
dyson_rey | 0:36c81e7b1032 | 305 | 81 0 |
dyson_rey | 0:36c81e7b1032 | 306 | 91 1 |
dyson_rey | 0:36c81e7b1032 | 307 | a1 2 |
dyson_rey | 0:36c81e7b1032 | 308 | b1 3 |
dyson_rey | 0:36c81e7b1032 | 309 | */ |
dyson_rey | 0:36c81e7b1032 | 310 | |
dyson_rey | 0:36c81e7b1032 | 311 | void adc_test(void) { |
dyson_rey | 0:36c81e7b1032 | 312 | int zraw; |
dyson_rey | 0:36c81e7b1032 | 313 | cs_adc = _ENABLE; |
dyson_rey | 0:36c81e7b1032 | 314 | adc.write(0x10); |
dyson_rey | 0:36c81e7b1032 | 315 | for (int j = 0; j < 3; j++) { |
dyson_rey | 0:36c81e7b1032 | 316 | rch[j] = adc.write(0xff); |
dyson_rey | 0:36c81e7b1032 | 317 | } |
dyson_rey | 0:36c81e7b1032 | 318 | adc.write(0x08); //start |
dyson_rey | 0:36c81e7b1032 | 319 | cs_adc = _DISABLE; // ADC chip select off |
dyson_rey | 0:36c81e7b1032 | 320 | |
dyson_rey | 0:36c81e7b1032 | 321 | zraw = (int)((rch[0] << 16) | (rch[1] << 8) | rch[2]); |
dyson_rey | 0:36c81e7b1032 | 322 | if (zraw == 0 || zraw == 16777215)status[3] = 1; |
dyson_rey | 0:36c81e7b1032 | 323 | xb.printf("%d,%d\r\n", pot_num, zraw); |
dyson_rey | 0:36c81e7b1032 | 324 | pc.printf("%d,%d\r\n", pot_num, zraw); |
dyson_rey | 0:36c81e7b1032 | 325 | } |
dyson_rey | 0:36c81e7b1032 | 326 | |
dyson_rey | 0:36c81e7b1032 | 327 | inline void pps_rise(void) { |
dyson_rey | 0:36c81e7b1032 | 328 | //__disable_irq(); |
dyson_rey | 0:36c81e7b1032 | 329 | t_pulse = utime.read(); |
dyson_rey | 0:36c81e7b1032 | 330 | //__enable_irq(); |
dyson_rey | 0:36c81e7b1032 | 331 | } |
dyson_rey | 0:36c81e7b1032 | 332 | |
dyson_rey | 0:36c81e7b1032 | 333 | void rx(void) { |
dyson_rey | 0:36c81e7b1032 | 334 | char ch[128]; |
dyson_rey | 0:36c81e7b1032 | 335 | int char_num = 0; |
dyson_rey | 0:36c81e7b1032 | 336 | while((ch[char_num]=xb.getc()) !='\n'){ |
dyson_rey | 0:36c81e7b1032 | 337 | char_num++; |
dyson_rey | 0:36c81e7b1032 | 338 | if(char_num==127){ |
dyson_rey | 0:36c81e7b1032 | 339 | xb.printf("**ERROR**\r\n"); |
dyson_rey | 0:36c81e7b1032 | 340 | break; |
dyson_rey | 0:36c81e7b1032 | 341 | } |
dyson_rey | 0:36c81e7b1032 | 342 | } |
dyson_rey | 0:36c81e7b1032 | 343 | if (ch[0] == 'r') { |
dyson_rey | 0:36c81e7b1032 | 344 | end_proc(); |
dyson_rey | 0:36c81e7b1032 | 345 | }else if(ch[0]=='s'){ |
dyson_rey | 0:36c81e7b1032 | 346 | sscanf(ch, "s,%lf", &start_time); |
dyson_rey | 0:36c81e7b1032 | 347 | //pc.printf("stime:%.0lf\r\n",start_time); |
dyson_rey | 0:36c81e7b1032 | 348 | if(!start){ |
dyson_rey | 0:36c81e7b1032 | 349 | xb.attach(NULL, Serial::RxIrq); |
dyson_rey | 0:36c81e7b1032 | 350 | stat_ticker.detach(); |
dyson_rey | 0:36c81e7b1032 | 351 | sd_init(); |
dyson_rey | 0:36c81e7b1032 | 352 | while(!start){ |
dyson_rey | 0:36c81e7b1032 | 353 | gps_read(0,0); |
dyson_rey | 0:36c81e7b1032 | 354 | } |
dyson_rey | 0:36c81e7b1032 | 355 | status[2] = 1; |
dyson_rey | 0:36c81e7b1032 | 356 | report(); |
dyson_rey | 0:36c81e7b1032 | 357 | utime.start(); |
dyson_rey | 0:36c81e7b1032 | 358 | PPS.rise(pps_rise); |
dyson_rey | 0:36c81e7b1032 | 359 | utime.reset(); |
dyson_rey | 0:36c81e7b1032 | 360 | adc_ticker.attach_us(&adc_work, 833); |
dyson_rey | 0:36c81e7b1032 | 361 | } |
dyson_rey | 0:36c81e7b1032 | 362 | } |
dyson_rey | 0:36c81e7b1032 | 363 | } |
dyson_rey | 0:36c81e7b1032 | 364 | |
dyson_rey | 0:36c81e7b1032 | 365 | void report(void) { |
dyson_rey | 0:36c81e7b1032 | 366 | xb.printf("%d,%d,%d,%d,%d\r\n", pot_num, status[0], status[1], status[2], status[3]); //num,ready,gps lock,measuring,error |
dyson_rey | 0:36c81e7b1032 | 367 | } |
dyson_rey | 0:36c81e7b1032 | 368 | |
dyson_rey | 0:36c81e7b1032 | 369 | int main(void) { |
dyson_rey | 0:36c81e7b1032 | 370 | wait(0.4); |
dyson_rey | 0:36c81e7b1032 | 371 | xb.baud(9600); |
dyson_rey | 0:36c81e7b1032 | 372 | |
dyson_rey | 0:36c81e7b1032 | 373 | data_init(); |
dyson_rey | 0:36c81e7b1032 | 374 | //sd_read(); |
dyson_rey | 0:36c81e7b1032 | 375 | adc_init(); |
dyson_rey | 0:36c81e7b1032 | 376 | stat_ticker.attach(&report,2); |
dyson_rey | 0:36c81e7b1032 | 377 | |
dyson_rey | 0:36c81e7b1032 | 378 | wait(2); |
dyson_rey | 0:36c81e7b1032 | 379 | adc_test(); |
dyson_rey | 0:36c81e7b1032 | 380 | |
dyson_rey | 0:36c81e7b1032 | 381 | //gps_read(0,0);//develop |
dyson_rey | 0:36c81e7b1032 | 382 | gps_read(1,0);//actual use |
dyson_rey | 0:36c81e7b1032 | 383 | xb.attach(&rx, Serial::RxIrq); |
dyson_rey | 0:36c81e7b1032 | 384 | |
dyson_rey | 0:36c81e7b1032 | 385 | led_rgb[1] = 1; |
dyson_rey | 0:36c81e7b1032 | 386 | while(1){ |
dyson_rey | 0:36c81e7b1032 | 387 | while (start) { |
dyson_rey | 0:36c81e7b1032 | 388 | led_rgb[0] = 1; |
dyson_rey | 0:36c81e7b1032 | 389 | if (write_ok[cur_buff]) { |
dyson_rey | 0:36c81e7b1032 | 390 | fp = fopen(filename, "a"); |
dyson_rey | 0:36c81e7b1032 | 391 | if (fp == NULL || kill == _KILLCOUNT) { |
dyson_rey | 0:36c81e7b1032 | 392 | end_proc(); |
dyson_rey | 0:36c81e7b1032 | 393 | } else { |
dyson_rey | 0:36c81e7b1032 | 394 | led_rgb[2] = 1; |
dyson_rey | 0:36c81e7b1032 | 395 | for (int i = 0; i < BUFF_DEPTH; i++) { |
dyson_rey | 0:36c81e7b1032 | 396 | fprintf(fp, "%d,%d,%f,%f\r\n",data[cur_buff][i].axis,data[cur_buff][i].acc, data[cur_buff][i].t_acc, data[cur_buff][i].t_pulse); |
dyson_rey | 0:36c81e7b1032 | 397 | } |
dyson_rey | 0:36c81e7b1032 | 398 | led_rgb[2] = 0; |
dyson_rey | 0:36c81e7b1032 | 399 | write_ok[cur_buff] = false; |
dyson_rey | 0:36c81e7b1032 | 400 | kill++; |
dyson_rey | 0:36c81e7b1032 | 401 | cur_buff++; |
dyson_rey | 0:36c81e7b1032 | 402 | if (cur_buff == BUFF_COUNT)cur_buff = 0; |
dyson_rey | 0:36c81e7b1032 | 403 | if(kill==_KILLCOUNT){ |
dyson_rey | 0:36c81e7b1032 | 404 | gps_read(0,1);//write even if no lock |
dyson_rey | 0:36c81e7b1032 | 405 | end_proc(); |
dyson_rey | 0:36c81e7b1032 | 406 | } |
dyson_rey | 0:36c81e7b1032 | 407 | }//if fp not null |
dyson_rey | 0:36c81e7b1032 | 408 | fclose(fp); |
dyson_rey | 0:36c81e7b1032 | 409 | }//if write_ok is true |
dyson_rey | 0:36c81e7b1032 | 410 | }//start changes to false |
dyson_rey | 0:36c81e7b1032 | 411 | } |
dyson_rey | 0:36c81e7b1032 | 412 | } |