Steps toward hierarchy changes
This commit is contained in:
parent
e56f25f9a1
commit
a292179f87
@ -14,7 +14,7 @@ DRV8833Motor::DRV8833Motor(DRV8833* driver,
|
|||||||
unsigned char pinIn2,
|
unsigned char pinIn2,
|
||||||
bool reverse)
|
bool reverse)
|
||||||
: Thing(driver->owner) {
|
: Thing(driver->owner) {
|
||||||
this->parent = driver;
|
this->SetParent(driver);
|
||||||
|
|
||||||
this->pinIn1 = pinIn1;
|
this->pinIn1 = pinIn1;
|
||||||
this->pinIn2 = pinIn2;
|
this->pinIn2 = pinIn2;
|
||||||
@ -144,7 +144,7 @@ DRV8833::DRV8833(Thing* parent,
|
|||||||
pinStandby,
|
pinStandby,
|
||||||
reverseA,
|
reverseA,
|
||||||
reverseB) {
|
reverseB) {
|
||||||
this->parent = parent;
|
this->SetParent(parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Arduino
|
} // namespace Arduino
|
||||||
|
@ -20,7 +20,7 @@ UltrasonicSensor::UltrasonicSensor(Thing* parent,
|
|||||||
unsigned char pinTrigger,
|
unsigned char pinTrigger,
|
||||||
unsigned char pinEcho)
|
unsigned char pinEcho)
|
||||||
: UltrasonicSensor(parent->owner, pinTrigger, pinEcho) {
|
: UltrasonicSensor(parent->owner, pinTrigger, pinEcho) {
|
||||||
this->parent = parent;
|
this->SetParent(parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
float UltrasonicSensor::GetDistance() {
|
float UltrasonicSensor::GetDistance() {
|
||||||
|
@ -8,11 +8,11 @@ PoseMsg::PoseMsg(unsigned char networkId, Thing* thing, bool force) {
|
|||||||
this->thingId = thing->id;
|
this->thingId = thing->id;
|
||||||
|
|
||||||
this->poseType = 0;
|
this->poseType = 0;
|
||||||
if (thing->positionUpdated || (force && thing->GetLinearVelocity().distance == 0)) {
|
if (thing->positionUpdated || (force && thing->GetParent() != nullptr)) {
|
||||||
this->position = thing->GetPosition();
|
this->position = thing->GetPosition();
|
||||||
this->poseType |= Pose_Position;
|
this->poseType |= Pose_Position;
|
||||||
}
|
}
|
||||||
if (thing->orientationUpdated || (force && thing->GetAngularVelocity().distance == 0)) {
|
if (thing->orientationUpdated || (force && thing->GetParent() != nullptr)) {
|
||||||
this->orientation = thing->GetOrientation();
|
this->orientation = thing->GetOrientation();
|
||||||
this->poseType |= Pose_Orientation;
|
this->poseType |= Pose_Orientation;
|
||||||
}
|
}
|
||||||
|
@ -45,8 +45,8 @@ Participant* Participant::GetParticipant(const char* ipAddress,
|
|||||||
participant->port == port)
|
participant->port == port)
|
||||||
return participant;
|
return participant;
|
||||||
}
|
}
|
||||||
std::cout << "Could not find participant " << ipAddress << ":" << (int)port
|
// std::cout << "Could not find participant " << ipAddress << ":" << (int)port
|
||||||
<< std::endl;
|
// << std::endl;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,7 +57,7 @@ Participant* Participant::GetParticipant(unsigned char participantId) {
|
|||||||
if (participant->networkId == participantId)
|
if (participant->networkId == participantId)
|
||||||
return participant;
|
return participant;
|
||||||
}
|
}
|
||||||
std::cout << "Could not find participant " << (int)participantId << std::endl;
|
// std::cout << "Could not find participant " << (int)participantId << std::endl;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,7 +104,15 @@ void Participant::Add(Thing* thing, bool checkId) {
|
|||||||
thing->id = this->thingCount + 1;
|
thing->id = this->thingCount + 1;
|
||||||
this->things[this->thingCount++] = thing;
|
this->things[this->thingCount++] = thing;
|
||||||
#else
|
#else
|
||||||
thing->id = (unsigned char)this->things.size() + 1;
|
// find highest id
|
||||||
|
int highestIx = 0;
|
||||||
|
for (Thing* thing : this->things) {
|
||||||
|
if (thing == nullptr)
|
||||||
|
continue;
|
||||||
|
if (thing->id > highestIx)
|
||||||
|
highestIx = thing->id;
|
||||||
|
}
|
||||||
|
thing->id = highestIx + 1;
|
||||||
this->things.push_back(thing);
|
this->things.push_back(thing);
|
||||||
#endif
|
#endif
|
||||||
// std::cout << "Add thing with generated ID " << this->ipAddress << ":"
|
// std::cout << "Add thing with generated ID " << this->ipAddress << ":"
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
#include "Arduino/ArduinoParticipant.h"
|
#include "Arduino/ArduinoParticipant.h"
|
||||||
#include "EspIdf/EspIdfParticipant.h"
|
#include "EspIdf/EspIdfParticipant.h"
|
||||||
|
|
||||||
|
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
@ -109,19 +108,29 @@ void ParticipantUDP::Update(unsigned long currentTimeMs) {
|
|||||||
|
|
||||||
void ParticipantUDP::UpdateMyThings(unsigned long currentTimeMs = 0) {
|
void ParticipantUDP::UpdateMyThings(unsigned long currentTimeMs = 0) {
|
||||||
for (Thing* thing : this->things) {
|
for (Thing* thing : this->things) {
|
||||||
if (thing == nullptr || thing->GetParent() != nullptr)
|
if (thing == nullptr) // || thing->GetParent() != nullptr)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
thing->Update(currentTimeMs, true);
|
if (thing->hierarchyChanged) {
|
||||||
|
std::cout << "thing hierarchy changed " << (int)thing->id << std::endl;
|
||||||
|
if (!(this->isIsolated || this->networkId == 0)) {
|
||||||
|
ThingMsg* thingMsg = new ThingMsg(this->networkId, thing);
|
||||||
|
this->Send(this->remoteSite, thingMsg);
|
||||||
|
delete thingMsg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (this->isIsolated || this->networkId == 0)
|
// Why don't we do recursive?
|
||||||
continue;
|
// Because when a thing creates a thing in the update,
|
||||||
|
// that new thing is not sent out (because of hierarchyChanged)
|
||||||
|
// before it is updated itself: it is immediatedly updated!
|
||||||
|
thing->Update(currentTimeMs, false);
|
||||||
|
|
||||||
|
if (!(this->isIsolated || this->networkId == 0)) {
|
||||||
if (thing->isTerminated) {
|
if (thing->isTerminated) {
|
||||||
DestroyMsg* destroyMsg = new DestroyMsg(this->networkId, thing);
|
DestroyMsg* destroyMsg = new DestroyMsg(this->networkId, thing);
|
||||||
this->Send(this->remoteSite, destroyMsg);
|
this->Send(this->remoteSite, destroyMsg);
|
||||||
delete destroyMsg;
|
delete destroyMsg;
|
||||||
this->Remove(thing);
|
|
||||||
} else {
|
} else {
|
||||||
// Send to remote site
|
// Send to remote site
|
||||||
PoseMsg* poseMsg = new PoseMsg(this->networkId, thing);
|
PoseMsg* poseMsg = new PoseMsg(this->networkId, thing);
|
||||||
@ -132,6 +141,9 @@ void ParticipantUDP::UpdateMyThings(unsigned long currentTimeMs = 0) {
|
|||||||
delete binaryMsg;
|
delete binaryMsg;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (thing->isTerminated)
|
||||||
|
this->Remove(thing);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ParticipantUDP::UpdateOtherThings(unsigned long currentTimeMs = 0) {
|
void ParticipantUDP::UpdateOtherThings(unsigned long currentTimeMs = 0) {
|
||||||
@ -139,7 +151,7 @@ void ParticipantUDP::UpdateOtherThings(unsigned long currentTimeMs = 0) {
|
|||||||
if (participant == nullptr || participant == this)
|
if (participant == nullptr || participant == this)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
participant->Update(currentTimeMs);
|
//participant->Update(currentTimeMs);
|
||||||
if (this->isIsolated)
|
if (this->isIsolated)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@ -363,13 +375,14 @@ void ParticipantUDP::Process(Participant* sender, SiteMsg* msg) {
|
|||||||
<< " -> " << (int)msg->networkId << "\n";
|
<< " -> " << (int)msg->networkId << "\n";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (this->networkId != msg->networkId)
|
if (this->networkId != msg->networkId) {
|
||||||
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)
|
for (Thing* thing : this->things)
|
||||||
this->SendThingInfo(sender, thing);
|
this->SendThingInfo(sender, thing);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void ParticipantUDP::Process(Participant* sender, InvestigateMsg* msg) {
|
void ParticipantUDP::Process(Participant* sender, InvestigateMsg* msg) {
|
||||||
#if defined(DEBUG)
|
#if defined(DEBUG)
|
||||||
|
26
Thing.cpp
26
Thing.cpp
@ -33,6 +33,7 @@ Thing::Thing(Participant* owner, int thingType, unsigned char thingId) {
|
|||||||
this->positionUpdated = true;
|
this->positionUpdated = true;
|
||||||
this->orientation = SwingTwist::identity;
|
this->orientation = SwingTwist::identity;
|
||||||
this->orientationUpdated = true;
|
this->orientationUpdated = true;
|
||||||
|
this->hierarchyChanged = true;
|
||||||
|
|
||||||
this->linearVelocity = Spherical::zero;
|
this->linearVelocity = Spherical::zero;
|
||||||
this->angularVelocity = Spherical::zero;
|
this->angularVelocity = Spherical::zero;
|
||||||
@ -45,29 +46,11 @@ Thing::Thing(Participant* owner, int thingType, unsigned char thingId) {
|
|||||||
|
|
||||||
Thing::Thing(Thing* parent, int thingType, unsigned char thingId)
|
Thing::Thing(Thing* parent, int thingType, unsigned char thingId)
|
||||||
: Thing(parent->owner, thingType, thingId) {
|
: Thing(parent->owner, thingType, thingId) {
|
||||||
this->parent = parent;
|
this->SetParent(parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Thing::Thing(Participant* owner,
|
|
||||||
// Type thingType,
|
|
||||||
// int thingId) {
|
|
||||||
// // no participant reference yet..
|
|
||||||
// this->owner = owner;
|
|
||||||
// this->networkId = networkId;
|
|
||||||
// this->id = thingId;
|
|
||||||
// this->type = (unsigned char)thingType;
|
|
||||||
|
|
||||||
// this->linearVelocity = Spherical::zero;
|
|
||||||
// this->angularVelocity = Spherical::zero;
|
|
||||||
// // std::cout << "Created thing " << (int)this->networkId << "/" <<
|
|
||||||
// // (int)this->id
|
|
||||||
// // << "\n";
|
|
||||||
// owner->Add(this, false);
|
|
||||||
// }
|
|
||||||
|
|
||||||
void Thing::Terminate() {
|
void Thing::Terminate() {
|
||||||
this->isTerminated = true;
|
this->isTerminated = true;
|
||||||
// Thing::Remove(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Thing* Thing::FindThing(const char* name) {
|
Thing* Thing::FindThing(const char* name) {
|
||||||
@ -94,6 +77,8 @@ void Thing::SetParent(Thing* parent) {
|
|||||||
this->parent = nullptr;
|
this->parent = nullptr;
|
||||||
} else
|
} else
|
||||||
parent->AddChild(this);
|
parent->AddChild(this);
|
||||||
|
std::cout << "setting parent for " << (int) this->id << std::endl;
|
||||||
|
this->hierarchyChanged = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Thing::SetParent(Thing* root, const char* name) {
|
void Thing::SetParent(Thing* root, const char* name) {
|
||||||
@ -199,6 +184,9 @@ void Thing::Update(unsigned long currentTimeMs, bool recursive) {
|
|||||||
// OnPoseChanged callback
|
// OnPoseChanged callback
|
||||||
this->positionUpdated = false;
|
this->positionUpdated = false;
|
||||||
this->orientationUpdated = false;
|
this->orientationUpdated = false;
|
||||||
|
// this->linearVelocityUpdated = false;
|
||||||
|
// this->angularVelocityUpdated = false;
|
||||||
|
this->hierarchyChanged = false;
|
||||||
|
|
||||||
if (recursive) {
|
if (recursive) {
|
||||||
for (unsigned char childIx = 0; childIx < this->childCount; childIx++) {
|
for (unsigned char childIx = 0; childIx < this->childCount; childIx++) {
|
||||||
|
5
Thing.h
5
Thing.h
@ -107,7 +107,10 @@ class Thing {
|
|||||||
/// @return The found thing of nullptr when nothing is found
|
/// @return The found thing of nullptr when nothing is found
|
||||||
Thing* GetChildByIndex(unsigned char ix);
|
Thing* GetChildByIndex(unsigned char ix);
|
||||||
|
|
||||||
protected:
|
/// @brief Indicator that the hierarchy of the thing has changed
|
||||||
|
bool hierarchyChanged = true;
|
||||||
|
|
||||||
|
private:
|
||||||
Thing* parent = nullptr;
|
Thing* parent = nullptr;
|
||||||
Thing** children = nullptr;
|
Thing** children = nullptr;
|
||||||
|
|
||||||
|
@ -42,8 +42,7 @@ void DifferentialDrive::SetWheelVelocity(float speedLeft, float speedRight) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void DifferentialDrive::Update(unsigned long currentMs, bool recursive) {
|
void DifferentialDrive::Update(unsigned long currentMs, bool recursive) {
|
||||||
if (this->linearVelocityUpdated == false)
|
if (this->linearVelocityUpdated) {
|
||||||
return;
|
|
||||||
// this assumes forward velocity only....
|
// this assumes forward velocity only....
|
||||||
float linearVelocity = this->GetLinearVelocity().distance;
|
float linearVelocity = this->GetLinearVelocity().distance;
|
||||||
|
|
||||||
@ -62,10 +61,8 @@ void DifferentialDrive::Update(unsigned long currentMs, bool recursive) {
|
|||||||
this->wheelRadius * Rad2Deg;
|
this->wheelRadius * Rad2Deg;
|
||||||
|
|
||||||
this->SetWheelVelocity(speedLeft, speedRight);
|
this->SetWheelVelocity(speedLeft, speedRight);
|
||||||
|
}
|
||||||
Thing::Update(currentMs, recursive);
|
Thing::Update(currentMs, recursive);
|
||||||
// std::cout << "lin. speed " << linearVelocity << " ang. speed " <<
|
|
||||||
// angularVelocity.distance << " left wheel "
|
|
||||||
// << speedLeft << " right wheel " << speedRight << "\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace RoboidControl
|
} // namespace RoboidControl
|
Loading…
x
Reference in New Issue
Block a user