Velocity refactoring

This commit is contained in:
Pascal Serrarens 2024-11-07 13:59:24 +01:00
parent 3726070f02
commit 610121f944
9 changed files with 82 additions and 64 deletions

View File

@ -73,7 +73,7 @@ void DifferentialDrive::SetVelocity(Polar velocity) {
SetTwistSpeed(velocity.distance, velocity.angle.InDegrees()); SetTwistSpeed(velocity.distance, velocity.angle.InDegrees());
} }
Polar DifferentialDrive::GetVelocity() { Spherical16 DifferentialDrive::GetVelocity() {
Motor *leftMotor = motors[0]; Motor *leftMotor = motors[0];
Motor *rightMotor = motors[1]; Motor *rightMotor = motors[1];
float leftSpeed = leftMotor->GetActualSpeed(); // in revolutions per second float leftSpeed = leftMotor->GetActualSpeed(); // in revolutions per second
@ -85,8 +85,9 @@ Polar DifferentialDrive::GetVelocity() {
float direction = speed >= 0 ? 0.0F : 180.0F; float direction = speed >= 0 ? 0.0F : 180.0F;
float magnitude = fabsf(speed); float magnitude = fabsf(speed);
Polar velocity = Polar( Spherical16 velocity =
magnitude, Angle::Degrees(direction)); // Polar(direction, magnitude); Spherical16(magnitude, Angle16::Degrees(direction),
Angle16::Degrees(0)); // Polar(direction, magnitude);
return velocity; return velocity;
} }

View File

@ -68,7 +68,7 @@ public:
/// information /// information
/// @remark This will be more expanded/detailed in a future version of Roboid /// @remark This will be more expanded/detailed in a future version of Roboid
/// Control /// Control
virtual Polar GetVelocity() override; virtual Spherical16 GetVelocity() override;
/// @brief Calculate the angular velocity of the roboid based on the wheel /// @brief Calculate the angular velocity of the roboid based on the wheel
/// velocities /// velocities
/// @return The angular speed of the roboid in local space /// @return The angular speed of the roboid in local space

View File

