Object creation/destruction works
This commit is contained in:
		
							parent
							
								
									a292179f87
								
							
						
					
					
						commit
						693c9b8b33
					
				| @ -123,9 +123,9 @@ DRV8833::DRV8833(Participant* participant, | |||||||
|     pinMode(pinStandby, OUTPUT); |     pinMode(pinStandby, OUTPUT); | ||||||
| 
 | 
 | ||||||
|   this->motorA = new DRV8833Motor(this, pinAIn1, pinAIn2, reverseA); |   this->motorA = new DRV8833Motor(this, pinAIn1, pinAIn2, reverseA); | ||||||
|   this->motorA->name = "Motor A"; |   this->motorA->SetName("Motor A"); | ||||||
|   this->motorB = new DRV8833Motor(this, pinBIn1, pinBIn2, reverseB); |   this->motorB = new DRV8833Motor(this, pinBIn1, pinBIn2, reverseB); | ||||||
|   this->motorB->name = "Motor B"; |   this->motorB->SetName("Motor B"); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| DRV8833::DRV8833(Thing* parent, | DRV8833::DRV8833(Thing* parent, | ||||||
|  | |||||||
| @ -7,15 +7,16 @@ namespace RoboidControl { | |||||||
| NameMsg::NameMsg(unsigned char networkId, Thing* thing) { | NameMsg::NameMsg(unsigned char networkId, Thing* thing) { | ||||||
|   this->networkId = networkId; |   this->networkId = networkId; | ||||||
|   this->thingId = thing->id; |   this->thingId = thing->id; | ||||||
|   if (thing->name == nullptr) |   const char* thingName = thing->GetName(); | ||||||
|  |   if (thingName == nullptr) | ||||||
|     this->nameLength = 0; |     this->nameLength = 0; | ||||||
|   else |   else | ||||||
|     this->nameLength = (unsigned char)strlen(thing->name); |     this->nameLength = (unsigned char)strlen(thingName); | ||||||
| 
 | 
 | ||||||
|   // the name string in the buffer is not \0 terminated!
 |   // the name string in the buffer is not \0 terminated!
 | ||||||
|   char* name = new char[this->nameLength + 1]; |   char* name = new char[this->nameLength + 1]; | ||||||
|   for (int i = 0; i < this->nameLength; i++) |   for (int i = 0; i < this->nameLength; i++) | ||||||
|     name[i] = thing->name[i]; |     name[i] = thingName[i]; | ||||||
|   name[this->nameLength] = '\0'; |   name[this->nameLength] = '\0'; | ||||||
|   this->name = name; |   this->name = name; | ||||||
| } | } | ||||||
|  | |||||||
| @ -151,22 +151,9 @@ void Participant::Remove(Thing* thing) { | |||||||
|   this->thingCount = lastThingIx; |   this->thingCount = lastThingIx; | ||||||
| #else | #else | ||||||
|   this->things.remove_if([thing](Thing* obj) { return obj == thing; }); |   this->things.remove_if([thing](Thing* obj) { return obj == thing; }); | ||||||
|   std::cout << "Removing [" << (int)thing->networkId << "/" << (int)thing->id |   // std::cout << "Removing [" << (int)thing->networkId << "/" << (int)thing->id
 | ||||||
|             << "] list size = " << this->things.size() << "\n"; |   //           << "] list size = " << this->things.size() << "\n";
 | ||||||
| #endif | #endif | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // void Participant::UpdateAll(unsigned long currentTimeMs) {
 |  | ||||||
| //   // Not very efficient, but it works for now.
 |  | ||||||
| 
 |  | ||||||
| //   for (Thing* thing : this->things) {
 |  | ||||||
| //     if (thing != nullptr && thing->GetParent() == nullptr) {  // update all
 |  | ||||||
| //     root things
 |  | ||||||
| //       // std::cout << " update " << (int)ix << " thingid " << (int)thing->id
 |  | ||||||
| //       //           << "\n";
 |  | ||||||
| //       thing->Update(currentTimeMs);
 |  | ||||||
| //     }
 |  | ||||||
| //   }
 |  | ||||||
| // }
 |  | ||||||
| 
 |  | ||||||
| }  // namespace RoboidControl
 | }  // namespace RoboidControl
 | ||||||
|  | |||||||
| @ -80,6 +80,8 @@ void ParticipantUDP::SetupUDP(int localPort, | |||||||
|   this->connected = true; |   this->connected = true; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | #pragma region Update | ||||||
|  | 
 | ||||||
| void ParticipantUDP::Update(unsigned long currentTimeMs) { | void ParticipantUDP::Update(unsigned long currentTimeMs) { | ||||||
|   if (currentTimeMs == 0) |   if (currentTimeMs == 0) | ||||||
|     currentTimeMs = Thing::GetTimeMs(); |     currentTimeMs = Thing::GetTimeMs(); | ||||||
| @ -112,12 +114,17 @@ void ParticipantUDP::UpdateMyThings(unsigned long currentTimeMs = 0) { | |||||||
|       continue; |       continue; | ||||||
| 
 | 
 | ||||||
|     if (thing->hierarchyChanged) { |     if (thing->hierarchyChanged) { | ||||||
|       std::cout << "thing hierarchy changed " << (int)thing->id << std::endl; |  | ||||||
|       if (!(this->isIsolated || this->networkId == 0)) { |       if (!(this->isIsolated || this->networkId == 0)) { | ||||||
|           ThingMsg* thingMsg = new ThingMsg(this->networkId, thing); |         ThingMsg* thingMsg = new ThingMsg(this->networkId, thing); | ||||||
|           this->Send(this->remoteSite, thingMsg); |         this->Send(this->remoteSite, thingMsg); | ||||||
|           delete thingMsg; |         delete thingMsg; | ||||||
|  |          | ||||||
|  |         if (thing->nameChanged) { | ||||||
|  |           NameMsg* nameMsg = new NameMsg(this->networkId, thing); | ||||||
|  |           this->Send(this->remoteSite, nameMsg); | ||||||
|  |           delete nameMsg; | ||||||
|         } |         } | ||||||
|  |       } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     // Why don't we do recursive?
 |     // Why don't we do recursive?
 | ||||||
| @ -127,12 +134,17 @@ void ParticipantUDP::UpdateMyThings(unsigned long currentTimeMs = 0) { | |||||||
|     thing->Update(currentTimeMs, false); |     thing->Update(currentTimeMs, false); | ||||||
| 
 | 
 | ||||||
|     if (!(this->isIsolated || this->networkId == 0)) { |     if (!(this->isIsolated || this->networkId == 0)) { | ||||||
|       if (thing->isTerminated) { |       if (thing->terminate) { | ||||||
|         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; | ||||||
|       } else { |       } else { | ||||||
|         //  Send to remote site
 |         //  Send to remote site
 | ||||||
|  |         if (thing->nameChanged) { | ||||||
|  |           NameMsg* nameMsg = new NameMsg(this->networkId, thing); | ||||||
|  |           this->Send(this->remoteSite, nameMsg); | ||||||
|  |           delete nameMsg; | ||||||
|  |         } | ||||||
|         PoseMsg* poseMsg = new PoseMsg(this->networkId, thing); |         PoseMsg* poseMsg = new PoseMsg(this->networkId, thing); | ||||||
|         this->Send(this->remoteSite, poseMsg); |         this->Send(this->remoteSite, poseMsg); | ||||||
|         delete poseMsg; |         delete poseMsg; | ||||||
| @ -141,7 +153,7 @@ void ParticipantUDP::UpdateMyThings(unsigned long currentTimeMs = 0) { | |||||||
|         delete binaryMsg; |         delete binaryMsg; | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|     if (thing->isTerminated) |     if (thing->terminate) | ||||||
|       this->Remove(thing); |       this->Remove(thing); | ||||||
|   } |   } | ||||||
| } | } | ||||||
| @ -151,7 +163,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; | ||||||
| 
 | 
 | ||||||
| @ -166,24 +178,8 @@ void ParticipantUDP::UpdateOtherThings(unsigned long currentTimeMs = 0) { | |||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void ParticipantUDP::ReceiveUDP() { | // Update
 | ||||||
| #if defined(_WIN32) || defined(_WIN64) | #pragma endregion | ||||||
|   Windows::ParticipantUDP* thisWindows = |  | ||||||
|       static_cast<Windows::ParticipantUDP*>(this); |  | ||||||
|   thisWindows->Receive(); |  | ||||||
| #elif defined(__unix__) || defined(__APPLE__) |  | ||||||
|   Posix::ParticipantUDP* thisPosix = static_cast<Posix::ParticipantUDP*>(this); |  | ||||||
|   thisPosix->Receive(); |  | ||||||
| #elif defined(ARDUINO) |  | ||||||
|   Arduino::ParticipantUDP* thisArduino = |  | ||||||
|       static_cast<Arduino::ParticipantUDP*>(this); |  | ||||||
|   thisArduino->Receive(); |  | ||||||
| #elif defined(IDF_VER) |  | ||||||
|   EspIdf::ParticipantUDP* thisEspIdf = |  | ||||||
|       static_cast<EspIdf::ParticipantUDP*>(this); |  | ||||||
|   thisEspIdf->Receive(); |  | ||||||
| #endif |  | ||||||
| } |  | ||||||
| 
 | 
 | ||||||
| #pragma region Send | #pragma region Send | ||||||
| 
 | 
 | ||||||
| @ -283,6 +279,25 @@ bool ParticipantUDP::Publish(IMessage* msg) { | |||||||
| 
 | 
 | ||||||
| #pragma region Receive | #pragma region Receive | ||||||
| 
 | 
 | ||||||
|  | void ParticipantUDP::ReceiveUDP() { | ||||||
|  | #if defined(_WIN32) || defined(_WIN64) | ||||||
|  |   Windows::ParticipantUDP* thisWindows = | ||||||
|  |       static_cast<Windows::ParticipantUDP*>(this); | ||||||
|  |   thisWindows->Receive(); | ||||||
|  | #elif defined(__unix__) || defined(__APPLE__) | ||||||
|  |   Posix::ParticipantUDP* thisPosix = static_cast<Posix::ParticipantUDP*>(this); | ||||||
|  |   thisPosix->Receive(); | ||||||
|  | #elif defined(ARDUINO) | ||||||
|  |   Arduino::ParticipantUDP* thisArduino = | ||||||
|  |       static_cast<Arduino::ParticipantUDP*>(this); | ||||||
|  |   thisArduino->Receive(); | ||||||
|  | #elif defined(IDF_VER) | ||||||
|  |   EspIdf::ParticipantUDP* thisEspIdf = | ||||||
|  |       static_cast<EspIdf::ParticipantUDP*>(this); | ||||||
|  |   thisEspIdf->Receive(); | ||||||
|  | #endif | ||||||
|  | } | ||||||
|  | 
 | ||||||
| void ParticipantUDP::ReceiveData(unsigned char packetSize, | void ParticipantUDP::ReceiveData(unsigned char packetSize, | ||||||
|                                  char* senderIpAddress, |                                  char* senderIpAddress, | ||||||
|                                  unsigned int senderPort) { |                                  unsigned int senderPort) { | ||||||
| @ -420,10 +435,10 @@ void ParticipantUDP::Process(Participant* sender, NameMsg* msg) { | |||||||
|             nameLength);  // Leave space for null terminator
 |             nameLength);  // Leave space for null terminator
 | ||||||
| #endif | #endif | ||||||
|     thingName[nameLength] = '\0'; |     thingName[nameLength] = '\0'; | ||||||
|     thing->name = thingName; |     thing->SetName(thingName); | ||||||
| 
 | 
 | ||||||
| #if !defined(NO_STD) | #if !defined(NO_STD) | ||||||
|     std::cout << thing->name; |     std::cout << thing->GetName(); | ||||||
| #endif | #endif | ||||||
|   } |   } | ||||||
| #if !defined(NO_STD) | #if !defined(NO_STD) | ||||||
|  | |||||||
| @ -9,6 +9,8 @@ | |||||||
| 
 | 
 | ||||||
| namespace RoboidControl { | namespace RoboidControl { | ||||||
| 
 | 
 | ||||||
|  | #pragma region Init | ||||||
|  | 
 | ||||||
| SiteServer::SiteServer(int port) { | SiteServer::SiteServer(int port) { | ||||||
|   this->name = "Site Server"; |   this->name = "Site Server"; | ||||||
|   this->publishInterval = 0; |   this->publishInterval = 0; | ||||||
| @ -19,6 +21,10 @@ SiteServer::SiteServer(int port) { | |||||||
|   SetupUDP(port, ipAddress, 0); |   SetupUDP(port, ipAddress, 0); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | #pragma endregion Init | ||||||
|  | 
 | ||||||
|  | #pragma region Update | ||||||
|  | 
 | ||||||
| void SiteServer::UpdateMyThings(unsigned long currentTimeMs) { | void SiteServer::UpdateMyThings(unsigned long currentTimeMs) { | ||||||
|   for (Thing* thing : this->things) { |   for (Thing* thing : this->things) { | ||||||
|     if (thing == nullptr) |     if (thing == nullptr) | ||||||
| @ -43,8 +49,12 @@ void SiteServer::UpdateMyThings(unsigned long currentTimeMs) { | |||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | #pragma endregion Update | ||||||
|  | 
 | ||||||
|  | #pragma region Receive | ||||||
|  | 
 | ||||||
| void SiteServer::Process(Participant* sender, ParticipantMsg* msg) { | void SiteServer::Process(Participant* sender, ParticipantMsg* msg) { | ||||||
|   if (msg->networkId == 0) { |   if (msg->networkId != sender->networkId) { | ||||||
|     // std::cout << this->name << " received New Client -> " <<
 |     // std::cout << this->name << " received New Client -> " <<
 | ||||||
|     // sender->ipAddress
 |     // sender->ipAddress
 | ||||||
|     //           << ":" << (int)sender->port << "\n";
 |     //           << ":" << (int)sender->port << "\n";
 | ||||||
| @ -59,21 +69,18 @@ void SiteServer::Process(Participant* sender, SiteMsg* msg) {} | |||||||
| void SiteServer::Process(Participant* sender, ThingMsg* msg) { | void SiteServer::Process(Participant* sender, ThingMsg* msg) { | ||||||
|   Thing* thing = sender->Get(msg->thingId); |   Thing* thing = sender->Get(msg->thingId); | ||||||
|   if (thing == nullptr) { |   if (thing == nullptr) { | ||||||
|     // #if defined(NO_STD)
 |  | ||||||
|     new Thing(sender, (Thing::Type)msg->thingType, msg->thingId); |     new Thing(sender, (Thing::Type)msg->thingType, msg->thingId); | ||||||
|     // #else
 | 
 | ||||||
|     //     auto thingMsgProcessor = thingMsgProcessors.find(msg->thingType);
 |     if (msg->parentId != 0) { | ||||||
|     //     //Thing* newThing;
 |       thing->SetParent(Get(msg->parentId)); | ||||||
|     //     if (thingMsgProcessor != thingMsgProcessors.end())  // found item
 |       if (thing->GetParent() != nullptr) | ||||||
|     //       //newThing =
 |         std::cout << "Could not find parent [" << (int)msg->networkId << "/" | ||||||
|     //           thingMsgProcessor->second(sender, msg->networkId,
 |                   << (int)msg->parentId << "]\n"; | ||||||
|     //           msg->thingId);
 |     } else | ||||||
|     //     else
 |       thing->SetParent(nullptr); | ||||||
|     //       //newThing =
 |  | ||||||
|     //         new Thing(sender, msg->networkId, msg->thingId,
 |  | ||||||
|     //                            (Thing::Type)msg->thingType);
 |  | ||||||
|     // #endif
 |  | ||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | #pragma endregion Receive | ||||||
|  | 
 | ||||||
| }  // namespace RoboidControl
 | }  // namespace RoboidControl
 | ||||||
|  | |||||||
							
								
								
									
										15
									
								
								Thing.cpp
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								Thing.cpp
									
									
									
									
									
								
							| @ -49,8 +49,13 @@ Thing::Thing(Thing* parent, int thingType, unsigned char thingId) | |||||||
|   this->SetParent(parent); |   this->SetParent(parent); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void Thing::Terminate() { | void Thing::SetName(const char* name) { | ||||||
|   this->isTerminated = true; |   this->name = name; | ||||||
|  |   this->nameChanged = true; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | const char* Thing::GetName() const { | ||||||
|  |   return this->name; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| Thing* Thing::FindThing(const char* name) { | Thing* Thing::FindThing(const char* name) { | ||||||
| @ -77,7 +82,6 @@ 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; |   this->hierarchyChanged = true; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -164,6 +168,8 @@ void Thing::SetModel(const char* url) { | |||||||
|   this->modelUrl = url; |   this->modelUrl = url; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | #pragma region Update | ||||||
|  | 
 | ||||||
| unsigned long Thing::GetTimeMs() { | unsigned long Thing::GetTimeMs() { | ||||||
| #if defined(ARDUINO) | #if defined(ARDUINO) | ||||||
|   return millis(); |   return millis(); | ||||||
| @ -187,6 +193,7 @@ void Thing::Update(unsigned long currentTimeMs, bool recursive) { | |||||||
|   // this->linearVelocityUpdated = false;
 |   // this->linearVelocityUpdated = false;
 | ||||||
|   // this->angularVelocityUpdated = false;
 |   // this->angularVelocityUpdated = false;
 | ||||||
|   this->hierarchyChanged = false; |   this->hierarchyChanged = false; | ||||||
|  |   this->nameChanged = false; | ||||||
| 
 | 
 | ||||||
|   if (recursive) { |   if (recursive) { | ||||||
|     for (unsigned char childIx = 0; childIx < this->childCount; childIx++) { |     for (unsigned char childIx = 0; childIx < this->childCount; childIx++) { | ||||||
| @ -202,6 +209,8 @@ void Thing::UpdateThings(unsigned long currentTimeMs) { | |||||||
|   IsolatedParticipant::Isolated()->Update(currentTimeMs); |   IsolatedParticipant::Isolated()->Update(currentTimeMs); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | #pragma endregion Update | ||||||
|  | 
 | ||||||
| int Thing::GenerateBinary(char* buffer, unsigned char* ix) { | int Thing::GenerateBinary(char* buffer, unsigned char* ix) { | ||||||
|   (void)buffer; |   (void)buffer; | ||||||
|   (void)ix; |   (void)ix; | ||||||
|  | |||||||
							
								
								
									
										9
									
								
								Thing.h
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								Thing.h
									
									
									
									
									
								
							| @ -114,9 +114,14 @@ class Thing { | |||||||
|   Thing* parent = nullptr; |   Thing* parent = nullptr; | ||||||
|   Thing** children = nullptr; |   Thing** children = nullptr; | ||||||
| 
 | 
 | ||||||
|  public: |  | ||||||
|   /// @brief The name of the thing
 |   /// @brief The name of the thing
 | ||||||
|   const char* name = nullptr; |   const char* name = nullptr; | ||||||
|  | 
 | ||||||
|  |  public: | ||||||
|  |   void SetName(const char* name); | ||||||
|  |   const char* GetName() const; | ||||||
|  |   bool nameChanged = false; | ||||||
|  | 
 | ||||||
|   /// @brief An URL pointing to the location where a model of the thing can be
 |   /// @brief An URL pointing to the location where a model of the thing can be
 | ||||||
|   /// found
 |   /// found
 | ||||||
|   const char* modelUrl = nullptr; |   const char* modelUrl = nullptr; | ||||||
| @ -177,7 +182,7 @@ class Thing { | |||||||
|  public: |  public: | ||||||
|   /// @brief Terminated things are no longer updated
 |   /// @brief Terminated things are no longer updated
 | ||||||
|   void Terminate(); |   void Terminate(); | ||||||
|   bool isTerminated = false; |   bool terminate = false; | ||||||
| 
 | 
 | ||||||
|   /// @brief Sets the location from where the 3D model of this Thing can be
 |   /// @brief Sets the location from where the 3D model of this Thing can be
 | ||||||
|   /// loaded from
 |   /// loaded from
 | ||||||
|  | |||||||
| @ -16,7 +16,7 @@ int TouchSensor::GenerateBinary(char* bytes, unsigned char* ix) { | |||||||
| 
 | 
 | ||||||
| void TouchSensor::ProcessBinary(char* bytes) { | void TouchSensor::ProcessBinary(char* bytes) { | ||||||
|   if (bytes[0] == 1) |   if (bytes[0] == 1) | ||||||
|     std::cout << this->name << " is Touching something!\n"; |     std::cout << this->GetName() << " is Touching something!\n"; | ||||||
|   this->touchedSomething |= (bytes[0] == 1); |   this->touchedSomething |= (bytes[0] == 1); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user