Small improvements

This commit is contained in:
Pascal Serrarens 2025-03-06 12:17:21 +01:00
parent a74671b8f4
commit a6a91798b2
5 changed files with 49 additions and 22 deletions

View File

@ -6,6 +6,8 @@
#include <thread>
using namespace RoboidControl;
using namespace std::this_thread;
using namespace std::chrono;
int main() {
// The robot's propulsion is a differential drive
@ -19,21 +21,19 @@ int main() {
while (true) {
// The left wheel turns forward when nothing is touched on the right side
// and turn backward when the roboid hits something on the right
float leftWheelSpeed = (touchRight->touchedSomething) ? -0.5f : 0.5f;
float leftWheelSpeed = (touchRight->touchedSomething) ? -600.0f : 600.0f;
// The right wheel does the same, but instead is controlled by
// touches on the left side
float rightWheelSpeed = (touchLeft->touchedSomething) ? -0.5f : 0.5f;
float rightWheelSpeed = (touchLeft->touchedSomething) ? -600.0f : 600.0f;
// When both sides are touching something, both wheels will turn backward
// and the roboid will move backwards
bb2b->SetWheelVelocity(leftWheelSpeed, rightWheelSpeed);
// Update the roboid state
Thing::UpdateThings(Thing::GetTimeMs());
// would like to do this:
// bb2b->Update();
bb2b->Update(true);
// and sleep for 100ms
std::this_thread::sleep_for(std::chrono::milliseconds(100));
sleep_for(milliseconds(100));
}
return 0;

View File

@ -175,20 +175,24 @@ unsigned long Thing::GetTimeMs() {
return static_cast<unsigned long>(ms.count());
}
void Thing::Update() {
void Thing::Update(bool recursive) {
#if defined(ARDUINO)
Update(millis());
#else
Update(GetTimeMs());
Update(GetTimeMs(), recursive);
#endif
}
void Thing::Update(unsigned long currentTimeMs) {
void Thing::Update(unsigned long currentTimeMs, bool recursive) {
(void)currentTimeMs;
// PoseMsg* poseMsg = new PoseMsg(this->networkId, this);
// participant->Send(remoteParticipant, poseMsg);
// delete poseMsg;
if (recursive) {
for (unsigned char childIx = 0; childIx < this->childCount; childIx++) {
Thing* child = this->children[childIx];
if (child == nullptr)
continue;
child->Update(currentTimeMs, recursive);
}
}
}
void Thing::UpdateThings(unsigned long currentTimeMs) {

View File

@ -178,12 +178,11 @@ class Thing {
static unsigned long GetTimeMs();
void Update();
void Update(bool recursive = false);
/// @brief Updates the state of the thing
/// @param currentTimeMs The current clock time in milliseconds
virtual void Update(
unsigned long currentTimeMs); // { (void)currentTimeMs; };
virtual void Update(unsigned long currentTimeMs, bool recursive = false);
static void UpdateThings(unsigned long currentTimeMs);

View File

@ -6,8 +6,8 @@ DifferentialDrive::DifferentialDrive() : Thing() {}
RoboidControl::DifferentialDrive::DifferentialDrive(Participant* participant)
: Thing(participant) {}
void DifferentialDrive::SetDimensions(float wheelDiameter,
float wheelSeparation) {
void DifferentialDrive::SetDriveDimensions(float wheelDiameter,
float wheelSeparation) {
this->wheelRadius =
wheelDiameter > 0 ? wheelDiameter / 2 : -wheelDiameter / 2;
this->wheelSeparation =
@ -40,7 +40,7 @@ void DifferentialDrive::SetWheelVelocity(float speedLeft, float speedRight) {
Spherical(speedRight, Direction::right));
}
void DifferentialDrive::Update(unsigned long currentMs) {
void DifferentialDrive::Update(unsigned long currentMs, bool recursive) {
if (this->linearVelocityUpdated == false)
return;
// this assumes forward velocity only....
@ -61,7 +61,7 @@ void DifferentialDrive::Update(unsigned long currentMs) {
this->wheelRadius * Rad2Deg;
this->SetWheelVelocity(speedLeft, speedRight);
Thing::Update(currentMs, recursive);
// std::cout << "lin. speed " << linearVelocity << " ang. speed " <<
// angularVelocity.distance << " left wheel "
// << speedLeft << " right wheel " << speedRight << "\n";

View File

@ -5,17 +5,39 @@
namespace RoboidControl {
/// @brief A thing which can move itself using a differential drive system
///
/// @sa @link https://en.wikipedia.org/wiki/Differential_wheeled_robot @endlink
class DifferentialDrive : public Thing {
public:
/// @brief Create a differential drive without networking support
DifferentialDrive();
/// @brief Create a differential drive with networking support
/// @param participant The local participant
DifferentialDrive(Participant* participant);
void SetDimensions(float wheelDiameter, float wheelSeparation);
/// @brief Configures the dimensions of the drive
/// @param wheelDiameter The diameter of the wheels in meters
/// @param wheelSeparation The distance between the wheels in meters
///
/// These values are used to compute the desired wheel speed from the set
/// linear and angular velocity.
/// @sa SetLinearVelocity SetAngularVelocity
void SetDriveDimensions(float wheelDiameter, float wheelSeparation);
/// @brief Congures the motors for the wheels
/// @param leftWheel The motor for the left wheel
/// @param rightWheel The motor for the right wheel
void SetMotors(Thing* leftWheel, Thing* rightWheel);
/// @brief Directly specify the speeds of the motors
/// @param speedLeft The speed of the left wheel in degrees per second.
/// Positive moves the robot in the forward direction.
/// @param speedRight The speed of the right wheel in degrees per second.
/// Positive moves the robot in the forward direction.
void SetWheelVelocity(float speedLeft, float speedRight);
virtual void Update(unsigned long currentMs) override;
/// @copydoc RoboidControl::Thing::Update(unsigned long)
virtual void Update(unsigned long currentMs, bool recursive = true) override;
protected:
/// @brief The radius of a wheel in meters
@ -26,7 +48,9 @@ class DifferentialDrive : public Thing {
/// @brief Convert revolutions per second to meters per second
float rpsToMs = 1.0f;
/// @brief The left wheel
Thing* leftWheel = nullptr;
/// @brief The right wheel
Thing* rightWheel = nullptr;
};