k64f_OneNET
Dependencies: EthernetInterface FXOS8700Q eCompass_FPU_Lib mbed-rtos mbed mbed
Fork of K64F_eCompass by
main.cpp
00001 #include "mbed.h" 00002 #include "FXOS8700Q.h" 00003 #include "eCompass_Lib.h" 00004 #include "rtos.h" 00005 //#include "MotionSensorDtypes.h" 00006 #include "EthernetInterface.h" 00007 00008 00009 FXOS8700Q_acc acc( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); 00010 FXOS8700Q_mag mag( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); 00011 Serial pc(USBTX, USBRX); 00012 eCompass compass; 00013 DigitalOut red(LED_RED); //debug led 00014 DigitalOut green(LED_GREEN); //debug ledeCompass compass; 00015 00016 //void calibrate_thread(void const *argument); 00017 //void print_thread(void const *argument); 00018 //void compass_thread(void const *argument); 00019 00020 extern axis6_t axis6; 00021 extern uint32_t seconds; 00022 extern uint32_t compass_type; // optional, NED compass is default 00023 extern int32_t tcount; 00024 extern uint8_t cdebug; 00025 int l = 0; 00026 volatile int sflag = 0; 00027 00028 char http_cmd[500]; 00029 char devid[] = "1100227"; 00030 char api_key[] ="Jl4VyAUUG6j4Qdms3ZyOjbAu6J8="; 00031 char host[] = "api.heclouds.com"; 00032 00033 EthernetInterface eth; //create ethernet 00034 TCPSocketConnection sock; 00035 00036 MotionSensorDataCounts mag_raw; 00037 MotionSensorDataCounts acc_raw; 00038 00039 00040 void hal_map( MotionSensorDataCounts * acc_raw, MotionSensorDataCounts * mag_raw) 00041 { 00042 int16_t t; 00043 // swap and negate X & Y axis 00044 t = acc_raw->x; 00045 acc_raw->x = acc_raw->y * -1; 00046 acc_raw->y = t * -1; 00047 // swap mag X & Y axis 00048 t = mag_raw->x; 00049 mag_raw->x = mag_raw->y; 00050 mag_raw->y = t; 00051 // negate mag Z axis 00052 mag_raw->z *= -1; 00053 } 00054 00055 void creat_cmd() 00056 { 00057 char tmp[30]; 00058 char str[100]; 00059 00060 sprintf(str,",;roll,%d;pitch,%d;yaw,%d", axis6.roll, axis6.pitch, axis6.yaw); 00061 http_cmd[0] = 0; 00062 strcat(http_cmd,"POST /devices/"); 00063 strcat(http_cmd,devid); 00064 strcat(http_cmd,"/datapoints?type=5 HTTP/1.1\r\n"); 00065 strcat(http_cmd,"api-key:"); 00066 strcat(http_cmd,api_key); 00067 strcat(http_cmd,"\r\n"); 00068 strcat(http_cmd,"Host:"); 00069 strcat(http_cmd,host); 00070 strcat(http_cmd,"\r\n"); 00071 sprintf(tmp,"Content-Length:%d\r\n\r\n", strlen(str)); 00072 strcat(http_cmd,tmp); 00073 strcat(http_cmd,str); 00074 } 00075 00076 // 00077 // Print data values for debug 00078 // 00079 void debug_print(void) 00080 { 00081 // Some useful printf statements for debug 00082 printf("roll=%d, pitch=%d, yaw=%d\r\n", axis6.roll, axis6.pitch, axis6.yaw); 00083 printf("Acc: X= %2.3f Y= %2.3f Z= %2.3f ", axis6.fGax, axis6.fGay, axis6.fGaz); 00084 printf("Mag: X= %4.1f Y= %4.1f Z= %4.1f\r\n\r\n", axis6.fUTmx, axis6.fUTmy, axis6.fUTmz); 00085 printf("Quaternion: Q0= %1.4f Q1= %1.4f Q2= %1.4f Q3= %1.4f\r\n\r\n", axis6.q0, axis6.q1, axis6.q2, axis6.q3); 00086 } 00087 00088 00089 void compass_thread(void const *argument) 00090 { 00091 // get raw data from the sensors 00092 acc.getAxis( acc_raw); 00093 mag.getAxis( mag_raw); 00094 if(tcount) compass.run( acc_raw, mag_raw); // calculate the eCompass 00095 if(l++ >= 50) 00096 { 00097 // take car of business once a second 00098 seconds++; 00099 sflag = 1; 00100 compass.calibrate(); 00101 debug_print(); 00102 l = 0; 00103 00104 if(sock.is_connected()) 00105 { 00106 green = 0; 00107 red = 1; 00108 creat_cmd(); 00109 sock.send(http_cmd, strlen(http_cmd)); 00110 //printf("%s\r\n",http_cmd); 00111 } 00112 else 00113 { 00114 green = 1; 00115 red = 0; 00116 sock.close(); 00117 00118 wait(3); 00119 00120 printf("IP Address is %s\r\n", eth.getIPAddress()); 00121 sock.connect("api.heclouds.com", 80); //connetct to OneNET 00122 00123 printf("reconnect the server\r\n"); 00124 while(!sock.is_connected()); 00125 } 00126 00127 } 00128 tcount++; 00129 } 00130 00131 00132 int main() 00133 { 00134 RtosTimer compass_timer(compass_thread, osTimerPeriodic); 00135 00136 /* ethernet init */ 00137 eth.init(); //DHCP 00138 eth.connect(); 00139 printf("IP Address is %s\r\n", eth.getIPAddress()); 00140 sock.connect("api.heclouds.com", 80); //connetct to OneNET 00141 00142 printf("\r\n\n\n\n\n\n\n"); 00143 printf("Who AM I= %X\r\n", acc.whoAmI()); 00144 acc.enable(); 00145 00146 acc.getAxis( acc_raw); 00147 mag.getAxis( mag_raw); 00148 00149 compass_timer.start(20); // Run the Compass every 20ms 00150 while(1) 00151 { 00152 //Thread::wait(osWaitForever); 00153 /**********receive response data*******************/ 00154 char buffer[300]; 00155 int ret = 0; 00156 buffer[0] = '\0'; 00157 if(sock.is_connected()) 00158 { 00159 ret = sock.receive(buffer, sizeof(buffer)-1); 00160 if(ret >= 0) 00161 { 00162 buffer[ret] = '\0'; 00163 } 00164 //printf("Received %d chars from server:\r\n%s\r\n", ret, buffer); 00165 } 00166 else 00167 { 00168 wait(3); 00169 } 00170 } 00171 }
Generated on Thu Jul 14 2022 03:04:58 by 1.7.2