Training Class with ROHM Sensor Board and LoRa mDot
Dependencies: MbedJSONValue libmDot mbed-rtos mbed
main.cpp
00001 /************************************************************************* 00002 * Originally this was a 00003 * Dragonfly Example program for 2015 AT&T Government Solutions Hackathon 00004 * 00005 * This is in process of being convertered to a mDot processor. mDot has a 00006 * limited set of IO that are available to the ROHM board. Most of the 00007 * Sensors will be used but the ones that can't have been commented out. 00008 * 00009 * The following hardware is required to successfully run this program: 00010 * - MultiTech UDK2 (4" square white PCB with Arduino headers, antenna 00011 * connector, micro USB ports, and 40-pin connector for Dragonfly) 00012 * - MultiTech mDot with a LoRa radio 00013 * - Seeed Studio Base Shield to elevate the ROHM board connectors away from mDOt 00014 * - MEMs Inertial and Environmental Nucleo Expansion board (LSM6DS0 00015 * 3-axis accelerometer + 3-axis gyroscope, LIS3MDL 3-axis 00016 * magnetometer, HTS221 humidity and temperature sensor and LPS25HB 00017 * pressure sensor) 00018 * 00019 * What this program does: 00020 * - reads data from all sensors on MEMs board and moisture sensor on a 00021 * periodic basis 00022 * - prints all sensor data to debug port on a periodic basis 00023 * - optionally sends LoRa sensor data when the timer expires 00024 * THis needs to be written yet. 00025 * - optionally sends sensor data to AT&T M2X cloud platform (user must 00026 * create own M2X account and configure a device) 00027 * - you need to set the "m2x_api_key" field and the "m2x_device_id" 00028 * field based on your M2X account for this to work 00029 * - you need to set the "do_cloud_post" flag to true for this to 00030 * work 00031 * 00032 * Setup: 00033 * - Seat the mDot on the UDK2 board 00034 * - Stack the Base Shield on the UDK2 Arduino headers 00035 * - Make sure the reference voltage selector switch (next to the A0 00036 * socket) is switched to 3.3V so you don't blow the mDot analog converter 00037 * accuracy will suffer as a result when compared to 5V. 00038 * - Stack the MEMs board on top of the Base Shield 00039 * - Plug in the power cable 00040 * - Plug a micro USB cable away from the multiple LED String 00041 * 00042 * Go have fun and make something cool! 00043 * 00044 ************************************************************************/ 00045 /* 00046 Sample Program Description: 00047 This Program will enable to Multi-Tech mDot platform to utilize ROHM's Multi-sensor Shield Board. 00048 This program will initialize most of the sensors on the shield and then read back the sensor data. 00049 Data will then be output to the UART Debug Terminal every 1 second. 00050 00051 Sample Program Author: 00052 ROHM USDC 00053 00054 Additional Resources: 00055 ROHM Sensor Shield GitHub Repository: https://github.com/ROHMUSDC/ROHM_SensorPlatform_Multi-Sensor-Shield 00056 */ 00057 00058 00059 00060 #include "mbed.h" 00061 #include "MbedJSONValue.h" 00062 #include <string> 00063 00064 // added the following help files for a mDot not required for Dragonfly. 00065 #include "mDot.h" 00066 #include "MTSLog.h" 00067 #include <vector> 00068 #include <algorithm> 00069 #include "rtos.h" 00070 00071 00072 // Debug serial port 00073 static Serial debug(USBTX, USBRX); 00074 00075 00076 // variables for sensor data 00077 float temp_celsius; 00078 float humidity_percent; 00079 float pressure_mbar; 00080 float moisture_percent; 00081 int32_t mag_mgauss[3]; 00082 int32_t acc_mg[3]; 00083 int32_t gyro_mdps[3]; 00084 00085 // misc variables 00086 static char wall_of_dash[] = "--------------------------------------------------"; 00087 bool radio_ok = false; 00088 static int thpm_interval_ms = 5000; 00089 static int motion_interval_ms = 5000; 00090 static int print_interval_ms = 5000; 00091 static int sms_interval_ms = 5000; 00092 int debug_baud = 115200; 00093 00094 00095 /**************************************************************************************************** 00096 00097 ****************************************************************************************************/ 00098 00099 //Macros for checking each of the different Sensor Devices 00100 #define AnalogTemp //BDE0600 00101 // #define AnalogUV //ML8511 // analog pin A4 on Arduino connector is not connected to the mDot on the UDK. 00102 #define HallSensor //BU52011 00103 #define RPR0521 //RPR0521 00104 #define KMX62 //KMX61, Accel/Mag 00105 #define COLOR //BH1745 00106 #define KX022 //KX022, Accel Only 00107 #define Pressure //BM1383 00108 #define SMS //allow SMS messaging now sending LORA!!!! 00109 //#define Web //allow M2X communication 00110 00111 00112 //Define Pins for I2C Interface 00113 I2C i2c(I2C_SDA, I2C_SCL); 00114 bool RepStart = true; 00115 bool NoRepStart = false; 00116 00117 //Define Sensor Variables 00118 #ifdef AnalogTemp 00119 AnalogIn BDE0600_Temp(PC_1); //Mapped to A2 pin 15 on the mDot 00120 uint16_t BDE0600_Temp_value; 00121 float BDE0600_output; 00122 #endif 00123 00124 //#ifdef AnalogUV // analog pin A4 on Arduino connector is not connected to the mDot on the UDK. 00125 //AnalogIn ML8511_UV(PA_7); //Mapped to A4 not a pin routed on the UDK to the mDot 00126 //uint16_t ML8511_UV_value; 00127 //float ML8511_output; 00128 //#endif 00129 00130 #ifdef HallSensor 00131 DigitalIn Hall_GPIO0(PA_4); // assigned to D10 on Arduino, mapped to pin 17 on mDot 00132 DigitalIn Hall_GPIO1(PA_7); // assigned to D11 on Arduino, mapped to pin 11 on mDot 00133 int Hall_Return1; 00134 int Hall_Return0; 00135 int32_t Hall_Return[2]; 00136 #endif 00137 00138 #ifdef RPR0521 00139 int RPR0521_addr_w = 0x70; //7bit addr = 0x38, with write bit 0 00140 int RPR0521_addr_r = 0x71; //7bit addr = 0x38, with read bit 1 00141 char RPR0521_ModeControl[2] = {0x41, 0xE6}; 00142 char RPR0521_ALSPSControl[2] = {0x42, 0x03}; 00143 char RPR0521_Persist[2] = {0x43, 0x20}; 00144 char RPR0521_Addr_ReadData = 0x44; 00145 char RPR0521_Content_ReadData[6]; 00146 int RPR0521_PS_RAWOUT = 0; //this is an output 00147 float RPR0521_PS_OUT = 0; 00148 int RPR0521_ALS_D0_RAWOUT = 0; 00149 int RPR0521_ALS_D1_RAWOUT = 0; 00150 float RPR0521_ALS_DataRatio = 0; 00151 float RPR0521_ALS_OUT = 0; //this is an output 00152 float RPR0521_ALS[2]; // is this ok taking an int to the [0] value and float to [1]??????????? 00153 #endif 00154 00155 #ifdef KMX62 00156 int KMX62_addr_w = 0x1C; //7bit addr = 0x38, with write bit 0 00157 int KMX62_addr_r = 0x1D; //7bit addr = 0x38, with read bit 1 00158 char KMX62_CNTL2[2] = {0x3A, 0x5F}; 00159 char KMX62_Addr_Accel_ReadData = 0x0A; 00160 char KMX62_Content_Accel_ReadData[6]; 00161 char KMX62_Addr_Mag_ReadData = 0x10; 00162 char KMX62_Content_Mag_ReadData[6]; 00163 short int MEMS_Accel_Xout = 0; 00164 short int MEMS_Accel_Yout = 0; 00165 short int MEMS_Accel_Zout = 0; 00166 double MEMS_Accel_Conv_Xout = 0; 00167 double MEMS_Accel_Conv_Yout = 0; 00168 double MEMS_Accel_Conv_Zout = 0; 00169 00170 short int MEMS_Mag_Xout = 0; 00171 short int MEMS_Mag_Yout = 0; 00172 short int MEMS_Mag_Zout = 0; 00173 float MEMS_Mag_Conv_Xout = 0; 00174 float MEMS_Mag_Conv_Yout = 0; 00175 float MEMS_Mag_Conv_Zout = 0; 00176 00177 double MEMS_Accel[3]; 00178 float MEMS_Mag[3]; 00179 #endif 00180 00181 #ifdef COLOR 00182 int BH1745_addr_w = 0x72; //write 00183 int BH1745_addr_r = 0x73; //read 00184 char BH1745_persistence[2] = {0x61, 0x03}; 00185 char BH1745_mode1[2] = {0x41, 0x00}; 00186 char BH1745_mode2[2] = {0x42, 0x92}; 00187 char BH1745_mode3[2] = {0x43, 0x02}; 00188 char BH1745_Content_ReadData[6]; 00189 char BH1745_Addr_color_ReadData = 0x50; 00190 int BH1745_Red; 00191 int BH1745_Blue; 00192 int BH1745_Green; 00193 int32_t BH1745[3]; //Red, Blue Green matrix 00194 #endif 00195 00196 #ifdef KX022 00197 int KX022_addr_w = 0x3C; //write 00198 int KX022_addr_r = 0x3D; //read 00199 char KX022_Accel_CNTL1[2] = {0x18, 0x41}; 00200 char KX022_Accel_ODCNTL[2] = {0x1B, 0x02}; 00201 char KX022_Accel_CNTL3[2] = {0x1A, 0xD8}; 00202 char KX022_Accel_TILT_TIMER[2] = {0x22, 0x01}; 00203 char KX022_Accel_CNTL2[2] = {0x18, 0xC1}; 00204 char KX022_Content_ReadData[6]; 00205 char KX022_Addr_Accel_ReadData = 0x06; 00206 float KX022_Accel_X; 00207 float KX022_Accel_Y; 00208 float KX022_Accel_Z; 00209 short int KX022_Accel_X_RawOUT = 0; 00210 short int KX022_Accel_Y_RawOUT = 0; 00211 short int KX022_Accel_Z_RawOUT = 0; 00212 int KX022_Accel_X_LB = 0; 00213 int KX022_Accel_X_HB = 0; 00214 int KX022_Accel_Y_LB = 0; 00215 int KX022_Accel_Y_HB = 0; 00216 int KX022_Accel_Z_LB = 0; 00217 int KX022_Accel_Z_HB = 0; 00218 float KX022_Accel[3]; 00219 #endif 00220 00221 #ifdef Pressure 00222 int Press_addr_w = 0xBA; //write 00223 int Press_addr_r = 0xBB; //read 00224 char PWR_DOWN[2] = {0x12, 0x01}; 00225 char SLEEP[2] = {0x13, 0x01}; 00226 char Mode_Control[2] = {0x14, 0xC4}; 00227 char Press_Content_ReadData[6]; 00228 char Press_Addr_ReadData =0x1A; 00229 int BM1383_Temp_highByte; 00230 int BM1383_Temp_lowByte; 00231 int BM1383_Pres_highByte; 00232 int BM1383_Pres_lowByte; 00233 int BM1383_Pres_leastByte; 00234 short int BM1383_Temp_Out; 00235 float BM1383_Temp_Conv_Out; 00236 float BM1383_Pres_Conv_Out; 00237 float_t BM1383[2]; // Temp is 0 and Pressure is 1 00238 float BM1383_Var; 00239 float BM1383_Deci; 00240 #endif 00241 00242 /**************************************************************************************************** 00243 // function prototypes 00244 ****************************************************************************************************/ 00245 bool init_mtsas(); 00246 void ReadAnalogTemp(); 00247 // void ReadAnalogUV (); // analog pin A4 on Arduino connector is not connected to the mDot on the UDK. 00248 void ReadHallSensor (); 00249 void ReadCOLOR (); 00250 void ReadRPR0521_ALS (); 00251 void ReadKMX62_Accel (); 00252 void ReadKMX62_Mag (); 00253 void ReadPressure (); 00254 void ReadKX022(); 00255 00256 // these options must match the settings on your Conduit 00257 // uncomment the following lines and edit their values to match your configuration 00258 static std::string config_network_name = "Arrow123"; 00259 static std::string config_network_pass = "Arrow123"; 00260 static uint8_t config_frequency_sub_band = 1; 00261 00262 /**************************************************************************************************** 00263 // main 00264 ****************************************************************************************************/ 00265 int main() 00266 { 00267 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); //NONE_, FATAL_, ERROR_, WARNING_, INFO_, DEBUG_, TRACE_ 00268 debug.baud(debug_baud); 00269 logInfo("starting..."); 00270 00271 int32_t ret; 00272 mDot* dot; 00273 00274 /**************************************************************************************************** 00275 Initialize LORA ************ 00276 ****************************************************************************************************/ 00277 // get a mDot handle 00278 dot = mDot::getInstance(); 00279 00280 // print library version information 00281 logInfo("version: %s", dot->getId().c_str()); 00282 00283 // reset to default config so we know what state we're in 00284 dot->resetConfig(); 00285 00286 dot->setLogLevel(mts::MTSLog::TRACE_LEVEL); 00287 00288 // set up the mDot with our network information: frequency sub band, network name, and network password 00289 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() 00290 00291 // frequency sub band is only applicable in the 915 (US) frequency band 00292 // if using a MultiTech Conduit gateway, use the same sub band as your Conduit (1-8) - the mDot will use the 8 channels in that sub band 00293 // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels 00294 logInfo("setting frequency sub band"); 00295 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { 00296 logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00297 } 00298 00299 logInfo("setting network name"); 00300 if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { 00301 logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00302 } 00303 00304 logInfo("setting network password"); 00305 if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { 00306 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00307 } 00308 00309 // a higher spreading factor allows for longer range but lower throughput 00310 // in the 915 (US) frequency band, spreading factors 7 - 10 are available 00311 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available 00312 logInfo("setting TX spreading factor"); 00313 if ((ret = dot->setTxDataRate(mDot::SF_7)) != mDot::MDOT_OK) { 00314 logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00315 } 00316 00317 // request receive confirmation of packets from the gateway 00318 logInfo("enabling ACKs"); 00319 if ((ret = dot->setAck(1)) != mDot::MDOT_OK) { 00320 logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00321 } 00322 00323 // save this configuration to the mDot's NVM 00324 logInfo("saving config"); 00325 if (! dot->saveConfig()) { 00326 logError("failed to save configuration"); 00327 } 00328 //******************************************* 00329 // end of configuration 00330 //******************************************* 00331 00332 // attempt to join the network 00333 logInfo("joining network"); 00334 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { 00335 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00336 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again 00337 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); 00338 } 00339 00340 00341 /**************************************************************************************************** 00342 Initialize I2C Devices ************ 00343 ****************************************************************************************************/ 00344 00345 #ifdef RPR0521 00346 i2c.write(RPR0521_addr_w, &RPR0521_ModeControl[0], 2, false); 00347 i2c.write(RPR0521_addr_w, &RPR0521_ALSPSControl[0], 2, false); 00348 i2c.write(RPR0521_addr_w, &RPR0521_Persist[0], 2, false); 00349 #endif 00350 00351 #ifdef KMX62 00352 i2c.write(KMX62_addr_w, &KMX62_CNTL2[0], 2, false); 00353 #endif 00354 00355 #ifdef COLOR 00356 i2c.write(BH1745_addr_w, &BH1745_persistence[0], 2, false); 00357 i2c.write(BH1745_addr_w, &BH1745_mode1[0], 2, false); 00358 i2c.write(BH1745_addr_w, &BH1745_mode2[0], 2, false); 00359 i2c.write(BH1745_addr_w, &BH1745_mode3[0], 2, false); 00360 #endif 00361 00362 #ifdef KX022 00363 i2c.write(KX022_addr_w, &KX022_Accel_CNTL1[0], 2, false); 00364 i2c.write(KX022_addr_w, &KX022_Accel_ODCNTL[0], 2, false); 00365 i2c.write(KX022_addr_w, &KX022_Accel_CNTL3[0], 2, false); 00366 i2c.write(KX022_addr_w, &KX022_Accel_TILT_TIMER[0], 2, false); 00367 i2c.write(KX022_addr_w, &KX022_Accel_CNTL2[0], 2, false); 00368 #endif 00369 00370 #ifdef Pressure 00371 i2c.write(Press_addr_w, &PWR_DOWN[0], 2, false); 00372 i2c.write(Press_addr_w, &SLEEP[0], 2, false); 00373 i2c.write(Press_addr_w, &Mode_Control[0], 2, false); 00374 #endif 00375 //End I2C Initialization Section ********************************************************** 00376 00377 00378 Timer thpm_timer; 00379 thpm_timer.start(); // Timer data is set in the Variable seciton see misc variables Timer motion_timer; 00380 Timer print_timer; 00381 print_timer.start(); 00382 Timer motion_timer; 00383 motion_timer.start(); 00384 00385 #ifdef SMS 00386 Timer sms_timer; 00387 sms_timer.start(); 00388 #endif 00389 00390 #ifdef Web 00391 Timer post_timer; 00392 post_timer.start(); 00393 #endif 00394 00395 while (true) { 00396 if (thpm_timer.read_ms() > thpm_interval_ms) { 00397 #ifdef AnalogTemp 00398 ReadAnalogTemp (); 00399 #endif 00400 00401 //#ifdef AnalogUV // analog pin A4 on Arduino connector is not connected to the mDot on the UDK. 00402 // ReadAnalogUV (); 00403 //#endif 00404 00405 #ifdef HallSensor 00406 ReadHallSensor (); 00407 #endif 00408 00409 #ifdef COLOR 00410 ReadCOLOR (); 00411 #endif 00412 00413 #ifdef RPR0521 //als digital 00414 ReadRPR0521_ALS (); 00415 #endif 00416 00417 #ifdef Pressure 00418 ReadPressure(); 00419 #endif 00420 thpm_timer.reset(); 00421 } 00422 00423 if (motion_timer.read_ms() > motion_interval_ms) { 00424 #ifdef KMX62 00425 ReadKMX62_Accel (); 00426 ReadKMX62_Mag (); 00427 #endif 00428 00429 #ifdef KX022 00430 ReadKX022 (); 00431 #endif 00432 motion_timer.reset(); 00433 } 00434 00435 if (print_timer.read_ms() > print_interval_ms) { 00436 logDebug("%s", wall_of_dash); 00437 logDebug("SENSOR DATA"); 00438 logDebug("temperature: %0.2f C", BM1383[0]); 00439 // logDebug("analog uv: %.1f mW/cm2", ML8511_output); // analog pin A4 on Arduino connector is not connected to the mDot on the UDK. 00440 logDebug("ambient Light %0.3f", RPR0521_ALS[0]); 00441 logDebug("proximity count %0.3f", RPR0521_ALS[1]); 00442 logDebug("hall effect: South %d\t North %d", Hall_Return[0],Hall_Return[1]); 00443 logDebug("pressure: %0.2f hPa", BM1383[1]); 00444 logDebug("magnetometer:\r\n\tx: %0.3f\ty: %0.3f\tz: %0.3f\tuT", MEMS_Mag[0], MEMS_Mag[1], MEMS_Mag[2]); 00445 logDebug("accelerometer:\r\n\tx: %0.3f\ty: %0.3f\tz: %0.3f\tg", MEMS_Accel[0], MEMS_Accel[1], MEMS_Accel[2]); 00446 logDebug("color:\r\n\tred: %ld\tgrn: %ld\tblu: %ld\t", BH1745[0], BH1745[1], BH1745[2]); 00447 logDebug("%s", wall_of_dash); 00448 print_timer.reset(); 00449 } 00450 00451 00452 00453 #ifdef SMS 00454 if (sms_timer.read_ms() > sms_interval_ms) { 00455 sms_timer.reset(); 00456 logInfo("SMS Send Routine"); 00457 printf(" In sms routine \r\n"); 00458 00459 char send_msg[20]; 00460 sprintf(send_msg, "Dilbert,%0.0f", RPR0521_ALS[0]); //***************************************probably note worthy 00461 std::string send_msg_str(send_msg); 00462 std::vector<uint8_t> data(send_msg_str.begin(), send_msg_str.end()); 00463 00464 // Added the mdot send code here vs that the sms fuction was doing. 00465 // send the data to the gateway 00466 if ((ret = dot->send(data)) != mDot::MDOT_OK) { 00467 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); 00468 } else { 00469 logInfo("successfully sent data to gateway"); 00470 } 00471 00472 } 00473 #endif 00474 wait_ms(10); 00475 } 00476 } 00477 00478 /************************************************************************************************/ 00479 // Sensor data acquisition functions 00480 /************************************************************************************************/ 00481 #ifdef AnalogTemp 00482 void ReadAnalogTemp () 00483 { 00484 BDE0600_Temp_value = BDE0600_Temp.read_u16(); 00485 00486 BDE0600_output = (float)BDE0600_Temp_value * (float)0.000050354; //(value * (3.3V/65535)) 00487 BDE0600_output = (BDE0600_output-(float)1.753)/((float)-0.01068) + (float)30; 00488 00489 // printf("BDE0600 Analog Temp Sensor Data:\r\n"); 00490 // printf(" Temp = %.2f C\r\n", BDE0600_output); 00491 } 00492 #endif 00493 00494 //#ifdef AnalogUV // analog pin A4 on Arduino connector is not connected to the mDot on the UDK. 00495 //void ReadAnalogUV () 00496 //{ 00497 // ML8511_UV_value = ML8511_UV.read_u16(); 00498 // ML8511_output = (float)ML8511_UV_value * (float)0.000050354; //(value * (3.3V/65535)) //Note to self: when playing with this, a negative value is seen... Honestly, I think this has to do with my ADC converstion... 00499 // ML8511_output = (ML8511_output-(float)2.2)/((float)0.129) + 10; // Added +5 to the offset so when inside (aka, no UV, readings show 0)... this is the wrong approach... and the readings don't make sense... Fix this. 00500 00501 // printf("ML8511 Analog UV Sensor Data:\r\n"); 00502 // printf(" UV = %.1f mW/cm2\r\n", ML8511_output); 00503 00504 //} 00505 //#endif 00506 00507 00508 #ifdef HallSensor 00509 void ReadHallSensor () 00510 { 00511 00512 Hall_Return[0] = Hall_GPIO0; 00513 Hall_Return[1] = Hall_GPIO1; 00514 00515 // printf("BU52011 Hall Switch Sensor Data:\r\n"); 00516 // printf(" South Detect = %d\r\n", Hall_Return[0]); 00517 // printf(" North Detect = %d\r\n", Hall_Return[1]); 00518 00519 00520 } 00521 #endif 00522 00523 #ifdef COLOR 00524 void ReadCOLOR () 00525 { 00526 00527 //Read color data from the IC 00528 i2c.write(BH1745_addr_w, &BH1745_Addr_color_ReadData, 1, RepStart); 00529 i2c.read(BH1745_addr_r, &BH1745_Content_ReadData[0], 6, NoRepStart); 00530 00531 //separate all data read into colors 00532 BH1745[0] = (BH1745_Content_ReadData[1]<<8) | (BH1745_Content_ReadData[0]); 00533 BH1745[1] = (BH1745_Content_ReadData[3]<<8) | (BH1745_Content_ReadData[2]); 00534 BH1745[2] = (BH1745_Content_ReadData[5]<<8) | (BH1745_Content_ReadData[4]); 00535 00536 //Output Data into UART 00537 // printf("BH1745 COLOR Sensor Data:\r\n"); 00538 // printf(" Red = %d ADC Counts\r\n",BH1745[0]); 00539 // printf(" Green = %d ADC Counts\r\n",BH1745[1]); 00540 // printf(" Blue = %d ADC Counts\r\n",BH1745[2]); 00541 00542 } 00543 #endif 00544 00545 #ifdef RPR0521 //als digital 00546 void ReadRPR0521_ALS () 00547 { 00548 i2c.write(RPR0521_addr_w, &RPR0521_Addr_ReadData, 1, RepStart); 00549 i2c.read(RPR0521_addr_r, &RPR0521_Content_ReadData[0], 6, NoRepStart); 00550 00551 RPR0521_ALS[1] = (RPR0521_Content_ReadData[1]<<8) | (RPR0521_Content_ReadData[0]); 00552 RPR0521_ALS_D0_RAWOUT = (RPR0521_Content_ReadData[3]<<8) | (RPR0521_Content_ReadData[2]); 00553 RPR0521_ALS_D1_RAWOUT = (RPR0521_Content_ReadData[5]<<8) | (RPR0521_Content_ReadData[4]); 00554 RPR0521_ALS_DataRatio = (float)RPR0521_ALS_D1_RAWOUT / (float)RPR0521_ALS_D0_RAWOUT; 00555 00556 if(RPR0521_ALS_DataRatio < (float)0.595) { 00557 RPR0521_ALS[0] = ((float)1.682*(float)RPR0521_ALS_D0_RAWOUT - (float)1.877*(float)RPR0521_ALS_D1_RAWOUT); 00558 } else if(RPR0521_ALS_DataRatio < (float)1.015) { 00559 RPR0521_ALS[0] = ((float)0.644*(float)RPR0521_ALS_D0_RAWOUT - (float)0.132*(float)RPR0521_ALS_D1_RAWOUT); 00560 } else if(RPR0521_ALS_DataRatio < (float)1.352) { 00561 RPR0521_ALS[0] = ((float)0.756*(float)RPR0521_ALS_D0_RAWOUT - (float)0.243*(float)RPR0521_ALS_D1_RAWOUT); 00562 } else if(RPR0521_ALS_DataRatio < (float)3.053) { 00563 RPR0521_ALS[0] = ((float)0.766*(float)RPR0521_ALS_D0_RAWOUT - (float)0.25*(float)RPR0521_ALS_D1_RAWOUT); 00564 } else { 00565 RPR0521_ALS[0] = 0; 00566 } 00567 // printf("RPR-0521 ALS/PROX Sensor Data:\r\n"); 00568 // printf(" ALS = %0.2f lx\r\n", RPR0521_ALS[0]); 00569 // printf(" PROX= %0.2f ADC Counts\r\n", RPR0521_ALS[1]); //defined as a float but is an unsigned. 00570 00571 } 00572 #endif 00573 00574 #ifdef KMX62 00575 void ReadKMX62_Accel () 00576 { 00577 //Read Accel Portion from the IC 00578 i2c.write(KMX62_addr_w, &KMX62_Addr_Accel_ReadData, 1, RepStart); 00579 i2c.read(KMX62_addr_r, &KMX62_Content_Accel_ReadData[0], 6, NoRepStart); 00580 00581 //Note: The highbyte and low byte return a 14bit value, dropping the two LSB in the Low byte. 00582 // However, because we need the signed value, we will adjust the value when converting to "g" 00583 MEMS_Accel_Xout = (KMX62_Content_Accel_ReadData[1]<<8) | (KMX62_Content_Accel_ReadData[0]); 00584 MEMS_Accel_Yout = (KMX62_Content_Accel_ReadData[3]<<8) | (KMX62_Content_Accel_ReadData[2]); 00585 MEMS_Accel_Zout = (KMX62_Content_Accel_ReadData[5]<<8) | (KMX62_Content_Accel_ReadData[4]); 00586 00587 //Note: Conversion to G is as follows: 00588 // Axis_ValueInG = MEMS_Accel_axis / 1024 00589 // However, since we did not remove the LSB previously, we need to divide by 4 again 00590 // Thus, we will divide the output by 4096 (1024*4) to convert and cancel out the LSB 00591 MEMS_Accel[0] = ((float)MEMS_Accel_Xout/4096/2); 00592 MEMS_Accel[1] = ((float)MEMS_Accel_Yout/4096/2); 00593 MEMS_Accel[2] = ((float)MEMS_Accel_Zout/4096/2); 00594 00595 // Return Data to UART 00596 // printf("KMX62 Accel+Mag Sensor Data:\r\n"); 00597 // printf(" AccX= %0.2f g\r\n", MEMS_Accel[0]); 00598 // printf(" AccY= %0.2f g\r\n", MEMS_Accel[1]); 00599 // printf(" AccZ= %0.2f g\r\n", MEMS_Accel[2]); 00600 00601 } 00602 00603 void ReadKMX62_Mag () 00604 { 00605 00606 //Read Mag portion from the IC 00607 i2c.write(KMX62_addr_w, &KMX62_Addr_Mag_ReadData, 1, RepStart); 00608 i2c.read(KMX62_addr_r, &KMX62_Content_Mag_ReadData[0], 6, NoRepStart); 00609 00610 //Note: The highbyte and low byte return a 14bit value, dropping the two LSB in the Low byte. 00611 // However, because we need the signed value, we will adjust the value when converting to "g" 00612 MEMS_Mag_Xout = (KMX62_Content_Mag_ReadData[1]<<8) | (KMX62_Content_Mag_ReadData[0]); 00613 MEMS_Mag_Yout = (KMX62_Content_Mag_ReadData[3]<<8) | (KMX62_Content_Mag_ReadData[2]); 00614 MEMS_Mag_Zout = (KMX62_Content_Mag_ReadData[5]<<8) | (KMX62_Content_Mag_ReadData[4]); 00615 00616 //Note: Conversion to G is as follows: 00617 // Axis_ValueInG = MEMS_Accel_axis / 1024 00618 // However, since we did not remove the LSB previously, we need to divide by 4 again 00619 // Thus, we will divide the output by 4095 (1024*4) to convert and cancel out the LSB 00620 MEMS_Mag[0] = (float)MEMS_Mag_Xout/4096*(float)0.146; 00621 MEMS_Mag[1] = (float)MEMS_Mag_Yout/4096*(float)0.146; 00622 MEMS_Mag[2] = (float)MEMS_Mag_Zout/4096*(float)0.146; 00623 00624 // Return Data to UART 00625 // printf(" MagX= %0.2f uT\r\n", MEMS_Mag[0]); 00626 // printf(" MagY= %0.2f uT\r\n", MEMS_Mag[1]); 00627 // printf(" MagZ= %0.2f uT\r\n", MEMS_Mag[2]); 00628 00629 } 00630 #endif 00631 00632 #ifdef KX022 00633 void ReadKX022 () 00634 { 00635 00636 //Read KX022 Portion from the IC 00637 i2c.write(KX022_addr_w, &KX022_Addr_Accel_ReadData, 1, RepStart); 00638 i2c.read(KX022_addr_r, &KX022_Content_ReadData[0], 6, NoRepStart); 00639 00640 //Format Data 00641 KX022_Accel_X_RawOUT = (KX022_Content_ReadData[1]<<8) | (KX022_Content_ReadData[0]); 00642 KX022_Accel_Y_RawOUT = (KX022_Content_ReadData[3]<<8) | (KX022_Content_ReadData[2]); 00643 KX022_Accel_Z_RawOUT = (KX022_Content_ReadData[5]<<8) | (KX022_Content_ReadData[4]); 00644 00645 //Scale Data 00646 KX022_Accel[0] = (float)KX022_Accel_X_RawOUT / 16384; 00647 KX022_Accel[1] = (float)KX022_Accel_Y_RawOUT / 16384; 00648 KX022_Accel[2] = (float)KX022_Accel_Z_RawOUT / 16384; 00649 00650 //Return Data through UART 00651 // printf("KX022 Accelerometer Sensor Data: \r\n"); 00652 // printf(" AccX= %0.2f g\r\n", KX022_Accel[0]); 00653 // printf(" AccY= %0.2f g\r\n", KX022_Accel[1]); 00654 // printf(" AccZ= %0.2f g\r\n", KX022_Accel[2]); 00655 00656 } 00657 #endif 00658 00659 00660 #ifdef Pressure 00661 void ReadPressure () 00662 { 00663 00664 i2c.write(Press_addr_w, &Press_Addr_ReadData, 1, RepStart); 00665 i2c.read(Press_addr_r, &Press_Content_ReadData[0], 6, NoRepStart); 00666 00667 BM1383_Temp_Out = (Press_Content_ReadData[0]<<8) | (Press_Content_ReadData[1]); 00668 BM1383[0] = (float)BM1383_Temp_Out/32; 00669 00670 BM1383_Var = (Press_Content_ReadData[2]<<3) | (Press_Content_ReadData[3] >> 5); 00671 BM1383_Deci = ((Press_Content_ReadData[3] & 0x1f) << 6 | ((Press_Content_ReadData[4] >> 2))); 00672 BM1383_Deci = (float)BM1383_Deci* (float)0.00048828125; //0.00048828125 = 2^-11 00673 BM1383[1] = (BM1383_Var + BM1383_Deci); //question pending here... 00674 00675 // printf("BM1383 Pressure Sensor Data:\r\n"); 00676 // printf(" Temperature= %0.2f C\r\n", BM1383[0]); 00677 // printf(" Pressure = %0.2f hPa\r\n", BM1383[1]); 00678 00679 } 00680 #endif
Generated on Sun Jul 17 2022 07:45:16 by 1.7.2