From cf58db6d84c9ba9c30268b480b0fc0d7f506bb9b Mon Sep 17 00:00:00 2001 From: Will Richards Date: Tue, 14 Feb 2023 14:58:59 -0800 Subject: [PATCH] Release Version 4.2 --- auxilary/package_loom4_index.json | 30 +++++ src/Radio/Loom_Freewave/Loom_Freewave.cpp | 143 ++++++++++++++++++++++ src/Radio/Loom_Freewave/Loom_Freewave.h | 93 ++++++++++++++ src/Radio/Radio.h | 4 +- 4 files changed, 268 insertions(+), 2 deletions(-) create mode 100644 src/Radio/Loom_Freewave/Loom_Freewave.cpp create mode 100644 src/Radio/Loom_Freewave/Loom_Freewave.h diff --git a/auxilary/package_loom4_index.json b/auxilary/package_loom4_index.json index 01ac32ad..51a0070d 100644 --- a/auxilary/package_loom4_index.json +++ b/auxilary/package_loom4_index.json @@ -117,6 +117,36 @@ } ], "tools":[] + }, + { + "name": "loom4", + "maintainer": "OPEnS Lab", + "websiteURL": "https://github.com/OPEnSLab-OSU/Loom-V4", + "email": "open.sensing@oregonstate.edu", + "help":{ + "online": "https://github.com/OPEnSLab-OSU/Loom-V4/issues" + }, + "platforms": [ + { + "name": "Loom SAMD Boards V4", + "architecture": "samd", + "version": "4.2", + "category": "contributed", + "help": { + "online": "https://github.com/OPEnSLab-OSU/Loom-V4/issues" + }, + "url": "https://github.com/OPEnSLab-OSU/Loom-V4/releases/download/v4.2/loom4.zip", + "archiveFileName": "loom4.zip", + "checksum": "SHA-256:92851e0410940f9d9413dd7c940def4317efcfdbdbcd304fd3d02fbbb6839cf9", + "size": "21346647", + "boards": [ + { + "name": "Loomified Adafruit Feather M0 (SAMD21)" + } + ] + } + ], + "tools":[] } ] } diff --git a/src/Radio/Loom_Freewave/Loom_Freewave.cpp b/src/Radio/Loom_Freewave/Loom_Freewave.cpp new file mode 100644 index 00000000..9d69056d --- /dev/null +++ b/src/Radio/Loom_Freewave/Loom_Freewave.cpp @@ -0,0 +1,143 @@ +#include "Loom_Freewave.h" + +////////////////////////////////////////////////////////////////////////////////////////////////////// +Loom_Freewave::Loom_Freewave( + Manager& man, + const uint8_t address, + const uint16_t max_message_len, + const uint8_t retryCount, + const uint16_t retryTimeout + ) : Radio("Freewave"), manInst(&man), serial1(Serial1), driver(serial1) + { + if(address == -1) + this->deviceAddress = manInst->get_instance_num(); + else + this->deviceAddress = address; + + manager = new RHReliableDatagram(driver, this->deviceAddress); + this->retryCount = retryCount; + this->retryTimeout = retryTimeout; + this->maxMessageLength = max_message_len; + manInst->registerModule(this); + } +////////////////////////////////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////////////////////////////////// +void Loom_Freewave::initialize(){ + + // Start serial communication with radio + serial1.begin(115200); + + // Set timeout time + printModuleName("Timeout time set to: " + String(retryTimeout)); + manager->setTimeout(retryTimeout); + + // Set retry attempts + printModuleName("Retry count set to: " + String(retryCount)); + manager->setRetries(retryCount); + + // Initialize the radio manager + if(manager->init()){ + printModuleName("Radio manager successfully initialized!"); + } + else{ + printModuleName("Radio manager failed to initialize!"); + moduleInitialized = false; + return; + } +} +////////////////////////////////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////////////////////////////////// +void Loom_Freewave::package(){ + if(moduleInitialized){ + JsonObject json = manInst->get_data_object(getModuleName()); + json["RSSI"] = getSignalStrength(); + } +} +////////////////////////////////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////////////////////////////////// +void Loom_Freewave::setAddress(uint8_t addr){ + deviceAddress = addr; + manager->setThisAddress(addr); + driver.sleep(); +} +////////////////////////////////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////////////////////////////////// +bool Loom_Freewave::receive(uint maxWaitTime){ + bool recvStatus = false; + uint8_t fromAddress; + + + // Write all null bytes to the buffer + uint8_t buffer[maxMessageLength]; + uint8_t len = sizeof(buffer); + + printModuleName("Waiting for packet..."); + + // Non-blocking receive if time is set to 0 + if(maxWaitTime == 0){ + recvStatus = manager->recvfromAck(buffer, &len, &fromAddress); + } + else{ + recvStatus = manager->recvfromAckTimeout(buffer, &len, maxWaitTime, &fromAddress); + } + + // If a packet was received + if(recvStatus){ + printModuleName("Packet Received!"); + signalStrength = driver.lastRssi(); + recvStatus = bufferToJson(buffer); + recvData = ""; + serializeJson(recvDoc, recvData); + deserializeJson(manInst->getDocument(), recvData); + + // Update device name + manInst->set_device_name(manInst->getDocument()["id"]["name"].as()); + manInst->set_instance_num(manInst->getDocument()["id"]["instance"].as()); + + } + else{ + printModuleName("No Packet Received"); + } + + driver.sleep(); + return recvStatus; +} +////////////////////////////////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////////////////////////////////// +bool Loom_Freewave::send(const uint8_t destinationAddress){ + uint8_t buffer[maxMessageLength]; + + // Try to write the JSON to the buffer + if(!jsonToBuffer(buffer, manInst->getDocument().as())){ + printModuleName("Failed to convert JSON to MsgPack"); + return false; + } + + if(!manager->sendtoWait((uint8_t*)buffer, sizeof(buffer), destinationAddress)){ + printModuleName("Failed to send packet to specified address!"); + return false; + } + + printModuleName("Successfully transmit packet!"); + signalStrength = driver.lastRssi(); + driver.sleep(); + return true; +} +////////////////////////////////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////////////////////////////////// +void Loom_Freewave::power_up(){ + driver.available(); +} +////////////////////////////////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////////////////////////////////// +void Loom_Freewave::power_down(){ + driver.sleep(); +} +////////////////////////////////////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/src/Radio/Loom_Freewave/Loom_Freewave.h b/src/Radio/Loom_Freewave/Loom_Freewave.h new file mode 100644 index 00000000..58597ce9 --- /dev/null +++ b/src/Radio/Loom_Freewave/Loom_Freewave.h @@ -0,0 +1,93 @@ +#pragma once + + +#include "../Radio.h" +#include "../../Loom_Manager.h" + +#include +#include +#include + +/** + * Used to communicate with LoRa type radios + * + * @author Will Richards + */ +class Loom_Freewave : public Radio{ + protected: + /* These aren't used with this module */ + void measure() override {}; + void print_measurements() override {}; + + + public: + + /** + * Construct a new LoRa driver + * @param man Reference to the manager + * @param max_message_len The maximum possible message length we can transmit + * @param address This device's LoRa address + * @param powerLevel Transmission power level, low to high + * @param retryCount Number of attempts to make before failing + * @param retryTimeout Length of time between retransmissions (ms) + */ + Loom_Freewave( + Manager& man, + const uint8_t address = -1, + const uint16_t max_message_len = RH_SERIAL_MAX_MESSAGE_LEN, + const uint8_t retryCount = 3, + const uint16_t retryTimeout = 200 + ); + + ~Loom_Freewave(){ + delete manager; + } + + /** + * Receive a JSON packet from another radio, blocking until the wait time expires or a packet is received + * @param maxWaitTime The maximum time to wait before continuing execution (Set to 0 for non-blocking) + */ + bool receive(uint maxWaitTime) override; + + /** + * Send the current JSON data to the specified address + * @param destinationAddress The address we want to send the data to + */ + bool send(const uint8_t destinationAddress) override; + + /** + * Initialize the module + */ + void initialize() override; + + /** + * Package basic data about the device + */ + void package() override; + + /** + * Power up the module + */ + void power_up() override; + + /** + * Power down the module + */ + void power_down() override; + + /** + * Set the address of the device + */ + void setAddress(const uint8_t addr); + + private: + Manager* manInst; // Instance of the manager + + String recvData = ""; + + HardwareSerial& serial1; // Serial reference + RH_Serial driver; // Freewave Driver + RHReliableDatagram* manager; // Manager for driver + + +}; \ No newline at end of file diff --git a/src/Radio/Radio.h b/src/Radio/Radio.h index b4efb909..221aae8f 100644 --- a/src/Radio/Radio.h +++ b/src/Radio/Radio.h @@ -51,7 +51,7 @@ class Radio : public Module{ */ bool bufferToJson(uint8_t* buffer){ - DeserializationError error = deserializeMsgPack(recvDoc, buffer, 251); + DeserializationError error = deserializeMsgPack(recvDoc, buffer, maxMessageLength); // Check if an error occurred if(error != DeserializationError::Ok){ @@ -66,7 +66,7 @@ class Radio : public Module{ * Convert the json to a message pack */ bool jsonToBuffer(uint8_t* buffer, JsonObjectConst json){ - bool status = serializeMsgPack(json, buffer, 251); + bool status = serializeMsgPack(json, buffer, maxMessageLength); return status; };