Compare commits
No commits in common. "8ce608ae2a1958cf46671419cc75b377e7f4adc0" and "a6a91798b24552522373414d43981b082025cfdd" have entirely different histories.
8ce608ae2a
...
a6a91798b2
@ -19,10 +19,8 @@
|
||||
namespace RoboidControl {
|
||||
namespace Arduino {
|
||||
|
||||
void LocalParticipant::Setup(int localPort,
|
||||
const char* remoteIpAddress,
|
||||
int remotePort) {
|
||||
#if defined(ARDUINO) && defined(HAS_WIFI)
|
||||
void LocalParticipant::Setup(int localPort, const char* remoteIpAddress, int remotePort) {
|
||||
#if defined(ARDUINO)
|
||||
this->remoteIpAddress = remoteIpAddress;
|
||||
this->remotePort = remotePort;
|
||||
GetBroadcastAddress();
|
||||
@ -45,19 +43,18 @@ void LocalParticipant::Setup(int localPort,
|
||||
}
|
||||
|
||||
void LocalParticipant::GetBroadcastAddress() {
|
||||
#if defined(ARDUINO) && defined(HAS_WIFI)
|
||||
#if defined(ARDUINO)
|
||||
IPAddress broadcastAddress = WiFi.localIP();
|
||||
broadcastAddress[3] = 255;
|
||||
String broadcastIpString = broadcastAddress.toString();
|
||||
this->broadcastIpAddress = new char[broadcastIpString.length() + 1];
|
||||
broadcastIpString.toCharArray(this->broadcastIpAddress,
|
||||
broadcastIpString.length() + 1);
|
||||
broadcastIpString.toCharArray(this->broadcastIpAddress, broadcastIpString.length() + 1);
|
||||
std::cout << "Broadcast address: " << broadcastIpAddress << "\n";
|
||||
#endif
|
||||
}
|
||||
|
||||
void LocalParticipant::Receive() {
|
||||
#if defined(ARDUINO) && defined(HAS_WIFI)
|
||||
#if defined(ARDUINO)
|
||||
int packetSize = udp.parsePacket();
|
||||
while (packetSize > 0) {
|
||||
udp.read(buffer, packetSize);
|
||||
@ -67,14 +64,12 @@ void LocalParticipant::Receive() {
|
||||
senderAddress.toCharArray(sender_ipAddress, 16);
|
||||
unsigned int sender_port = udp.remotePort();
|
||||
|
||||
// Participant* remoteParticipant = this->GetParticipant(sender_ipAddress,
|
||||
// sender_port); if (remoteParticipant == nullptr) {
|
||||
// remoteParticipant = this->AddParticipant(sender_ipAddress,
|
||||
// sender_port);
|
||||
// Participant* remoteParticipant = this->GetParticipant(sender_ipAddress, sender_port);
|
||||
// if (remoteParticipant == nullptr) {
|
||||
// remoteParticipant = this->AddParticipant(sender_ipAddress, sender_port);
|
||||
// // std::cout << "New sender " << sender_ipAddress << ":" << sender_port
|
||||
// // << "\n";
|
||||
// // std::cout << "New remote participant " <<
|
||||
// remoteParticipant->ipAddress
|
||||
// // std::cout << "New remote participant " << remoteParticipant->ipAddress
|
||||
// // << ":" << remoteParticipant->port << " "
|
||||
// // << (int)remoteParticipant->networkId << "\n";
|
||||
// }
|
||||
@ -87,7 +82,7 @@ void LocalParticipant::Receive() {
|
||||
}
|
||||
|
||||
bool LocalParticipant::Send(Participant* remoteParticipant, int bufferSize) {
|
||||
#if defined(ARDUINO) && defined(HAS_WIFI)
|
||||
#if defined(ARDUINO)
|
||||
// std::cout << "Sending to:\n " << remoteParticipant->ipAddress << ":"
|
||||
// << remoteParticipant->port << "\n";
|
||||
|
||||
@ -107,7 +102,7 @@ bool LocalParticipant::Send(Participant* remoteParticipant, int bufferSize) {
|
||||
}
|
||||
|
||||
bool LocalParticipant::Publish(IMessage* msg) {
|
||||
#if defined(ARDUINO) && defined(HAS_WIFI)
|
||||
#ifdef ARDUINO
|
||||
int bufferSize = msg->Serialize((char*)this->buffer);
|
||||
if (bufferSize <= 0)
|
||||
return true;
|
||||
|
@ -2,10 +2,6 @@
|
||||
|
||||
#include "../LocalParticipant.h"
|
||||
|
||||
#if defined(HAS_WIFI)
|
||||
#include <WiFiUdp.h>
|
||||
#endif
|
||||
|
||||
namespace RoboidControl {
|
||||
namespace Arduino {
|
||||
|
||||
@ -17,14 +13,6 @@ class LocalParticipant : public RoboidControl::LocalParticipant {
|
||||
bool Publish(IMessage* msg);
|
||||
|
||||
protected:
|
||||
#if defined(HAS_WIFI)
|
||||
const char* remoteIpAddress = nullptr;
|
||||
unsigned short remotePort = 0;
|
||||
char* broadcastIpAddress = nullptr;
|
||||
|
||||
WiFiUDP udp;
|
||||
#endif
|
||||
|
||||
void GetBroadcastAddress();
|
||||
};
|
||||
|
||||
|
@ -42,13 +42,8 @@ struct NssServer {
|
||||
} nssServer;
|
||||
#endif
|
||||
|
||||
bool StartWifi(const char* wifiSsid,
|
||||
const char* wifiPassword,
|
||||
bool hotspotFallback) {
|
||||
#if !defined(HAS_WIFI)
|
||||
return false;
|
||||
#else
|
||||
#if defined(UNO_R4) || defined(ARDUINO_ARCH_RP2040)
|
||||
bool StartWifi(const char* wifiSsid, const char* wifiPassword, bool hotspotFallback) {
|
||||
#if UNO_R4 || ARDUINO_ARCH_RP2040
|
||||
if (WiFi.status() == WL_NO_MODULE) {
|
||||
Serial.println("WiFi not present, WiFiSync is disabled");
|
||||
return false;
|
||||
@ -125,10 +120,8 @@ bool StartWifi(const char* wifiSsid,
|
||||
#if ESP32
|
||||
printf("Checking credentials in flash\n");
|
||||
wifiPreferences.begin(PREFERENCES_NAMESPACE);
|
||||
wifiPreferences.getBytes(STORAGE_KEY_WIFI, &credentials,
|
||||
sizeof(credentials));
|
||||
if (strcmp(wifiSsid, credentials.ssid) != 0 ||
|
||||
strcmp(wifiPassword, credentials.password) != 0) {
|
||||
wifiPreferences.getBytes(STORAGE_KEY_WIFI, &credentials, sizeof(credentials));
|
||||
if (strcmp(wifiSsid, credentials.ssid) != 0 || strcmp(wifiPassword, credentials.password) != 0) {
|
||||
printf("Updating credentials in flash...");
|
||||
const int ssidLen = strlen(wifiSsid);
|
||||
if (ssidLen < 32) {
|
||||
@ -141,8 +134,7 @@ bool StartWifi(const char* wifiSsid,
|
||||
memcpy(credentials.password, wifiPassword, pwdLen);
|
||||
credentials.password[pwdLen] = '\0';
|
||||
}
|
||||
wifiPreferences.putBytes(STORAGE_KEY_WIFI, &credentials,
|
||||
sizeof(credentials));
|
||||
wifiPreferences.putBytes(STORAGE_KEY_WIFI, &credentials, sizeof(credentials));
|
||||
printf(" completed.\n");
|
||||
}
|
||||
wifiPreferences.end();
|
||||
@ -150,15 +142,10 @@ bool StartWifi(const char* wifiSsid,
|
||||
}
|
||||
|
||||
return (!hotSpotEnabled);
|
||||
#endif
|
||||
}
|
||||
|
||||
void CheckFirmware(String url, String FIRMWARE_NAME, int FIRMWARE_VERSION) {
|
||||
#if !defined(HAS_WIFI)
|
||||
return;
|
||||
#else
|
||||
#if defined(UNO_R4) // Uno R4 Wifi does not support this kind of firmware
|
||||
// update (as far as I know)
|
||||
#if defined(UNO_R4) // Uno R4 Wifi does not support this kind of firmware update (as far as I know)
|
||||
return;
|
||||
#else
|
||||
Serial.println("Checking for firmware updates.");
|
||||
@ -190,12 +177,10 @@ void CheckFirmware(String url, String FIRMWARE_NAME, int FIRMWARE_VERSION) {
|
||||
switch (ret) {
|
||||
case HTTP_UPDATE_FAILED:
|
||||
#if defined(ESP32)
|
||||
Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s",
|
||||
httpUpdate.getLastError(),
|
||||
Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s", httpUpdate.getLastError(),
|
||||
httpUpdate.getLastErrorString().c_str());
|
||||
#else
|
||||
Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s",
|
||||
ESPhttpUpdate.getLastError(),
|
||||
Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s", ESPhttpUpdate.getLastError(),
|
||||
ESPhttpUpdate.getLastErrorString().c_str());
|
||||
#endif
|
||||
break;
|
||||
@ -213,6 +198,5 @@ void CheckFirmware(String url, String FIRMWARE_NAME, int FIRMWARE_VERSION) {
|
||||
Serial.println(httpCode);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
#endif
|
@ -3,9 +3,11 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
namespace RoboidControl {
|
||||
namespace Arduino {
|
||||
|
||||
DRV8833Motor::DRV8833Motor(Participant* participant, unsigned char pinIn1, unsigned char pinIn2, bool reverse)
|
||||
DRV8833Motor::DRV8833Motor(Participant* participant,
|
||||
unsigned char pinIn1,
|
||||
unsigned char pinIn2,
|
||||
bool reverse)
|
||||
: Thing(participant) {
|
||||
this->pinIn1 = pinIn1;
|
||||
this->pinIn2 = pinIn2;
|
||||
@ -45,7 +47,7 @@ void DRV8833Motor::SetAngularVelocity(Spherical velocity) {
|
||||
if (velocity.direction.horizontal.InDegrees() < 0)
|
||||
motorSpeed = -motorSpeed;
|
||||
if (this->reverse)
|
||||
motorSpeed = -motorSpeed;
|
||||
motorSpeed =-motorSpeed;
|
||||
|
||||
// std::cout << "ang speed " << this->name << " = " << angularSpeed << " rpm " << rpm
|
||||
// << ", motor signal = " << (int)motorSignal << "\n";
|
||||
@ -116,5 +118,4 @@ DRV8833::DRV8833(Participant* participant,
|
||||
this->motorB->name = "Motor B";
|
||||
}
|
||||
|
||||
} // namespace Arduino
|
||||
} // namespace RoboidControl
|
@ -5,18 +5,16 @@
|
||||
namespace RoboidControl {
|
||||
namespace Arduino {
|
||||
|
||||
DigitalInput::DigitalInput(Participant* participant, unsigned char pin)
|
||||
: TouchSensor(participant) {
|
||||
this->pin = pin;
|
||||
DigitalInput::DigitalInput(Participant* participant, unsigned char pin) : TouchSensor(participant) {
|
||||
this->pin = pin;
|
||||
|
||||
pinMode(pin, INPUT);
|
||||
pinMode(pin, INPUT);
|
||||
}
|
||||
|
||||
void DigitalInput::Update(unsigned long currentTimeMs, bool recursive) {
|
||||
this->touchedSomething = digitalRead(pin) == LOW;
|
||||
// std::cout << "DigitalINput pin " << (int)this->pin << ": " <<
|
||||
// this->touchedSomething << "\n";
|
||||
Thing::Update(currentTimeMs, recursive);
|
||||
void DigitalInput::Update(unsigned long currentTimeMs) {
|
||||
this->touchedSomething = digitalRead(pin) == LOW;
|
||||
|
||||
// std::cout << "DigitalINput pin " << (int)this->pin << ": " << this->touchedSomething << "\n";
|
||||
}
|
||||
|
||||
} // namespace Arduino
|
||||
|
@ -13,9 +13,8 @@ class DigitalInput : public TouchSensor {
|
||||
/// @param pin The digital pin
|
||||
DigitalInput(Participant* participant, unsigned char pin);
|
||||
|
||||
/// @copydoc RoboidControl::Thing::Update(unsigned long currentTimeMs)
|
||||
virtual void Update(unsigned long currentTimeMs,
|
||||
bool recursive = false) override;
|
||||
/// @copydoc RoboidControl::Thing::Update(unsigned long currentTimeMs)
|
||||
virtual void Update(unsigned long currentTimeMs) override;
|
||||
|
||||
protected:
|
||||
/// @brief The pin used for digital input
|
||||
|
@ -5,9 +5,7 @@
|
||||
namespace RoboidControl {
|
||||
namespace Arduino {
|
||||
|
||||
UltrasonicSensor::UltrasonicSensor(Participant* participant,
|
||||
unsigned char pinTrigger,
|
||||
unsigned char pinEcho)
|
||||
UltrasonicSensor::UltrasonicSensor(Participant* participant, unsigned char pinTrigger, unsigned char pinEcho)
|
||||
: TouchSensor(participant) {
|
||||
this->pinTrigger = pinTrigger;
|
||||
this->pinEcho = pinEcho;
|
||||
@ -25,8 +23,7 @@ float UltrasonicSensor::GetDistance() {
|
||||
digitalWrite(pinTrigger, LOW);
|
||||
|
||||
// Measure the duration of the pulse on the echo pin
|
||||
float duration_us =
|
||||
pulseIn(pinEcho, HIGH, 100000); // the result is in microseconds
|
||||
float duration_us = pulseIn(pinEcho, HIGH, 100000); // the result is in microseconds
|
||||
|
||||
// Calculate the distance:
|
||||
// * Duration should be divided by 2, because the ping goes to the object
|
||||
@ -46,16 +43,14 @@ float UltrasonicSensor::GetDistance() {
|
||||
|
||||
this->touchedSomething |= (this->distance <= this->touchDistance);
|
||||
|
||||
// std::cout << "Ultrasonic " << this->distance << " " <<
|
||||
// this->touchedSomething << "\n";
|
||||
// std::cout << "Ultrasonic " << this->distance << " " << this->touchedSomething << "\n";
|
||||
|
||||
return distance;
|
||||
}
|
||||
|
||||
void UltrasonicSensor::Update(unsigned long currentTimeMs, bool recursive) {
|
||||
void UltrasonicSensor::Update(unsigned long currentTimeMs) {
|
||||
this->touchedSomething = false;
|
||||
GetDistance();
|
||||
Thing::Update(currentTimeMs, recursive);
|
||||
}
|
||||
|
||||
// void UltrasonicSensor::ProcessBinary(char* bytes) {
|
||||
|
@ -12,9 +12,7 @@ class UltrasonicSensor : public TouchSensor {
|
||||
/// @param participant The participant to use
|
||||
/// @param pinTrigger The pin number of the trigger signal
|
||||
/// @param pinEcho The pin number of the echo signal
|
||||
UltrasonicSensor(Participant* participant,
|
||||
unsigned char pinTrigger,
|
||||
unsigned char pinEcho);
|
||||
UltrasonicSensor(Participant* participant, unsigned char pinTrigger, unsigned char pinEcho);
|
||||
|
||||
// parameters
|
||||
|
||||
@ -25,14 +23,12 @@ class UltrasonicSensor : public TouchSensor {
|
||||
|
||||
/// @brief The last read distance
|
||||
float distance = 0;
|
||||
/// @brief erform an ultrasonic 'ping' to determine the distance to the
|
||||
/// nearest object
|
||||
/// @brief erform an ultrasonic 'ping' to determine the distance to the nearest object
|
||||
/// @return the measured distance in meters to the nearest object
|
||||
float GetDistance();
|
||||
|
||||
/// @copydoc RoboidControl::Thing::Update(unsigned long currentTimeMs)
|
||||
virtual void Update(unsigned long currentTimeMs,
|
||||
bool recursive = false) override;
|
||||
virtual void Update(unsigned long currentTimeMs) override;
|
||||
|
||||
protected:
|
||||
/// @brief The pin number of the trigger signal
|
||||
|
@ -2,18 +2,12 @@
|
||||
#include "Things/DifferentialDrive.h"
|
||||
#include "Things/TouchSensor.h"
|
||||
|
||||
#if defined(ARDUINO)
|
||||
#include "Arduino.h"
|
||||
|
||||
#else
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
using namespace RoboidControl;
|
||||
using namespace std::this_thread;
|
||||
using namespace std::chrono;
|
||||
#endif
|
||||
|
||||
using namespace RoboidControl;
|
||||
|
||||
int main() {
|
||||
// The robot's propulsion is a differential drive
|
||||
@ -39,11 +33,7 @@ int main() {
|
||||
bb2b->Update(true);
|
||||
|
||||
// and sleep for 100ms
|
||||
#if defined(ARDUINO)
|
||||
delay(100);
|
||||
#else
|
||||
sleep_for(milliseconds(100));
|
||||
#endif
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -5,23 +5,21 @@
|
||||
#include "Arduino/ArduinoParticipant.h"
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
#include "Windows/WindowsParticipant.h"
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include "Windows/WindowsParticipant.h"
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
#include "Posix/PosixParticipant.h"
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h> // For fcntl
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#include <chrono>
|
||||
#include "Posix/PosixParticipant.h"
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
|
||||
namespace RoboidControl {
|
||||
|
||||
// LocalParticipant::LocalParticipant() {}
|
||||
@ -34,8 +32,7 @@ LocalParticipant::LocalParticipant(int port) {
|
||||
}
|
||||
|
||||
LocalParticipant::LocalParticipant(const char* ipAddress, int port) {
|
||||
this->ipAddress = "0.0.0.0"; // ipAddress; // maybe this is not needed
|
||||
// anymore, keeping it to "0.0.0.0"
|
||||
this->ipAddress = "0.0.0.0"; // ipAddress; // maybe this is not needed anymore, keeping it to "0.0.0.0"
|
||||
this->port = port;
|
||||
if (this->port == 0)
|
||||
this->isIsolated = true;
|
||||
@ -58,20 +55,15 @@ void LocalParticipant::begin() {
|
||||
SetupUDP(this->port, this->ipAddress, this->port);
|
||||
}
|
||||
|
||||
void LocalParticipant::SetupUDP(int localPort,
|
||||
const char* remoteIpAddress,
|
||||
int remotePort) {
|
||||
void LocalParticipant::SetupUDP(int localPort, const char* remoteIpAddress, int remotePort) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
Windows::LocalParticipant* thisWindows =
|
||||
static_cast<Windows::LocalParticipant*>(this);
|
||||
Windows::LocalParticipant* thisWindows = static_cast<Windows::LocalParticipant*>(this);
|
||||
thisWindows->Setup(localPort, remoteIpAddress, remotePort);
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
Posix::LocalParticipant* thisPosix =
|
||||
static_cast<Posix::LocalParticipant*>(this);
|
||||
Posix::LocalParticipant* thisPosix = static_cast<Posix::LocalParticipant*>(this);
|
||||
thisPosix->Setup(localPort, remoteIpAddress, remotePort);
|
||||
#elif defined(ARDUINO)
|
||||
Arduino::LocalParticipant* thisArduino =
|
||||
static_cast<Arduino::LocalParticipant*>(this);
|
||||
Arduino::LocalParticipant* thisArduino = static_cast<Arduino::LocalParticipant*>(this);
|
||||
thisArduino->Setup(localPort, remoteIpAddress, remotePort);
|
||||
#endif
|
||||
this->connected = true;
|
||||
@ -79,15 +71,13 @@ void LocalParticipant::SetupUDP(int localPort,
|
||||
|
||||
void LocalParticipant::Update(unsigned long currentTimeMs) {
|
||||
if (currentTimeMs == 0) {
|
||||
currentTimeMs = Thing::GetTimeMs();
|
||||
// #if defined(ARDUINO)
|
||||
// currentTimeMs = millis();
|
||||
// #elif defined(__unix__) || defined(__APPLE__)
|
||||
// auto now = std::chrono::steady_clock::now();
|
||||
// auto ms =
|
||||
// std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
|
||||
// currentTimeMs = static_cast<unsigned long>(ms.count());
|
||||
// #endif
|
||||
#if defined(ARDUINO)
|
||||
currentTimeMs = millis();
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
|
||||
currentTimeMs = static_cast<unsigned long>(ms.count());
|
||||
#endif
|
||||
}
|
||||
|
||||
if (this->isIsolated == false) {
|
||||
@ -122,16 +112,13 @@ void LocalParticipant::Update(unsigned long currentTimeMs) {
|
||||
|
||||
void LocalParticipant::ReceiveUDP() {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
Windows::LocalParticipant* thisWindows =
|
||||
static_cast<Windows::LocalParticipant*>(this);
|
||||
Windows::LocalParticipant* thisWindows = static_cast<Windows::LocalParticipant*>(this);
|
||||
thisWindows->Receive();
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
Posix::LocalParticipant* thisPosix =
|
||||
static_cast<Posix::LocalParticipant*>(this);
|
||||
Posix::LocalParticipant* thisPosix = static_cast<Posix::LocalParticipant*>(this);
|
||||
thisPosix->Receive();
|
||||
#elif defined(ARDUINO)
|
||||
Arduino::LocalParticipant* thisArduino =
|
||||
static_cast<Arduino::LocalParticipant*>(this);
|
||||
Arduino::LocalParticipant* thisArduino = static_cast<Arduino::LocalParticipant*>(this);
|
||||
thisArduino->Receive();
|
||||
#endif
|
||||
}
|
||||
@ -145,23 +132,17 @@ Participant* LocalParticipant::GetParticipant(const char* ipAddress, int port) {
|
||||
}
|
||||
|
||||
Participant* LocalParticipant::AddParticipant(const char* ipAddress, int port) {
|
||||
// std::cout << "New Participant " << ipAddress << ":" << port << "\n";
|
||||
std::cout << "New Participant " << ipAddress << ":" << port << "\n";
|
||||
Participant* participant = new Participant(ipAddress, port);
|
||||
#if defined(NO_STD)
|
||||
participant->networkId = this->senderCount;
|
||||
this->senders[this->senderCount++] = participant;
|
||||
#else
|
||||
participant->networkId = (unsigned char)this->senders.size();
|
||||
this->senders.push_back(participant);
|
||||
#endif
|
||||
return participant;
|
||||
}
|
||||
|
||||
#pragma region Send
|
||||
|
||||
void LocalParticipant::SendThingInfo(Participant* remoteParticipant,
|
||||
Thing* thing) {
|
||||
// std::cout << "Send thing info " << (int)thing->id << " \n";
|
||||
void LocalParticipant::SendThingInfo(Participant* remoteParticipant, Thing* thing) {
|
||||
std::cout << "Send thing info " << (int)thing->id << " \n";
|
||||
ThingMsg* thingMsg = new ThingMsg(this->networkId, thing);
|
||||
this->Send(remoteParticipant, thingMsg);
|
||||
delete thingMsg;
|
||||
@ -185,16 +166,13 @@ bool LocalParticipant::Send(Participant* remoteParticipant, IMessage* msg) {
|
||||
return true;
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
Windows::LocalParticipant* thisWindows =
|
||||
static_cast<Windows::LocalParticipant*>(this);
|
||||
Windows::LocalParticipant* thisWindows = static_cast<Windows::LocalParticipant*>(this);
|
||||
return thisWindows->Send(remoteParticipant, bufferSize);
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
Posix::LocalParticipant* thisPosix =
|
||||
static_cast<Posix::LocalParticipant*>(this);
|
||||
Posix::LocalParticipant* thisPosix = static_cast<Posix::LocalParticipant*>(this);
|
||||
return thisPosix->Send(remoteParticipant, bufferSize);
|
||||
#elif defined(ARDUINO)
|
||||
Arduino::LocalParticipant* thisArduino =
|
||||
static_cast<Arduino::LocalParticipant*>(this);
|
||||
Arduino::LocalParticipant* thisArduino = static_cast<Arduino::LocalParticipant*>(this);
|
||||
return thisArduino->Send(remoteParticipant, bufferSize);
|
||||
#endif
|
||||
}
|
||||
@ -222,16 +200,13 @@ void LocalParticipant::PublishThingInfo(Thing* thing) {
|
||||
|
||||
bool LocalParticipant::Publish(IMessage* msg) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
Windows::LocalParticipant* thisWindows =
|
||||
static_cast<Windows::LocalParticipant*>(this);
|
||||
Windows::LocalParticipant* thisWindows = static_cast<Windows::LocalParticipant*>(this);
|
||||
return thisWindows->Publish(msg);
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
Posix::LocalParticipant* thisPosix =
|
||||
static_cast<Posix::LocalParticipant*>(this);
|
||||
Posix::LocalParticipant* thisPosix = static_cast<Posix::LocalParticipant*>(this);
|
||||
return thisPosix->Publish(msg);
|
||||
#elif defined(ARDUINO)
|
||||
Arduino::LocalParticipant* thisArduino =
|
||||
static_cast<Arduino::LocalParticipant*>(this);
|
||||
Arduino::LocalParticipant* thisArduino = static_cast<Arduino::LocalParticipant*>(this);
|
||||
return thisArduino->Publish(msg);
|
||||
#endif
|
||||
}
|
||||
@ -241,11 +216,8 @@ bool LocalParticipant::Publish(IMessage* msg) {
|
||||
|
||||
#pragma region Receive
|
||||
|
||||
void LocalParticipant::ReceiveData(unsigned char packetSize,
|
||||
char* senderIpAddress,
|
||||
unsigned int senderPort) {
|
||||
Participant* remoteParticipant =
|
||||
this->GetParticipant(senderIpAddress, senderPort);
|
||||
void LocalParticipant::ReceiveData(unsigned char packetSize, char* senderIpAddress, unsigned int senderPort) {
|
||||
Participant* remoteParticipant = this->GetParticipant(senderIpAddress, senderPort);
|
||||
if (remoteParticipant == nullptr) {
|
||||
remoteParticipant = this->AddParticipant(senderIpAddress, senderPort);
|
||||
// std::cout << "New sender " << sender_ipAddress << ":" << sender_port
|
||||
@ -258,8 +230,7 @@ void LocalParticipant::ReceiveData(unsigned char packetSize,
|
||||
ReceiveData(packetSize, remoteParticipant);
|
||||
}
|
||||
|
||||
void LocalParticipant::ReceiveData(unsigned char bufferSize,
|
||||
Participant* remoteParticipant) {
|
||||
void LocalParticipant::ReceiveData(unsigned char bufferSize, Participant* remoteParticipant) {
|
||||
unsigned char msgId = this->buffer[0];
|
||||
// std::cout << "receive msg " << (int)msgId << "\n";
|
||||
switch (msgId) {
|
||||
@ -304,11 +275,10 @@ void LocalParticipant::ReceiveData(unsigned char bufferSize,
|
||||
void LocalParticipant::Process(Participant* sender, ParticipantMsg* msg) {}
|
||||
|
||||
void LocalParticipant::Process(Participant* sender, SiteMsg* msg) {
|
||||
// std::cout << this->name << ": process NetworkId [" << (int)this->networkId
|
||||
// << "/" << (int)msg->networkId << "]\n";
|
||||
std::cout << this->name << ": process NetworkId [" << (int)this->networkId << "/" << (int)msg->networkId << "]\n";
|
||||
if (this->networkId != msg->networkId) {
|
||||
this->networkId = msg->networkId;
|
||||
// std::cout << this->things.size() << " things\n";
|
||||
std::cout << this->things.size() << " things\n";
|
||||
for (Thing* thing : this->things)
|
||||
this->SendThingInfo(sender, thing);
|
||||
}
|
||||
@ -325,27 +295,22 @@ void LocalParticipant::Process(Participant* sender, NameMsg* msg) {
|
||||
int stringLen = nameLength + 1;
|
||||
char* thingName = new char[stringLen];
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
strncpy_s(thingName, stringLen, msg->name,
|
||||
stringLen - 1); // Leave space for null terminator
|
||||
strncpy_s(thingName, stringLen, msg->name, stringLen - 1); // Leave space for null terminator
|
||||
#else
|
||||
// Use strncpy with bounds checking for other platforms (Arduino, POSIX,
|
||||
// ESP-IDF)
|
||||
strncpy(thingName, msg->name,
|
||||
stringLen - 1); // Leave space for null terminator
|
||||
thingName[stringLen - 1] = '\0'; // Ensure null termination
|
||||
// Use strncpy with bounds checking for other platforms (Arduino, POSIX, ESP-IDF)
|
||||
strncpy(thingName, msg->name, stringLen - 1); // Leave space for null terminator
|
||||
thingName[stringLen - 1] = '\0'; // Ensure null termination
|
||||
#endif
|
||||
thingName[nameLength] = '\0';
|
||||
thing->name = thingName;
|
||||
// std::cout << "thing name = " << thing->name << " length = " << nameLength
|
||||
// << "\n";
|
||||
std::cout << "thing name = " << thing->name << " length = " << nameLength << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
void LocalParticipant::Process(Participant* sender, PoseMsg* msg) {}
|
||||
|
||||
void LocalParticipant::Process(Participant* sender, BinaryMsg* msg) {
|
||||
// std::cout << this->name << ": process Binary [" << (int)this->networkId <<
|
||||
// "/"
|
||||
// std::cout << this->name << ": process Binary [" << (int)this->networkId << "/"
|
||||
// << (int)msg->networkId << "]\n";
|
||||
Thing* thing = sender->Get(msg->networkId, msg->thingId);
|
||||
if (thing != nullptr)
|
||||
@ -354,9 +319,8 @@ void LocalParticipant::Process(Participant* sender, BinaryMsg* msg) {
|
||||
thing = this->Get(msg->networkId, msg->thingId);
|
||||
if (thing != nullptr)
|
||||
thing->ProcessBinary(msg->bytes);
|
||||
// else
|
||||
// std::cout << "custom msg for unknown thing [" << (int)msg->networkId
|
||||
// << "/" << (int)msg->thingId << "]\n";
|
||||
else
|
||||
std::cout << "custom msg for unknown thing [" << (int)msg->networkId << "/" << (int)msg->thingId << "]\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,9 +10,7 @@
|
||||
#include "Messages/ThingMsg.h"
|
||||
#include "Participant.h"
|
||||
|
||||
#if !defined(NO_STD)
|
||||
#include <list>
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
#include <winsock2.h>
|
||||
@ -22,41 +20,34 @@
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#elif defined(ARDUINO)
|
||||
// #include <WiFiUdp.h>
|
||||
#include <WiFiUdp.h>
|
||||
#endif
|
||||
|
||||
namespace RoboidControl {
|
||||
|
||||
constexpr int MAX_SENDER_COUNT = 256;
|
||||
|
||||
/// @brief A local participant is the local device which can communicate with
|
||||
/// other participants It manages all local things and communcation with other
|
||||
/// participants. Each application has a local participant which is usually
|
||||
/// explicit in the code. An participant can be isolated. In that case it is
|
||||
/// standalong and does not communicate with other participants.
|
||||
/// @brief A local participant is the local device which can communicate with other participants
|
||||
/// It manages all local things and communcation with other participants.
|
||||
/// Each application has a local participant which is usually explicit in the code.
|
||||
/// An participant can be isolated. In that case it is standalong and does not communicate with other participants.
|
||||
///
|
||||
/// It is possible to work with an hidden participant by creating things without
|
||||
/// specifying a participant in the constructor. In that case an hidden isolated
|
||||
/// participant is created which can be obtained using
|
||||
/// RoboidControl::LocalParticipant::Isolated().
|
||||
/// It is possible to work with an hidden participant by creating things without specifying a participant in the
|
||||
/// constructor. In that case an hidden isolated participant is created which can be
|
||||
/// obtained using RoboidControl::LocalParticipant::Isolated().
|
||||
/// @sa RoboidControl::Thing::Thing()
|
||||
class LocalParticipant : public Participant {
|
||||
public:
|
||||
/// @brief Create a participant without connecting to a site
|
||||
/// @param port The port on which the participant communicates
|
||||
/// These participant typically broadcast Participant messages to let site
|
||||
/// servers on the local network know their presence. Alternatively they can
|
||||
/// broadcast information which can be used directly by other participants.
|
||||
/// These participant typically broadcast Participant messages to let site servers on the local network know their presence.
|
||||
/// Alternatively they can broadcast information which can be used directly by other participants.
|
||||
LocalParticipant(int port = 7681);
|
||||
/// @brief Create a participant which will try to connect to a site.
|
||||
/// @param ipAddress The IP address of the site
|
||||
/// @param port The port used by the site
|
||||
LocalParticipant(const char* ipAddress, int port = 7681);
|
||||
// Note to self: one cannot specify the port used by the local participant
|
||||
// now!!
|
||||
// Note to self: one cannot specify the port used by the local participant now!!
|
||||
|
||||
/// @brief Isolated participant is used when the application is run without
|
||||
/// networking
|
||||
/// @brief Isolated participant is used when the application is run without networking
|
||||
/// @return A participant without networking support
|
||||
static LocalParticipant* Isolated();
|
||||
|
||||
@ -64,8 +55,7 @@ class LocalParticipant : public Participant {
|
||||
/// Isolated participants do not communicate with other participants
|
||||
bool isIsolated = false;
|
||||
|
||||
/// The interval in milliseconds for publishing (broadcasting) data on the
|
||||
/// local network
|
||||
/// The interval in milliseconds for publishing (broadcasting) data on the local network
|
||||
long publishInterval = 3000; // 3 seconds
|
||||
|
||||
/// @brief The name of the participant
|
||||
@ -77,11 +67,11 @@ class LocalParticipant : public Participant {
|
||||
Participant* remoteSite = nullptr;
|
||||
|
||||
#if defined(ARDUINO)
|
||||
// const char* remoteIpAddress = nullptr;
|
||||
// unsigned short remotePort = 0;
|
||||
// char* broadcastIpAddress = nullptr;
|
||||
const char* remoteIpAddress = nullptr;
|
||||
unsigned short remotePort = 0;
|
||||
char* broadcastIpAddress = nullptr;
|
||||
|
||||
// WiFiUDP udp;
|
||||
WiFiUDP udp;
|
||||
#else
|
||||
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
@ -104,17 +94,10 @@ class LocalParticipant : public Participant {
|
||||
bool Send(Participant* remoteParticipant, IMessage* msg);
|
||||
bool Publish(IMessage* msg);
|
||||
|
||||
void ReceiveData(unsigned char bufferSize,
|
||||
char* senderIpAddress,
|
||||
unsigned int senderPort);
|
||||
void ReceiveData(unsigned char bufferSize, char* senderIpAddress, unsigned int senderPort);
|
||||
void ReceiveData(unsigned char bufferSize, Participant* remoteParticipant);
|
||||
|
||||
#if defined(NO_STD)
|
||||
unsigned char senderCount = 0;
|
||||
Participant* senders[MAX_SENDER_COUNT];
|
||||
#else
|
||||
std::list<Participant*> senders;
|
||||
#endif
|
||||
|
||||
protected:
|
||||
unsigned long nextPublishMe = 0;
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "LowLevelMessages.h"
|
||||
|
||||
// #include <iostream>
|
||||
#include <iostream>
|
||||
#include "LinearAlgebra/float16.h"
|
||||
|
||||
namespace RoboidControl {
|
||||
|
@ -42,24 +42,15 @@ Thing* Participant::Get(unsigned char networkId, unsigned char thingId) {
|
||||
void Participant::Add(Thing* thing, bool checkId) {
|
||||
if (checkId && thing->id == 0) {
|
||||
// allocate a new thing ID
|
||||
#if defined(NO_STD)
|
||||
thing->id = this->thingCount + 1;
|
||||
this->things[this->thingCount++] = thing;
|
||||
#else
|
||||
thing->id = (unsigned char)this->things.size() + 1;
|
||||
this->things.push_back(thing);
|
||||
#endif
|
||||
// std::cout << "Add thing with generated ID " << this->ipAddress << ":" <<
|
||||
// this->port << "[" << (int)thing->networkId << "/"
|
||||
// << (int)thing->id << "]\n";
|
||||
} else {
|
||||
Thing* foundThing = Get(thing->networkId, thing->id);
|
||||
if (foundThing == nullptr) {
|
||||
#if defined(NO_STD)
|
||||
this->things[this->thingCount++] = thing;
|
||||
#else
|
||||
this->things.push_back(thing);
|
||||
#endif
|
||||
// std::cout << "Add thing " << this->ipAddress << ":" << this->port <<
|
||||
// "[" << (int)thing->networkId << "/"
|
||||
// << (int)thing->id << "]\n";
|
||||
@ -72,24 +63,9 @@ void Participant::Add(Thing* thing, bool checkId) {
|
||||
}
|
||||
|
||||
void Participant::Remove(Thing* thing) {
|
||||
#if defined(NO_STD)
|
||||
for (unsigned char thingIx = 0; thingIx < this->thingCount; thingIx++)
|
||||
if (this->things[thingIx] == thing)
|
||||
this->things[thingIx] = nullptr;
|
||||
// compacting
|
||||
unsigned char lastThingIx = 0;
|
||||
for (unsigned char thingIx = 0; thingIx < this->thingCount; thingIx++) {
|
||||
if (this->things[thingIx] == nullptr)
|
||||
continue;
|
||||
this->things[lastThingIx] = this->things[thingIx];
|
||||
lastThingIx++;
|
||||
}
|
||||
this->thingCount = lastThingIx;
|
||||
#else
|
||||
this->things.remove_if([thing](Thing* obj) { return obj == thing; });
|
||||
std::cout << "Removing " << thing->networkId << "/" << thing->id
|
||||
<< " list size = " << this->things.size() << "\n";
|
||||
#endif
|
||||
}
|
||||
|
||||
// void Participant::UpdateAll(unsigned long currentTimeMs) {
|
||||
|
@ -3,21 +3,16 @@
|
||||
|
||||
namespace RoboidControl {
|
||||
|
||||
constexpr int MAX_THING_COUNT = 256;
|
||||
|
||||
/// @brief A participant is a device which manages things.
|
||||
/// It can communicate with other participant to synchronise the state of
|
||||
/// things. This class is used to register the things the participant is
|
||||
/// managing. It also maintains the communcation information to contact the
|
||||
/// participant. It is used as a basis for the local participant, but also as a
|
||||
/// reference to remote participants.
|
||||
/// It can communicate with other participant to synchronise the state of things.
|
||||
/// This class is used to register the things the participant is managing.
|
||||
/// It also maintains the communcation information to contact the participant.
|
||||
/// It is used as a basis for the local participant, but also as a reference to remote participants.
|
||||
class Participant {
|
||||
public:
|
||||
/// @brief The Ip Address of a participant. When the participant is local,
|
||||
/// this contains 0.0.0.0
|
||||
const char* ipAddress = "0.0.0.0";
|
||||
/// @brief The port number for UDP communication with the participant. This is
|
||||
/// 0 for isolated participants.
|
||||
public:
|
||||
/// @brief The Ip Address of a participant. When the participant is local, this contains 0.0.0.0
|
||||
const char *ipAddress = "0.0.0.0";
|
||||
/// @brief The port number for UDP communication with the participant. This is 0 for isolated participants.
|
||||
int port = 0;
|
||||
|
||||
/// @brief The network Id to identify the participant.
|
||||
@ -29,34 +24,29 @@ class Participant {
|
||||
/// @brief Create a new participant with the given communcation info
|
||||
/// @param ipAddress The IP address of the participant
|
||||
/// @param port The port of the participant
|
||||
Participant(const char* ipAddress, int port);
|
||||
Participant(const char *ipAddress, int port);
|
||||
/// @brief Destructor for the participant
|
||||
~Participant();
|
||||
|
||||
protected:
|
||||
#if defined(NO_STD)
|
||||
unsigned char thingCount = 0;
|
||||
Thing* things[MAX_THING_COUNT];
|
||||
#else
|
||||
protected:
|
||||
/// @brief The list of things managed by this participant
|
||||
std::list<Thing*> things;
|
||||
#endif
|
||||
std::list<Thing *> things;
|
||||
|
||||
public:
|
||||
public:
|
||||
/// @brief Find a thing managed by this participant
|
||||
/// @param networkId The network ID for the thing
|
||||
/// @param thingId The ID of the thing
|
||||
/// @return The thing if found or nullptr when no thing has been found
|
||||
/// @note The use of the network ID is likely to disappear in future versions.
|
||||
Thing* Get(unsigned char networkId, unsigned char thingId);
|
||||
/// @brief Add a new thing for this participant.
|
||||
Thing *Get(unsigned char networkId, unsigned char thingId);
|
||||
/// @brief Add a new thing for this participant.
|
||||
/// @param thing The thing to add
|
||||
/// @param checkId Checks the thing ID of the thing. If it is 0, a new thing
|
||||
/// Id will be assigned.
|
||||
void Add(Thing* thing, bool checkId = true);
|
||||
/// @param checkId Checks the thing ID of the thing. If it is 0, a new thing Id will be assigned.
|
||||
void Add(Thing *thing, bool checkId = true);
|
||||
/// @brief Remove a thing for this participant
|
||||
/// @param thing The thing to remove
|
||||
void Remove(Thing* thing);
|
||||
void Remove(Thing *thing);
|
||||
|
||||
};
|
||||
|
||||
} // namespace RoboidControl
|
||||
} // namespace Control
|
||||
|
@ -9,9 +9,6 @@ Supporting:
|
||||
- ESP8266
|
||||
- ESP32
|
||||
- UNO R4 WiFi
|
||||
- ESP8266
|
||||
- ESP32
|
||||
- UNO R4 WiFi
|
||||
|
||||
# Basic components
|
||||
|
||||
|
@ -2,10 +2,8 @@
|
||||
|
||||
#include "Things/TemperatureSensor.h"
|
||||
|
||||
#if !defined(NO_STD)
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#endif
|
||||
|
||||
namespace RoboidControl {
|
||||
|
||||
@ -16,49 +14,37 @@ SiteServer::SiteServer(int port) {
|
||||
this->ipAddress = "0.0.0.0";
|
||||
this->port = port;
|
||||
|
||||
#if defined(NO_STD)
|
||||
this->senders[this->senderCount++] = this;
|
||||
#else
|
||||
this->senders.push_back(this);
|
||||
#endif
|
||||
|
||||
SetupUDP(port, ipAddress, 0);
|
||||
|
||||
#if !defined(NO_STD)
|
||||
Register<TemperatureSensor>((unsigned char)Thing::Type::TemperatureSensor);
|
||||
#endif
|
||||
}
|
||||
|
||||
void SiteServer::Process(Participant* sender, ParticipantMsg* msg) {
|
||||
void SiteServer::Process(Participant *sender, ParticipantMsg *msg) {
|
||||
if (msg->networkId == 0) {
|
||||
// std::cout << this->name << " received New Client -> " <<
|
||||
// sender->ipAddress
|
||||
// << ":" << (int)sender->port << "\n";
|
||||
SiteMsg* msg = new SiteMsg(sender->networkId);
|
||||
std::cout << this->name << " received New Client -> " << sender->ipAddress
|
||||
<< ":" << (int)sender->port << "\n";
|
||||
SiteMsg *msg = new SiteMsg(sender->networkId);
|
||||
this->Send(sender, msg);
|
||||
delete msg;
|
||||
}
|
||||
}
|
||||
|
||||
void SiteServer::Process(Participant* sender, SiteMsg* msg) {}
|
||||
void SiteServer::Process(Participant *sender, SiteMsg *msg) {}
|
||||
|
||||
void SiteServer::Process(Participant* sender, ThingMsg* msg) {
|
||||
Thing* thing = sender->Get(msg->networkId, msg->thingId);
|
||||
void SiteServer::Process(Participant *sender, ThingMsg *msg) {
|
||||
Thing *thing = sender->Get(msg->networkId, msg->thingId);
|
||||
if (thing == nullptr) {
|
||||
#if defined(NO_STD)
|
||||
new Thing(sender, msg->networkId, msg->thingId,
|
||||
(Thing::Type)msg->thingType);
|
||||
#else
|
||||
auto thingMsgProcessor = thingMsgProcessors.find(msg->thingType);
|
||||
Thing* newThing;
|
||||
if (thingMsgProcessor != thingMsgProcessors.end()) // found item
|
||||
newThing =
|
||||
thingMsgProcessor->second(sender, msg->networkId, msg->thingId);
|
||||
Thing *newThing;
|
||||
if (thingMsgProcessor != thingMsgProcessors.end()) // found item
|
||||
newThing = thingMsgProcessor->second(sender, msg->networkId, msg->thingId);
|
||||
else
|
||||
newThing = new Thing(sender, msg->networkId, msg->thingId,
|
||||
(Thing::Type)msg->thingType);
|
||||
#endif
|
||||
//sender->Add(newThing);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace RoboidControl
|
||||
} // namespace Control
|
||||
|
14
SiteServer.h
14
SiteServer.h
@ -2,11 +2,9 @@
|
||||
|
||||
#include "LocalParticipant.h"
|
||||
|
||||
#if !defined(NO_STD)
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#endif
|
||||
|
||||
namespace RoboidControl {
|
||||
|
||||
@ -17,16 +15,12 @@ class SiteServer : public LocalParticipant {
|
||||
|
||||
// virtual void Update(unsigned long currentTimeMs = 0) override;
|
||||
|
||||
#if !defined(NO_STD)
|
||||
template <typename ThingClass>
|
||||
void Register(unsigned char thingType) {
|
||||
thingMsgProcessors[thingType] = [](Participant* participant,
|
||||
unsigned char networkId,
|
||||
unsigned char thingId) {
|
||||
thingMsgProcessors[thingType] = [](Participant* participant, unsigned char networkId, unsigned char thingId) {
|
||||
return new ThingClass(participant, networkId, thingId);
|
||||
};
|
||||
};
|
||||
#endif
|
||||
|
||||
protected:
|
||||
unsigned long nextPublishMe = 0;
|
||||
@ -35,12 +29,8 @@ class SiteServer : public LocalParticipant {
|
||||
virtual void Process(Participant* sender, SiteMsg* msg) override;
|
||||
virtual void Process(Participant* sender, ThingMsg* msg) override;
|
||||
|
||||
#if !defined(NO_STD)
|
||||
using ThingConstructor = std::function<Thing*(Participant* participant,
|
||||
unsigned char networkId,
|
||||
unsigned char thingId)>;
|
||||
using ThingConstructor = std::function<Thing*(Participant* participant, unsigned char networkId, unsigned char thingId)>;
|
||||
std::unordered_map<unsigned char, ThingConstructor> thingMsgProcessors;
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace RoboidControl
|
||||
|
24
Thing.cpp
24
Thing.cpp
@ -1,16 +1,12 @@
|
||||
#include "Thing.h"
|
||||
|
||||
#include "LocalParticipant.h"
|
||||
|
||||
#include <string.h>
|
||||
// #include <algorithm>
|
||||
// #include <iostream>
|
||||
// #include <list>
|
||||
// #include <chrono>
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
|
||||
#if defined(ARDUINO)
|
||||
#include "Arduino.h"
|
||||
#endif
|
||||
#include "LocalParticipant.h"
|
||||
|
||||
namespace RoboidControl {
|
||||
|
||||
@ -173,18 +169,18 @@ void Thing::SetModel(const char* url) {
|
||||
}
|
||||
|
||||
unsigned long Thing::GetTimeMs() {
|
||||
#if defined(ARDUINO)
|
||||
return millis();
|
||||
#else
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
now.time_since_epoch());
|
||||
return static_cast<unsigned long>(ms.count());
|
||||
#endif
|
||||
}
|
||||
|
||||
void Thing::Update(bool recursive) {
|
||||
#if defined(ARDUINO)
|
||||
Update(millis());
|
||||
#else
|
||||
Update(GetTimeMs(), recursive);
|
||||
#endif
|
||||
}
|
||||
|
||||
void Thing::Update(unsigned long currentTimeMs, bool recursive) {
|
||||
@ -195,7 +191,7 @@ void Thing::Update(unsigned long currentTimeMs, bool recursive) {
|
||||
if (child == nullptr)
|
||||
continue;
|
||||
child->Update(currentTimeMs, recursive);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
3
Thing.h
3
Thing.h
@ -1,9 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#if !defined(NO_STD)
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#endif
|
||||
#include "LinearAlgebra/Spherical.h"
|
||||
#include "LinearAlgebra/SwingTwist.h"
|
||||
|
||||
|
@ -24,7 +24,6 @@ void DifferentialDrive::SetDriveDimensions(float wheelDiameter,
|
||||
void DifferentialDrive::SetMotors(Thing* leftWheel, Thing* rightWheel) {
|
||||
float distance = this->wheelSeparation / 2;
|
||||
this->leftWheel = leftWheel;
|
||||
;
|
||||
if (leftWheel != nullptr)
|
||||
this->leftWheel->SetPosition(Spherical(distance, Direction::left));
|
||||
|
||||
|
@ -6,11 +6,9 @@ namespace RoboidControl {
|
||||
|
||||
// TemperatureSensor::TemperatureSensor() : Thing(Type::TemperatureSensor) {}
|
||||
|
||||
// TemperatureSensor::TemperatureSensor() : Thing(Type::TemperatureSensor) {}
|
||||
//TemperatureSensor::TemperatureSensor() : Thing(Type::TemperatureSensor) {}
|
||||
|
||||
TemperatureSensor::TemperatureSensor(Participant* participant,
|
||||
unsigned char networkId,
|
||||
unsigned char thingId)
|
||||
TemperatureSensor::TemperatureSensor(Participant* participant, unsigned char networkId, unsigned char thingId)
|
||||
: Thing(participant, networkId, thingId, Type::TemperatureSensor) {}
|
||||
|
||||
void TemperatureSensor::SetTemperature(float temp) {
|
||||
@ -18,7 +16,7 @@ void TemperatureSensor::SetTemperature(float temp) {
|
||||
}
|
||||
|
||||
void TemperatureSensor::GenerateBinary(char* buffer, unsigned char* ix) {
|
||||
// std::cout << "Send temperature: " << this->temperature << "\n";
|
||||
std::cout << "Send temperature: " << this->temperature << "\n";
|
||||
LowLevelMessages::SendFloat16(buffer, ix, this->temperature);
|
||||
}
|
||||
|
||||
|
@ -13,8 +13,8 @@ TouchSensor::TouchSensor(Thing* parent) : Thing(parent->owner) {
|
||||
void TouchSensor::GenerateBinary(char* bytes, unsigned char* ix) {}
|
||||
|
||||
void TouchSensor::ProcessBinary(char* bytes) {
|
||||
// if (bytes[0] == 1)
|
||||
// std::cout << this->name << " is Touching something!\n";
|
||||
if (bytes[0] == 1)
|
||||
std::cout << this->name << " is Touching something!\n";
|
||||
this->touchedSomething |= (bytes[0] == 1);
|
||||
}
|
||||
|
||||
|
20
library.json
20
library.json
@ -1,20 +0,0 @@
|
||||
{
|
||||
"name": "RoboidCOntrol",
|
||||
"version": "0.3.0",
|
||||
"description": "Controlling Roboids",
|
||||
"keywords": "robotics, networking",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Pascal Serrarens",
|
||||
"email": "pascal@passer.life"
|
||||
}
|
||||
],
|
||||
"license": "MPL",
|
||||
"src": {
|
||||
"include": "Thing.h",
|
||||
"src": [
|
||||
"Things/*.cpp",
|
||||
"Arduino/*.cpp"
|
||||
]
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user