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: libmDot-mbed5 DOGS102 ISL29011 MMA845x MPL3115A2 NCP5623B X_NUCLEO_IKS01A1 Senet_Packet
Fork of MTDOT-UDKDemo_Senet by
Diff: main.cpp
- Revision:
- 14:07ff01da6bbf
- Parent:
- 12:ad92bbb6312f
- Child:
- 16:7b3cc3221db1
- Child:
- 17:162e42587b4d
--- a/main.cpp Tue Aug 23 23:35:00 2016 -0400
+++ b/main.cpp Wed Aug 24 13:56:52 2016 -0400
@@ -44,10 +44,30 @@
#include "font_6x8.h"
#include "MultiTech_Logo.h"
+// Min send delay from end of RX2
+#define MIN_SEND_DELAY 0
+
+// Send frame period for unchanged sensor value
+#define MIN_SEND_PERIOD pckt_time
+
+// Send frame period for unchanged sensor value
+#define MAX_SEND_PERIOD 100
+
#elif defined(MTDOT_UDK)
#include "x_nucleo_iks01a1.h"
+// Min send delay from end of RX2
+#define MIN_SEND_DELAY 2000
+
+// Min send period (limits sending to fast)
+#define MIN_SEND_PERIOD 1
+
+// Max send period (max time between sends)
+#define MAX_SEND_PERIOD 12
+
+
+
#endif
#include "mDot.h"
@@ -116,13 +136,16 @@
* LoRaWAN Configuration
*/
static uint8_t app_id[8] = {0x00,0x25,0x0C,0x00,0x00,0x01,0x00,0x01};
-static uint8_t app_key[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+static uint8_t app_key[16] = {0x9F,0x93,0xDC,0xEB,0xAC,0x2F,0x4F,0xEE,0x80,0x3C,0x05,0x7A,0x54,0x8E,0x44,0x99};
static std::vector<uint8_t> config_app_id(app_id,app_id+sizeof(app_id)/sizeof(uint8_t));
static std::vector<uint8_t> config_app_key(app_key,app_key+sizeof(app_key)/sizeof(uint8_t));
static uint8_t config_frequency_sub_band = 0;
static bool config_adr_on = true;
+bool position_changed = true;
+uint32_t sample_period = 0;
+
#ifdef MTDOT_EVB
MMA845x_DATA accel_data;
MPL3115A2_DATA baro_data;
@@ -138,7 +161,6 @@
*/
uint8_t position_value = 0xFF; // 00 unknown, 01 is flat, 02 is vertical
uint8_t reflected_value = 0xFE;
-bool position_changed = true;
unsigned char test;
char txtstr[17];
@@ -161,12 +183,16 @@
#elif defined(MTDOT_UDK)
+uint16_t position_value = 0;
+uint16_t reflected_value = 0;
+
static X_NUCLEO_IKS01A1 *mems_shield;
#endif
mDot* mdot_radio;
bool exit_program = false;
+Ticker joinTicker;
/*
* Process downlink
@@ -188,25 +214,15 @@
*/
void SendFrame(std::vector<uint8_t> frame)
{
-#if 0
- static uint32_t count = 0;
- printf("%lu: send data: ",count++);
- for(uint32_t i = 0;i < frame.size();i++)
- printf("%02X",frame[i]);
- printf("\r\n");
-#endif
+ int32_t mdot_ret;
- int32_t mdot_ret;
if ((mdot_ret = mdot_radio->send(frame)) != mDot::MDOT_OK) {
log_error(mdot_radio, "failed to send", mdot_ret);
}
else {
- printf("successfully sent data to gateway\r\n");
+ printf("successfully sent data\r\n");
frame.clear();
- if ((mdot_ret = mdot_radio->recv(frame)) != mDot::MDOT_OK) {
- log_error(mdot_radio,"failed to recv:", mdot_ret);
- } else {
-
+ if ((mdot_ret = mdot_radio->recv(frame)) == mDot::MDOT_OK) {
printf("recv data: ");
for(uint32_t i = 0;i < frame.size();i++)
printf("%02X",frame[i]);
@@ -214,6 +230,10 @@
ReceiveData(frame);
}
+#ifndef DOWNLINK_REFLECT
+ position_changed = false;
+ reflected_value = position_value;
+#endif
}
}
@@ -506,24 +526,23 @@
uint32_t PrepareFrame(std::vector<uint8_t> &frame, BoardSensorData &data)
{
- if((reflected_value != position_value)|| position_changed)
+ frame.clear();
+
+ if((reflected_value != position_value)|| position_changed || ( ( sample_period % MAX_SEND_PERIOD ) == 0 ) )
{
- frame.clear();
// we will send a simple byte descriptor of the current position of the device: 01 is laying flat, 02 is vertically oriented
frame.push_back(0x00);
frame.push_back(position_value);
- return 2;
}
- return 0;
+ return frame.size();
}
bool checkForExit(bool _exit)
{
// Check for PB1 press during network join attempt
if (exit_program) {
- printf("Exiting program\n\r");
-
+ printf("Exiting program\n\r");
evbLCD->clearBuffer();
sprintf(txtstr,"Exiting Program");
evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
@@ -552,11 +571,6 @@
}
-void wait()
-{
- osDelay(1000);
-}
-
/*
* Sets pb1_low flag. Slag is cleared in pb1_debounce thread
*/
@@ -655,6 +669,17 @@
void ReceiveData(std::vector<uint8_t> frame)
{
+ uint16_t value;
+
+ if(frame.size() >= 2)
+ {
+ value = frame[0] << 8 | frame[1];
+ if(value == position_value)
+ {
+ reflected_value = value;
+ position_changed = false;
+ }
+ }
}
void BoardInit()
@@ -689,29 +714,90 @@
data.accel_x = accel_data[0];
data.accel_y = accel_data[1];
data.accel_z = accel_data[2];
+ /* z-axis : > 0 = rightside up, < 0 upside down
+ * x-axis: com LED to the left x < 0, x > 0 on the right
+ * y-axis: y > 0 COM LED down, y < 0 COM LED up
+ */
+ bool up = false;
+ bool down = false;
+ bool right = false;
+ bool left = false;
+ bool horizontal = false;
+ bool upsidedown = false;
+ uint16_t next_value = 0;
+
+ // rightside up
+ if(data.accel_z >= 750)
+ {
+ horizontal = true;
+ }
+ // upside down
+ else if(data.accel_z <= -750)
+ {
+ horizontal = true;
+ upsidedown = true;
+ position_value = (2 << 12) | (1 << 8);
+ }
+ // vertical down
+ else if(data.accel_y >= 900 )
+ {
+ down = true;
+ }
+ // vertical up
+ else if(data.accel_y <= -900 )
+ {
+ up = true;
+ }
+ // side right
+ else if(data.accel_x > 900)
+ {
+ right = true;
+ }
+ // side left
+ else
+ {
+ left = true;
+ }
+
+ if(horizontal)
+ {
+ next_value = (2 << 12) | (upsidedown << 8);
+ }
+ else
+ {
+ next_value = (up << 12) | (left << 8) | (down << 4) | right;
+ }
+
+ if(next_value != position_value)
+ {
+ position_value = next_value;
+ position_changed = true;
+ }
}
- printf("%s: Temperature=%f, Pressure=%f, x=%ld, y=%ld, z=%ld\r\n",__func__,
- data.temperature, data.pressure,
- data.accel_x, data.accel_y, data.accel_z);
+ printf("%s: position_value=%04x, reflected_value=%04x\r\n",__func__, position_value, reflected_value);
}
uint32_t PrepareFrame(std::vector<uint8_t> &frame, BoardSensorData &data)
{
- static uint8_t buffer[256];
- uint16_t xyz_mask = 0;
+ static uint8_t buffer[64];
+
+ frame.clear();
// Sensor packet type serialized to the LMIC frame buffer
- SensorPacket packet(buffer, sizeof(frame));
+ SensorPacket packet(buffer, sizeof(buffer));
- packet.setPrimarySensor(xyz_mask);
- packet.setTemperature(data.temperature);
- packet.setPressure(data.pressure);
-
- // Serialize packet to LMIC transmit buffer
- packet.serialize();
+ if( position_changed || (reflected_value != position_value) || ( ( sample_period % MAX_SEND_PERIOD ) == 0 ) )
+ {
+ packet.setPrimarySensor(position_value);
+ packet.setTemperature(data.temperature);
+ packet.setPressure(data.pressure);
+
+ // Serialize packet
+ packet.serialize();
- frame.assign(packet.payload(), packet.payload() + packet.length());
+ frame.assign(packet.payload(), packet.payload() + packet.length());
+ }
return frame.size();
}
@@ -724,16 +810,17 @@
printf("Exiting\n\r");
}
-void wait()
-{
- osDelay(5000);
-}
-
-
#else
#error Board type not defined!
#endif
+DigitalOut joinLED(PA_0);
+
+void joinLedToggle()
+{
+ joinLED = !joinLED;
+}
+
void mDotConfigureAndJoin()
{
bool ok;
@@ -758,7 +845,7 @@
// reset to default config so we know what state we're in
mdot_radio->resetConfig();
- mdot_radio->setLogLevel(6);
+ //mdot_radio->setLogLevel(6);
mdot_radio->setAntennaGain(-3);
@@ -833,22 +920,26 @@
}while(ok == false);
- // attempt to join the network
- printf("joining network\r\n");
- while ((mdot_ret = mdot_radio->joinNetwork()) != mDot::MDOT_OK) {
- log_error(mdot_radio,"failed to join network:", mdot_ret);
- if (mdot_radio->getFrequencyBand() == mDot::FB_868){
- mdot_ret = mdot_radio->getNextTxMs();
- }
- else {
- mdot_ret = 0;
- }
+ joinTicker.attach(joinLedToggle,1);
+
+ // attempt to join the network
+ printf("joining network\r\n");
+ while ((mdot_ret = mdot_radio->joinNetwork()) != mDot::MDOT_OK) {
+ log_error(mdot_radio,"failed to join network:", mdot_ret);
+ if (mdot_radio->getFrequencyBand() == mDot::FB_868){
+ mdot_ret = mdot_radio->getNextTxMs();
+ }
+ else {
+ mdot_ret = 0;
+ }
+ checkForExit(true);
+ printf("delay = %lu\n\r",mdot_ret);
+ osDelay(mdot_ret + 10000);
+ }
+ printf("network joined\r\n");
- checkForExit(true);
-
- printf("delay = %lu\n\r",mdot_ret);
- osDelay(mdot_ret + 10000);
- }
+ joinTicker.detach();
+ joinLED=1;
}
@@ -869,17 +960,26 @@
/*
* Main data acquisition loop
*/
- do {
- // Acquire sensor values
- ReadSensors(sensorData);
+ while(!checkForExit(false))
+ {
+ if( MIN_SEND_DELAY > 0 )
+ osDelay( MIN_SEND_DELAY );
- // Send sensor packets
- if( PrepareFrame(frame, sensorData) > 0 ){
- SendFrame( frame );
+ // Minimum delay between sampling
+ if( ( sample_period % MIN_SEND_PERIOD ) == 0 )
+ {
+ // Acquire sensor values
+ ReadSensors(sensorData);
+
+ // Generate frame if send conditions are satisified
+ if( PrepareFrame(frame, sensorData) > 0 )
+ {
+ // Send sensor packets
+ SendFrame( frame );
+ }
}
- wait();
-
- } while(!checkForExit(false));
+ sample_period++;
+ }
ExitingProgram();
}
