Ok, In this Blog I will talk about implementation of the Sensor Subsystem in My project.
Firstly the Role of Sensor Subsystem was to identify the weather for ride and to send it to the User. I had Dropped the Idea of pothole detection as it was not a worth Idea, moreover In my Nextion display You could just touch to report the pothole. see the BUMP Button below !!
As there was a mBed Library available online HERE as well as an Example Program available HERE it was easy to get started with the Library and implement the rest.
Now I would like to post my Implementation code snippet
Some Important defn
const static float seaLevelPressure = 1013.25; const static float declinationAngle = 0.003839724; float alt, LPS22HB_p, LPS22HB_p_previous, LPS22HB_t, HTS221_h, HTS221_t; float angle; int32_t LSM303AGR_a[3],LSM303AGR_m[3]; int32_t LSM6DSL_a[3],LSM6DSL_g[3];
Message Banks to be displayed on the Nextion display
// Message Bank // To /* Var Bank Msg * greatingMessages 1 0-2 * pressureMessages 2 0-1 * ridingMessages 3 0-2 * weatherMessages 4 0-5 */ const char* greatingMessages[3] = { "Good Morning", "Good Evening", "Good Day" }; const char* pressureMessages[2] = { "Up Up Up the hill we go", "Down we goooooo.... Yeeaahhh....." }; const char* ridingMessages[3] = { "Com'on its a Pleasent Fine AWESOME day :)", "Hey Its a Holiday ! I will pickup dirt :(", "I know you are sleeping :| " }; const char* weatherMessages[6] = { "Its pleasent to ride bike :)", "Its not to bad :) a bit Humid :)", "Its HUMID !! :|", "Not Today, Its very Humid :\\", "Its Chilling :O", "Its Hot :<" };
Obj Creation
/* Instantiate the expansion board */ static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); /* Retrieve the composing elements of the expansion board */ static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer; static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor; static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro; static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer;
Function called to update Messages based on Sensor Values
void updateMessage(void){ hum_temp->get_temperature(&HTS221_t); hum_temp->get_humidity(&HTS221_h); // printf("HTS221: [temp] %7s C, [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2)); // Use this in another function if(HTS221_h < 70){ messageInUse=0; }else if(HTS221_h > 70 && HTS221_h < 80){ messageInUse=1; }else if(HTS221_h > 80 && HTS221_h < 90){ messageInUse=2; } else{ messageInUse=3; } if(HTS221_t < 20){ messageInUse=4; }else if(HTS221_t > 20 && HTS221_t < 35){ messageInUse=0; }else{ messageInUse=5; } press_temp->get_temperature(&LPS22HB_t); press_temp->get_pressure(&LPS22HB_p); //printf("LPS22HB: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2)); if(LPS22HB_p_previous > LPS22HB_p){ messageInUse=0; }else{ messageInUse=1; } if(LPS22HB_t < 20){ messageInUse=4; }else if(LPS22HB_t > 20 && LPS22HB_t < 35){ messageInUse=0; }else{ messageInUse=5; } //#ifdef DEBUG nextion.printf("%s\n",weatherMessages[messageInUse]); //#endif }
inside Perodic Function to gather sensor data periodically
#ifdef DEBUG nextion.printf("Speedy Calc Over ---- \n Sensor Data Polling Begin\n"); #endif hum_temp->get_temperature(&HTS221_t); hum_temp->get_humidity(&HTS221_h); #ifdef DEBUG printf("HTS221: [temp] %7s C, [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2)); #endif press_temp->get_temperature(&LPS22HB_t); press_temp->get_pressure(&LPS22HB_p_previous); //wait(5); #ifdef DEBUG printf("LPS22HB: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2)); printf("---\r\n"); #endif magnetometer->get_m_axes(LSM303AGR_m); #ifdef DEBUG printf("LSM303AGR [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); #endif accelerometer->get_x_axes(LSM303AGR_a); #ifdef DEBUG printf("LSM303AGR [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); #endif acc_gyro->get_x_axes(LSM6DSL_a); #ifdef DEBUG printf("LSM6DSL [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); #endif acc_gyro->get_g_axes(LSM6DSL_g); #ifdef DEBUG printf("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); #endif //Altitude computation based on http://keisan.casio.com/exec/system/1224585971 formula alt = pow((double)(seaLevelPressure/LPS22HB_p),(double)(1/5.257)) - 1; alt = alt * (LPS22HB_t + 273.15); alt = alt / 0.0065; angle -= declinationAngle; // Correct for when signs are reversed. if(angle < 0){ angle += 2*M_PI; } // Check for wrap due to addition of declination. if(angle > 2*M_PI){ angle -= 2*M_PI; } angle = angle * 180/M_PI; updateMessage();
Hope you found this SHORT UPDATE useful
If you want more information please comment and I will include it in the blog
Thanks for Reading
Regards,
GS Gill