![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
放出機構コントローラ用プログラム
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "BMP180.h" 00003 #include <stdio.h> 00004 #include <stdlib.h> 00005 #include <math.h> 00006 #include <string.h> 00007 00008 00009 I2C bmp(D4, D5); 00010 BMP180 bmp180(&bmp); 00011 Serial xbee(PA_9, PA_10); 00012 I2C i2c(D4, D5); // sda, scl 00013 #include "SSD1306.h" 00014 DigitalOut led(LED1); 00015 DigitalIn button(A1); 00016 DigitalOut led_button(A2); 00017 const int AQCM0802_addr = 0x7C; 00018 00019 unsigned char mode; 00020 unsigned char contrast = 0; // 0-63 00021 unsigned char contrastFlag = false; 00022 int CGcounter; 00023 int FADEcounter; 00024 00025 void lcd_cmd(char x) 00026 { 00027 char data[2]; 00028 data[0] = 0x00; // CO = 0,RS = 0 00029 data[1] = x; 00030 i2c.write(AQCM0802_addr, data, 2); 00031 } 00032 00033 void lcd_contdata(char x) 00034 { 00035 char data[2]; 00036 data[0] = 0xC0; //0b11000000 CO = 1, RS = 1 00037 data[1] = x; 00038 i2c.write(AQCM0802_addr, data, 2); 00039 } 00040 00041 void lcd_lastdata(char x) 00042 { 00043 char data[2]; 00044 data[0] = 0x40; //0b11000000 CO = 0, RS = 1 00045 data[1] = x; 00046 i2c.write(AQCM0802_addr, data, 2); 00047 } 00048 00049 void lcd_printStr(const char *s) 00050 { 00051 while(*s) { 00052 if(*(s + 1)) { 00053 lcd_contdata(*s); 00054 } else { 00055 lcd_lastdata(*s); 00056 } 00057 s++; 00058 } 00059 } 00060 00061 void lcd_printHex(unsigned char num) 00062 { 00063 lcd_contdata(num); 00064 } 00065 00066 void lcd_init() 00067 { 00068 wait(0.04); 00069 // LCD initialize 00070 lcd_cmd(0x38); // function set 00071 lcd_cmd(0x39); // function set 00072 lcd_cmd(0x04); // EntryModeSet 00073 lcd_cmd(0x14); // interval osc 00074 lcd_cmd(0x70 | (contrast & 0xF)); // contrast Low 00075 lcd_cmd(0x5C | ((contrast >> 4) & 0x3)); // contast High/icon/power 00076 lcd_cmd(0x6C); // follower control 00077 wait(0.2); 00078 lcd_cmd(0x38); // function set 00079 lcd_cmd(0x0C); // Display On 00080 lcd_cmd(0x01); // Clear Display 00081 wait(0.2); // need additional wait to Clear Display 00082 } 00083 00084 void lcd_setCursor(unsigned char x,unsigned char y) 00085 { 00086 lcd_cmd(0x80 | (y * 0x40 + x)); 00087 } 00088 00089 void setContrast(unsigned char c) 00090 { 00091 lcd_cmd(0x39); 00092 lcd_cmd(0x70 | (c & 0x0f)); // contrast Low 00093 lcd_cmd(0x5C | ((c >> 4) & 0x03)); // contast High/icon/power 00094 lcd_cmd(0x38); 00095 } 00096 00097 float get_P() 00098 { 00099 float sum = 0.0f; 00100 for(int i=0;i<10;i++){ 00101 bmp180.startPressure(BMP180::ULTRA_LOW_POWER); 00102 wait_ms(10); // Wait for conversion to complete 00103 int pressure; 00104 if(bmp180.getPressure(&pressure) != 0) { 00105 printf("Error getting pressure\n"); 00106 } 00107 sum += pressure; 00108 } 00109 return sum/1000.0f; 00110 } 00111 00112 float get_T() 00113 { 00114 float sum = 0.0f; 00115 for(int i=0;i<10;i++){ 00116 bmp180.startTemperature(); 00117 wait_ms(5); // Wait for conversion to complete 00118 float temp; 00119 if(bmp180.getTemperature(&temp) != 0) { 00120 printf("Error getting temperature\n"); 00121 } 00122 sum += temp; 00123 } 00124 return sum/10.0f; 00125 } 00126 00127 00128 int main() 00129 { 00130 ssd1306_init(); 00131 ssd1306_init_meter_display(); 00132 button.mode(PullUp); 00133 while(1) { 00134 if (bmp180.init() != 0) { 00135 printf("Error communicating with BMP180\n"); 00136 } else { 00137 printf("Initialized BMP180\n"); 00138 break; 00139 } 00140 wait(1); 00141 } 00142 00143 char buf[64] = {}; 00144 int buf_count = 0; 00145 float pressure, temp, ground_P, ground_T, height; 00146 bool init = false; 00147 char str_lcd[15]; 00148 char strtmp[15]; 00149 int n; 00150 lcd_init(); 00151 lcd_setCursor(0,0); 00152 lcd_printStr("Hello"); 00153 lcd_setCursor(3,1); 00154 lcd_printStr("SSSRC"); 00155 contrast = 35; 00156 setContrast(contrast); 00157 while(1) { 00158 int timer = 0; 00159 int bb = button; 00160 bool deploy = false; 00161 while(!bb){ 00162 deploy = true; 00163 led_button = 1; 00164 if(timer == 0){ 00165 lcd_setCursor(0,0); 00166 lcd_printStr("55555555"); 00167 lcd_setCursor(0,1); 00168 lcd_printStr("55555555"); 00169 contrast = 35; 00170 }else if(timer == 10){ 00171 lcd_setCursor(0,0); 00172 lcd_printStr("44444444"); 00173 lcd_setCursor(0,1); 00174 lcd_printStr("44444444"); 00175 contrast = 35; 00176 }else if(timer == 20){ 00177 lcd_setCursor(0,0); 00178 lcd_printStr("33333333"); 00179 lcd_setCursor(0,1); 00180 lcd_printStr("33333333"); 00181 contrast = 35; 00182 }else if(timer == 30){ 00183 lcd_setCursor(0,0); 00184 lcd_printStr("22222222"); 00185 lcd_setCursor(0,1); 00186 lcd_printStr("22222222"); 00187 contrast = 35; 00188 }else if(timer == 40){ 00189 lcd_setCursor(0,0); 00190 lcd_printStr("11111111"); 00191 lcd_setCursor(0,1); 00192 lcd_printStr("11111111"); 00193 contrast = 35; 00194 }else if(timer>=50){ 00195 xbee.printf("O"); 00196 lcd_setCursor(0,0); 00197 lcd_printStr("CanSat "); 00198 lcd_setCursor(0,1); 00199 lcd_printStr(" Deploy!"); 00200 contrast = 35; 00201 } 00202 wait(0.1); 00203 timer++; 00204 bb = button; 00205 } 00206 if(deploy){ 00207 lcd_setCursor(0,0); 00208 lcd_printStr(" "); 00209 lcd_setCursor(0,1); 00210 lcd_printStr(" "); 00211 contrast = 35; 00212 } 00213 led_button = 0; 00214 //buf[0] = '0'; 00215 int nosignal = 0; 00216 bool push = false; 00217 while(xbee.readable()==0) { 00218 xbee.printf("G"); 00219 nosignal++; 00220 bb = button; 00221 if(nosignal == 5000){ 00222 lcd_setCursor(0,0); 00223 lcd_printStr("NoSignal"); 00224 lcd_setCursor(0,1); 00225 lcd_printStr(" "); 00226 contrast = 35; 00227 }else if(!bb && nosignal<5000){ 00228 push = true; 00229 break; 00230 } 00231 } 00232 if(push){ 00233 printf("1"); 00234 continue; 00235 } 00236 while(1) { 00237 int c = xbee.getc(); 00238 //printf("%c",c); 00239 if(buf_count == 0 && c != 'P') { 00240 continue; 00241 } 00242 buf[buf_count] = c; 00243 buf_count++; 00244 if(c == '\n') { 00245 buf[buf_count] = '\0'; 00246 printf("buf:%s\r\n",buf); 00247 sscanf(buf,"PP%fT%f\n", &pressure, &temp); 00248 pressure += 1.77f; 00249 buf_count = 0; 00250 while(xbee.readable()==1) { 00251 c = xbee.getc(); 00252 } 00253 push = false; 00254 for(int i=0;i<8;i++){ 00255 bb = button; 00256 if(!bb){ 00257 led_button = 1; 00258 push = true; 00259 break; 00260 } 00261 wait(0.1); 00262 } 00263 if(push){ 00264 printf("2"); 00265 break; 00266 } 00267 led = 1; 00268 for(int i=0;i<2;i++){ 00269 bb = button; 00270 if(!bb){ 00271 led_button = 1; 00272 push = true; 00273 break; 00274 } 00275 wait(0.1); 00276 led = !led; 00277 } 00278 led = 0; 00279 if(push){ 00280 printf("3"); 00281 break; 00282 } 00283 ground_P = get_P(); 00284 ground_T = get_T(); 00285 //height = ( pow(ground_P/pressure, 1/5.257f)-1 )*(temp+273.15f)/0.0065f; 00286 height = 290*((temp+ground_T)/2+273.15f)/9.80665f*log(ground_P/pressure); 00287 if(init) { 00288 printf("P0=%f P=%f T=%f B-G=%f\r\n--------------------\r\n",ground_P,pressure,temp,pressure-ground_P); 00289 if(height<-100 or height>1000) continue; 00290 if(pressure-ground_P>0) height=0.0; 00291 sprintf(str_lcd,"%.1fhPa",ground_P-pressure); 00292 n=8-strlen(str_lcd); 00293 for(int i=0; i<n; i++) { 00294 sprintf(strtmp," %s",str_lcd); 00295 sprintf(str_lcd,"%s",strtmp); 00296 } 00297 lcd_setCursor(0,0); 00298 lcd_printStr(str_lcd); 00299 printf("%s\r\n",str_lcd); 00300 00301 sprintf(str_lcd,"%.1f""\xf2""C",ground_T); 00302 n=8-strlen(str_lcd); 00303 for(int i=0; i<n; i++) { 00304 sprintf(strtmp," %s",str_lcd); 00305 sprintf(str_lcd,"%s",strtmp); 00306 } 00307 00308 lcd_setCursor(0,1); 00309 lcd_printStr(str_lcd); 00310 contrast = 35; 00311 setContrast(contrast); 00312 printf("%s\r\n\r\n",str_lcd); 00313 printf("height=%f m\r\n",height); 00314 ssd1306_draw_meter( (int)height ); 00315 } 00316 init = true; 00317 printf("P0=%f P=%f T=%f B-G=%f\r\n--------------------\r\n",ground_P,pressure,temp,pressure-ground_P); 00318 } 00319 break; 00320 } 00321 } 00322 }
Generated on Fri Jul 15 2022 22:31:55 by
![doxygen](doxygen.png)