@ -1,6 +1,6 @@
#include "NetworkSync.h" #include "NetworkSync.h"
#define RC_DEBUG 1 // #define RC_DEBUG 1
#ifdef RC_DEBUG #ifdef RC_DEBUG
#include <Arduino.h> #include <Arduino.h>
@ -39,6 +39,7 @@ void NetworkSync::ReceiveNetworkId() {
#ifdef RC_DEBUG #ifdef RC_DEBUG
SERIALPORT.printf("Received network Id %d\n", this->networkId); SERIALPORT.printf("Received network Id %d\n", this->networkId);
#endif #endif
SendThing(roboid);
SendName(roboid); SendName(roboid);
SendModel(roboid); SendModel(roboid);
for (unsigned char childIx = 0; childIx < roboid->childCount; childIx++) { for (unsigned char childIx = 0; childIx < roboid->childCount; childIx++) {
@ -64,6 +65,21 @@ void NetworkSync::PublishState(Roboid *roboid) {
PublishPerception(roboid); PublishPerception(roboid);
} }
void NetworkSync::SendThing(Thing *thing) {
if (thing == nullptr)
return;
unsigned char ix = 0;
buffer[ix++] = CreateMsg;
buffer[ix++] = thing->id;
buffer[ix++] = thing->type;
SendBuffer(ix);
#ifdef RC_DEBUG
printf("Sent Thing [%d/%d] %d\n", networkId, buffer[1], buffer[2]);
#endif
}
void NetworkSync::NewObject(InterestingThing *thing) { void NetworkSync::NewObject(InterestingThing *thing) {
if (thing == nullptr || thing->networkId != 0x00) if (thing == nullptr || thing->networkId != 0x00)
return; return;
@ -314,11 +330,9 @@ void NetworkSync::PublishTrackedObject(Roboid *roboid,
} }
void NetworkSync::SendPoseMsg(Buffer sendBuffer, Roboid *roboid) { void NetworkSync::SendPoseMsg(Buffer sendBuffer, Roboid *roboid) {
// SERIALPORT.printf("old send pose?\n"); Spherical16 velocity = roboid->propulsion->GetVelocity();
Polar velocity = roboid->propulsion->GetVelocity(); Spherical16 worldVelocity = roboid->orientation * velocity;
Vector2 worldVelocity2 = Vector3 worldVelocity3 = worldVelocity.ToVector3();
Vector2::Rotate(Vector2::forward * velocity.distance, velocity.angle);
Vector3 worldVelocity3 = Vector3(worldVelocity2.x, 0, worldVelocity2.y);
float angularVelocity = roboid->propulsion->GetAngularVelocity(); float angularVelocity = roboid->propulsion->GetAngularVelocity();
Vector3 worldAngularVelocity = Vector3(0, angularVelocity, 0); Vector3 worldAngularVelocity = Vector3(0, angularVelocity, 0);

View File

@ -10,22 +10,23 @@ namespace RoboidControl {
/// @brief Interface for synchronizaing state between clients across a network /// @brief Interface for synchronizaing state between clients across a network
class NetworkSync { class NetworkSync {
public: public:
NetworkSync() {}; NetworkSync() {};
NetworkSync(Roboid* roboid); NetworkSync(Roboid *roboid);
unsigned char networkId = 0; unsigned char networkId = 0;
/// @brief Retreive and send the roboid state /// @brief Retreive and send the roboid state
/// @param roboid The roboid for which the state is updated /// @param roboid The roboid for which the state is updated
virtual void NetworkUpdate(Roboid* roboid) = 0; virtual void NetworkUpdate(Roboid *roboid) = 0;
/// @brief Inform that the given object is no longer being tracked /// @brief Inform that the given object is no longer being tracked
/// @param obj /// @param obj
virtual void SendDestroyThing(InterestingThing* obj); virtual void SendDestroyThing(InterestingThing *obj);
virtual void NewObject(InterestingThing* obj); virtual void NewObject(InterestingThing *obj);
void SendName(Roboid* roboid); void SendThing(Thing *thing);
virtual void SendModel(Roboid* obj); void SendName(Roboid *roboid);
void SendModel(Thing* thing); virtual void SendModel(Roboid *obj);
void SendModel(Thing *thing);
/// @brief The id of a Pose message /// @brief The id of a Pose message
static const unsigned char PoseMsg = 0x10; static const unsigned char PoseMsg = 0x10;
@ -57,71 +58,65 @@ class NetworkSync {
static const unsigned char DeviceMsg = 0xA0; static const unsigned char DeviceMsg = 0xA0;
static const unsigned char NetworkIdMsg = 0xA1; static const unsigned char NetworkIdMsg = 0xA1;
typedef void (*Buffer)(UInt8* buffer, UInt16 bufferSize); typedef void (*Buffer)(UInt8 *buffer, UInt16 bufferSize);
void ReceiveMessage(Roboid* roboid, unsigned char bytecount); void ReceiveMessage(Roboid *roboid, unsigned char bytecount);
void ReceiveNetworkId(); void ReceiveNetworkId();
void PublishState(Roboid* roboid); void PublishState(Roboid *roboid);
void SendInvestigate(InterestingThing* thing); void SendInvestigate(InterestingThing *thing);
void SendPoseMsg(Buffer sendBuffer, Roboid* roboid); void SendPoseMsg(Buffer sendBuffer, Roboid *roboid);
// void SendDestroyThing(Buffer sendBuffer, InterestingThing* obj); // void SendDestroyThing(Buffer sendBuffer, InterestingThing* obj);
// void PublishNewObject(); // void PublishNewObject();
void PublishRelativeThing(Thing* thing, bool recurse = false); void PublishRelativeThing(Thing *thing, bool recurse = false);
void PublishPerception(Roboid* roboid); void PublishPerception(Roboid *roboid);
void PublishTrackedObjects(Roboid* roboid, InterestingThing** objects); void PublishTrackedObjects(Roboid *roboid, InterestingThing **objects);
virtual void SendPosition(Spherical16 worldPosition) {}; virtual void SendPosition(Spherical16 worldPosition) {};
virtual void SendPose(Spherical16 worldPosition, virtual void SendPose(Spherical16 worldPosition,
SwingTwist16 worldOrientation) {}; SwingTwist16 worldOrientation) {};
// void SendPose(Roboid* roboid, bool recurse = true); // void SendPose(Roboid* roboid, bool recurse = true);
void SendPose(Thing* thing, bool recurse = true); void SendPose(Thing *thing, bool recurse = true);
virtual void SendText(const char* s); virtual void SendText(const char *s);
void SendInt(const int x); void SendInt(const int x);
protected: protected:
Roboid* roboid; Roboid *roboid;
NetworkPerception* networkPerception; NetworkPerception *networkPerception;
void PublishState(Sensor* sensor); void PublishState(Sensor *sensor);
void PublishTrackedObject(Roboid* roboid, InterestingThing* object); void PublishTrackedObject(Roboid *roboid, InterestingThing *object);
// void PublishRelativeObject(Buffer sendBuffer, // void PublishRelativeObject(Buffer sendBuffer,
// UInt8 parentId, // UInt8 parentId,
// InterestingThing* object); // InterestingThing* object);
void SendSingle100(unsigned char* data, unsigned int startIndex, float value); void SendSingle100(unsigned char *data, unsigned int startIndex, float value);
void SendFloat16(unsigned char* data, unsigned char* startIndex, float value); void SendFloat16(unsigned char *data, unsigned char *startIndex, float value);
void SendInt32(unsigned char* data, unsigned int startIndex, Int32 value); void SendInt32(unsigned char *data, unsigned int startIndex, Int32 value);
void SendAngle8(unsigned char* data, void SendAngle8(unsigned char *data, unsigned int startIndex,
unsigned int startIndex,
const float value); const float value);
// void SendAngle16(unsigned char *data, unsigned int startIndex, // void SendAngle16(unsigned char *data, unsigned int startIndex,
// const float value); // const float value);
// void SendAngle32(unsigned char *data, unsigned int startIndex, // void SendAngle32(unsigned char *data, unsigned int startIndex,
// const float value); // const float value);
void SendVector3(unsigned char* data, void SendVector3(unsigned char *data, unsigned char *startIndex,
unsigned char* startIndex,
const Vector3 v); const Vector3 v);
void SendQuaternion(unsigned char* data, void SendQuaternion(unsigned char *data, const int startIndex,
const int startIndex,
const Quaternion q); const Quaternion q);
void SendPolar(unsigned char* data, unsigned char* startIndex, Polar p); void SendPolar(unsigned char *data, unsigned char *startIndex, Polar p);
void SendSpherical16(unsigned char* data, void SendSpherical16(unsigned char *data, unsigned char *startIndex,
unsigned char* startIndex,
Spherical16 s); Spherical16 s);
void SendSwingTwist(unsigned char* data, void SendSwingTwist(unsigned char *data, unsigned char *startIndex,
unsigned char* startIndex,
const SwingTwist16 r); const SwingTwist16 r);
void SendQuat32(unsigned char* data, void SendQuat32(unsigned char *data, unsigned char *startIndex,
unsigned char* startIndex,
const Quaternion q); const Quaternion q);
unsigned char buffer[256]; unsigned char buffer[256];
@ -131,5 +126,5 @@ class NetworkSync {
void PublishDevice(); void PublishDevice();
}; };
} // namespace RoboidControl } // namespace RoboidControl
} // namespace Passer } // namespace Passer

View File

@ -35,8 +35,10 @@ void Propulsion::SetTwistSpeed(Vector2 linear, float yaw) {}
void Propulsion::SetTwistSpeed(Vector3 linear, float yaw, float pitch, void Propulsion::SetTwistSpeed(Vector3 linear, float yaw, float pitch,
float roll) {} float roll) {}
void Propulsion::SetVelocity(Polar velocity) {} void Propulsion::SetVelocity(Spherical16 velocity) {
this->linearVelocity = velocity;
}
Polar Propulsion::GetVelocity() { return Polar::zero; } Spherical16 Propulsion::GetVelocity() { return this->linearVelocity; }
float Propulsion::GetAngularVelocity() { return 0; } float Propulsion::GetAngularVelocity() { return 0; }

View File

@ -59,12 +59,12 @@ public:
virtual void SetTwistSpeed(Vector3 linear, float yaw = 0.0F, virtual void SetTwistSpeed(Vector3 linear, float yaw = 0.0F,
float pitch = 0.0F, float roll = 0.0F); float pitch = 0.0F, float roll = 0.0F);
virtual void SetVelocity(Polar velocity); virtual void SetVelocity(Spherical16 velocity);
/// @brief Retrieve the current velocity of the roboid /// @brief Retrieve the current velocity of the roboid
/// @return The velocity in polar coordinates /// @return The velocity in polar coordinates
/// The actual units of the velocity depend on the implementation /// The actual units of the velocity depend on the implementation
virtual Polar GetVelocity(); virtual Spherical16 GetVelocity();
/// @brief Retrieve the current angular velocity of the roboid /// @brief Retrieve the current angular velocity of the roboid
/// @return The angular velocity /// @return The angular velocity
/// The actual unit of the angular velocity depend on the implementation /// The actual unit of the angular velocity depend on the implementation
@ -79,6 +79,8 @@ protected:
/// @brief The Placement of the motors used for Propulsion /// @brief The Placement of the motors used for Propulsion
// Placement *placement = nullptr; // Placement *placement = nullptr;
Motor **motors = nullptr; Motor **motors = nullptr;
Spherical16 linearVelocity = Spherical16::zero;
}; };
} // namespace RoboidControl } // namespace RoboidControl

View File

@ -18,11 +18,10 @@ Roboid::Roboid() : Thing(0) {
this->type = RoboidType; this->type = RoboidType;
this->perception = new Perception(); this->perception = new Perception();
this->perception->roboid = this; this->perception->roboid = this;
this->propulsion = nullptr; this->propulsion = new Propulsion();
this->networkSync = nullptr; this->networkSync = nullptr;
this->position = Spherical16::zero;
this->roboidPosition = Spherical16::zero; this->orientation = SwingTwist16::identity;
this->roboidOrientation = SwingTwist16::identity;
} }
Roboid::Roboid(Propulsion *propulsion) : Roboid() { Roboid::Roboid(Propulsion *propulsion) : Roboid() {
@ -43,10 +42,11 @@ void Roboid::Update(unsigned long currentTimeMs) {
float deltaTime = (float)(currentTimeMs - lastUpdateTimeMs) / 1000; float deltaTime = (float)(currentTimeMs - lastUpdateTimeMs) / 1000;
// Conversion from old units // Conversion from old units
Polar polarVelocity = this->propulsion->GetVelocity(); // Polar polarVelocity = this->propulsion->GetVelocity();
this->linearVelocity = Spherical16( // this->linearVelocity = Spherical16(
polarVelocity.distance, // polarVelocity.distance,
Angle16::Degrees(polarVelocity.angle.InDegrees()), Angle16()); // Angle16::Degrees(polarVelocity.angle.InDegrees()), Angle16());
this->linearVelocity = this->propulsion->GetVelocity();
float oldAngular = this->propulsion->GetAngularVelocity(); float oldAngular = this->propulsion->GetAngularVelocity();
this->angularVelocity = this->angularVelocity =
Spherical16(oldAngular, Angle16(), Angle16::Degrees(90)); Spherical16(oldAngular, Angle16(), Angle16::Degrees(90));
@ -54,7 +54,8 @@ void Roboid::Update(unsigned long currentTimeMs) {
// SetPosition(this->position + this->orientation * Spherical16::forward * // SetPosition(this->position + this->orientation * Spherical16::forward *
// this->linearVelocity.distance * // this->linearVelocity.distance *
// deltaTime); // deltaTime);
// this->roboidPosition = this->position; // assuming the roboid is the root // this->roboidPosition = this->position; // assuming the roboid is the
// root
this->position += this->orientation * Spherical16::forward * this->position += this->orientation * Spherical16::forward *
this->linearVelocity.distance * deltaTime; this->linearVelocity.distance * deltaTime;

View File

@ -21,6 +21,7 @@ const unsigned int Thing::ControlledMotorType =
const unsigned int Thing::UncontrolledMotorType = const unsigned int Thing::UncontrolledMotorType =
MotorType | (unsigned int)Type::UncontrolledMotor; MotorType | (unsigned int)Type::UncontrolledMotor;
const unsigned int Thing::ServoType = (unsigned int)Type::Servo; const unsigned int Thing::ServoType = (unsigned int)Type::Servo;
const unsigned int Thing::HumanoidType = (unsigned int)Type::Humanoid;
bool Thing::IsMotor() { return (type & Thing::MotorType) != 0; } bool Thing::IsMotor() { return (type & Thing::MotorType) != 0; }

View File

@ -32,6 +32,7 @@ public:
static const unsigned int UncontrolledMotorType; static const unsigned int UncontrolledMotorType;
/// @brief The type of an object received from the network /// @brief The type of an object received from the network
static const unsigned int ServoType; static const unsigned int ServoType;
static const unsigned int HumanoidType;
/// @brief Check if the Thing is a Motor /// @brief Check if the Thing is a Motor
/// @return True when the Thing is a Motor and False otherwise /// @return True when the Thing is a Motor and False otherwise
@ -118,6 +119,7 @@ protected:
UncontrolledMotor, UncontrolledMotor,
Servo, Servo,
// Other // Other
Humanoid,
ExternalSensor, ExternalSensor,
}; };