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.
Dependencies: h3lis331dl libmDot mbed-rtos mbed-src
main.cpp
00001 // ---------------- 00002 // CTIA Helmet Demo 00003 // ---------------- 00004 00005 #include "mbed.h" 00006 #include "mDot.h" 00007 #include "rtos.h" 00008 #include <H3LIS331DL.h> 00009 #include <string> 00010 #include <vector> 00011 00012 #define G_THRESHOLD 5 00013 00014 //please get these value by running H3LIS331DL_AdjVal 00015 #define VAL_X_AXIS 71 //68 00016 #define VAL_Y_AXIS 97 //38 00017 #define VAL_Z_AXIS 722 //664 00018 #define GAIN 0.003 00019 00020 // these options must match the settings on your Conduit 00021 static std::string config_network_name = "Escalation"; 00022 static std::string config_network_pass = "Escalation"; 00023 static uint8_t config_frequency_sub_band = 3; 00024 00025 //Globals 00026 mDot* dot; 00027 H3LIS331DL* h3lis=new H3LIS331DL(I2C_SDA, I2C_SCL); 00028 DigitalOut led(LED1); 00029 Ticker ledTick; 00030 Ticker heart; 00031 Timer timer; 00032 volatile bool timeToSendHeartbeat = true; 00033 00034 //Function prototypes 00035 void log_error(mDot* dot, const char* msg, int32_t retval); 00036 bool setFrequencySubBand(uint8_t subBand); 00037 bool setNetworkName(const std::string name); 00038 bool setNetworkPassphrase(const std::string passphrase); 00039 bool setAck(uint8_t retries); 00040 bool joinNetwork(); 00041 bool sendData(const std::string text); 00042 H3LIS331DL* rebuild(H3LIS331DL* h3lis ); 00043 00044 00045 void ledTock() { 00046 led = !led; 00047 } 00048 00049 void beat() { 00050 timeToSendHeartbeat = true; 00051 } 00052 int begin; 00053 00054 00055 int main() { 00056 int32_t ret; 00057 unsigned char id; 00058 std::vector<uint8_t> data; 00059 char data_str[40]; 00060 int16_t x=0, y=0, z=0, x_max=0, y_max=0, z_max=0; 00061 uint16_t vector_sum=0, vector_sum_max=0; 00062 00063 ledTick.attach(&ledTock, 0.1); 00064 00065 // get the mDot handle 00066 dot = mDot::getInstance(); 00067 00068 00069 // reset to default config so we know what state we're in 00070 dot->resetConfig(); 00071 dot->getDeviceId(); 00072 00073 printf("\r\n\r\n"); 00074 printf("=======================\r\n"); 00075 printf(" CTIA Helment Demo\r\n"); 00076 printf("=======================\r\n"); 00077 00078 printf("Library version: %s\r\n\r\n", dot->getId().c_str()); 00079 00080 // set up the mDot with our network information 00081 setFrequencySubBand(config_frequency_sub_band); 00082 setNetworkName(config_network_name); 00083 setNetworkPassphrase(config_network_pass); 00084 setAck(0); //disable acks 00085 00086 printf("Initializing accelerometer...\r\n"); 00087 // H3LIS331DL_FULLSCALE_4 = +- 200g, H3LIS331DL_FULLSCALE_2 = +- 100g 00088 h3lis->init(H3LIS331DL_ODR_1000Hz, H3LIS331DL_NORMAL, H3LIS331DL_FULLSCALE_2); 00089 // h3lis.init(H3LIS331DL_ODR_100Hz, H3LIS331DL_NORMAL, H3LIS331DL_FULLSCALE_2); 00090 00091 00092 if (!h3lis->getWHO_AM_I(&id)) { 00093 printf("Failed to initialize accelerometer!\r\n"); 00094 return 0; 00095 } 00096 00097 // Join network 00098 while (!joinNetwork()) { wait(5); }; 00099 ledTick.detach(); 00100 led = 1; 00101 00102 heart.attach(&beat, 10); 00103 timer.start(); 00104 while (true) { 00105 begin = timer.read_us(); 00106 00107 h3lis->readXYZ(&x,&y,&z); 00108 x = (x - VAL_X_AXIS) * GAIN; 00109 y = (y - VAL_Y_AXIS) * GAIN; 00110 z = (z - VAL_Z_AXIS) * GAIN; 00111 00112 // printf takes about 50 ms ! 00113 // printf("Reading: X:% d Y:% d Z:% d, timer: % d\r\n", x, y, z, begin); 00114 00115 vector_sum = sqrt((double)(x*x + y*y + z*z)); 00116 00117 // If impact occuring 00118 if (vector_sum > G_THRESHOLD) { 00119 //Update peak forces if needed 00120 if (vector_sum > vector_sum_max) { 00121 vector_sum_max = vector_sum; 00122 x_max = x; 00123 y_max = y; 00124 z_max = z; 00125 } 00126 printf("Reading: X:% d Y:% d Z:% d\r\n", x, y, z); 00127 } 00128 // If impact detected 00129 else if (vector_sum_max > 0) { 00130 printf("IMPACT: X:% d Y:% d Z:% d\r\n", x_max, y_max, z_max); 00131 sprintf(data_str, "%d,%d,%d", x_max, y_max, z_max); 00132 00133 // send the data 00134 sendData(data_str); 00135 00136 // Reset maximums 00137 vector_sum_max = 0; 00138 x_max = 0; 00139 y_max = 0; 00140 z_max = 0; 00141 timeToSendHeartbeat = false; 00142 } 00143 //Idle 00144 else { 00145 if (timeToSendHeartbeat) { 00146 sendData("heartbeat"); 00147 timeToSendHeartbeat = false; 00148 } 00149 } 00150 wait_ms(10); //Accelerometer reading updates every 1ms 00151 } 00152 00153 return 0; 00154 } 00155 00156 void log_error(mDot* dot, const char* msg, int32_t retval) { 00157 printf("%s - %ld:%s, %s\r\n", msg, retval, mDot::getReturnCodeString(retval).c_str(), dot->getLastError().c_str()); 00158 } 00159 00160 bool setFrequencySubBand(uint8_t subBand) 00161 { 00162 int32_t ret; 00163 printf("Setting frequency sub band to '%d'...\r\n", subBand); 00164 if ((ret = dot->setFrequencySubBand(subBand)) != mDot::MDOT_OK) { 00165 log_error(dot, "Failed to set frequency sub band", ret); 00166 return false; 00167 } 00168 return true; 00169 } 00170 00171 bool setNetworkName(const std::string name) 00172 { 00173 int32_t ret; 00174 printf("Setting network name to '%s'...\r\n", name.c_str()); 00175 if ((ret = dot->setNetworkName(name)) != mDot::MDOT_OK) { 00176 log_error(dot, "Failed to set network name", ret); 00177 return false; 00178 } 00179 return true; 00180 } 00181 00182 bool setNetworkPassphrase(const std::string passphrase) 00183 { 00184 int32_t ret; 00185 printf("Setting passphrase...\r\n"); 00186 if ((ret = dot->setNetworkPassphrase(passphrase)) != mDot::MDOT_OK) { 00187 log_error(dot, "Failed to set network password", ret); 00188 return false; 00189 } 00190 return true; 00191 } 00192 00193 bool setAck(uint8_t retries) 00194 { 00195 int32_t ret; 00196 printf("Setting ack...\r\n"); 00197 if ((ret = dot->setAck(retries)) != mDot::MDOT_OK) 00198 { 00199 log_error(dot, "Failed to set ack", ret); 00200 return false; 00201 } 00202 return true; 00203 } 00204 00205 bool joinNetwork() 00206 { 00207 int32_t ret; 00208 printf("\r\nJoining network... "); 00209 // reset to default config so we know what state we're in 00210 00211 00212 if ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { 00213 log_error(dot, "Failed", ret); 00214 dot->resetConfig(); 00215 dot->getDeviceId(); 00216 osDelay(200); 00217 return false; 00218 } 00219 printf("Network Joined!\r\n"); 00220 return true; 00221 } 00222 00223 bool sendData(const std::string text) 00224 { 00225 if (dot->getNextTxMs() != 0) { 00226 printf("Sending in %lu ms...\r\n", dot->getNextTxMs()); 00227 return false; 00228 } 00229 00230 int32_t ret; 00231 printf("Sending: '%s' ", text.c_str()); 00232 std::vector<uint8_t> data(text.begin(), text.end()); 00233 if ((ret = dot->send(data, 1)) != mDot::MDOT_OK) 00234 { 00235 log_error(dot, "Failed to send data", ret); 00236 return false; 00237 } 00238 printf("Data sent!\r\n"); 00239 return true; 00240 } 00241 00242 H3LIS331DL* rebuild(H3LIS331DL* h3lis ){ 00243 H3LIS331DL* new_h3lis = new H3LIS331DL( I2C_SDA, I2C_SCL ); 00244 delete h3lis; 00245 new_h3lis->init(H3LIS331DL_ODR_1000Hz, H3LIS331DL_NORMAL, H3LIS331DL_FULLSCALE_2); 00246 00247 return new_h3lis; 00248 } 00249 00250
Generated on Wed Jul 13 2022 13:00:08 by
1.7.2