Small improvements
This commit is contained in:
parent
a74671b8f4
commit
a6a91798b2
@ -6,6 +6,8 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
using namespace RoboidControl;
|
using namespace RoboidControl;
|
||||||
|
using namespace std::this_thread;
|
||||||
|
using namespace std::chrono;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// The robot's propulsion is a differential drive
|
// The robot's propulsion is a differential drive
|
||||||
@ -19,21 +21,19 @@ int main() {
|
|||||||
while (true) {
|
while (true) {
|
||||||
// The left wheel turns forward when nothing is touched on the right side
|
// The left wheel turns forward when nothing is touched on the right side
|
||||||
// and turn backward when the roboid hits something on the right
|
// 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
|
// The right wheel does the same, but instead is controlled by
|
||||||
// touches on the left side
|
// 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
|
// When both sides are touching something, both wheels will turn backward
|
||||||
// and the roboid will move backwards
|
// and the roboid will move backwards
|
||||||
bb2b->SetWheelVelocity(leftWheelSpeed, rightWheelSpeed);
|
bb2b->SetWheelVelocity(leftWheelSpeed, rightWheelSpeed);
|
||||||
|
|
||||||
// Update the roboid state
|
// Update the roboid state
|
||||||
Thing::UpdateThings(Thing::GetTimeMs());
|
bb2b->Update(true);
|
||||||
// would like to do this:
|
|
||||||
// bb2b->Update();
|
|
||||||
|
|
||||||
// and sleep for 100ms
|
// and sleep for 100ms
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
sleep_for(milliseconds(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
18
Thing.cpp
18
Thing.cpp
@ -175,20 +175,24 @@ unsigned long Thing::GetTimeMs() {
|
|||||||
return static_cast<unsigned long>(ms.count());
|
return static_cast<unsigned long>(ms.count());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Thing::Update() {
|
void Thing::Update(bool recursive) {
|
||||||
#if defined(ARDUINO)
|
#if defined(ARDUINO)
|
||||||
Update(millis());
|
Update(millis());
|
||||||
#else
|
#else
|
||||||
Update(GetTimeMs());
|
Update(GetTimeMs(), recursive);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void Thing::Update(unsigned long currentTimeMs) {
|
void Thing::Update(unsigned long currentTimeMs, bool recursive) {
|
||||||
(void)currentTimeMs;
|
(void)currentTimeMs;
|
||||||
|
if (recursive) {
|
||||||
// PoseMsg* poseMsg = new PoseMsg(this->networkId, this);
|
for (unsigned char childIx = 0; childIx < this->childCount; childIx++) {
|
||||||
// participant->Send(remoteParticipant, poseMsg);
|
Thing* child = this->children[childIx];
|
||||||
// delete poseMsg;
|
if (child == nullptr)
|
||||||
|
continue;
|
||||||
|
child->Update(currentTimeMs, recursive);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Thing::UpdateThings(unsigned long currentTimeMs) {
|
void Thing::UpdateThings(unsigned long currentTimeMs) {
|
||||||
|
5
Thing.h
5
Thing.h
@ -178,12 +178,11 @@ class Thing {
|
|||||||
|
|
||||||
static unsigned long GetTimeMs();
|
static unsigned long GetTimeMs();
|
||||||
|
|
||||||
void Update();
|
void Update(bool recursive = false);
|
||||||
|
|
||||||
/// @brief Updates the state of the thing
|
/// @brief Updates the state of the thing
|
||||||
/// @param currentTimeMs The current clock time in milliseconds
|
/// @param currentTimeMs The current clock time in milliseconds
|
||||||
virtual void Update(
|
virtual void Update(unsigned long currentTimeMs, bool recursive = false);
|
||||||
unsigned long currentTimeMs); // { (void)currentTimeMs; };
|
|
||||||
|
|
||||||
static void UpdateThings(unsigned long currentTimeMs);
|
static void UpdateThings(unsigned long currentTimeMs);
|
||||||
|
|
||||||
|
@ -6,8 +6,8 @@ DifferentialDrive::DifferentialDrive() : Thing() {}
|
|||||||
RoboidControl::DifferentialDrive::DifferentialDrive(Participant* participant)
|
RoboidControl::DifferentialDrive::DifferentialDrive(Participant* participant)
|
||||||
: Thing(participant) {}
|
: Thing(participant) {}
|
||||||
|
|
||||||
void DifferentialDrive::SetDimensions(float wheelDiameter,
|
void DifferentialDrive::SetDriveDimensions(float wheelDiameter,
|
||||||
float wheelSeparation) {
|
float wheelSeparation) {
|
||||||
this->wheelRadius =
|
this->wheelRadius =
|
||||||
wheelDiameter > 0 ? wheelDiameter / 2 : -wheelDiameter / 2;
|
wheelDiameter > 0 ? wheelDiameter / 2 : -wheelDiameter / 2;
|
||||||
this->wheelSeparation =
|
this->wheelSeparation =
|
||||||
@ -40,7 +40,7 @@ void DifferentialDrive::SetWheelVelocity(float speedLeft, float speedRight) {
|
|||||||
Spherical(speedRight, Direction::right));
|
Spherical(speedRight, Direction::right));
|
||||||
}
|
}
|
||||||
|
|
||||||
void DifferentialDrive::Update(unsigned long currentMs) {
|
void DifferentialDrive::Update(unsigned long currentMs, bool recursive) {
|
||||||
if (this->linearVelocityUpdated == false)
|
if (this->linearVelocityUpdated == false)
|
||||||
return;
|
return;
|
||||||
// this assumes forward velocity only....
|
// this assumes forward velocity only....
|
||||||
@ -61,7 +61,7 @@ void DifferentialDrive::Update(unsigned long currentMs) {
|
|||||||
this->wheelRadius * Rad2Deg;
|
this->wheelRadius * Rad2Deg;
|
||||||
|
|
||||||
this->SetWheelVelocity(speedLeft, speedRight);
|
this->SetWheelVelocity(speedLeft, speedRight);
|
||||||
|
Thing::Update(currentMs, recursive);
|
||||||
// std::cout << "lin. speed " << linearVelocity << " ang. speed " <<
|
// std::cout << "lin. speed " << linearVelocity << " ang. speed " <<
|
||||||
// angularVelocity.distance << " left wheel "
|
// angularVelocity.distance << " left wheel "
|
||||||
// << speedLeft << " right wheel " << speedRight << "\n";
|
// << speedLeft << " right wheel " << speedRight << "\n";
|
||||||
|
@ -5,17 +5,39 @@
|
|||||||
namespace RoboidControl {
|
namespace RoboidControl {
|
||||||
|
|
||||||
/// @brief A thing which can move itself using a differential drive system
|
/// @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 {
|
class DifferentialDrive : public Thing {
|
||||||
public:
|
public:
|
||||||
|
/// @brief Create a differential drive without networking support
|
||||||
DifferentialDrive();
|
DifferentialDrive();
|
||||||
|
/// @brief Create a differential drive with networking support
|
||||||
|
/// @param participant The local participant
|
||||||
DifferentialDrive(Participant* 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);
|
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);
|
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:
|
protected:
|
||||||
/// @brief The radius of a wheel in meters
|
/// @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
|
/// @brief Convert revolutions per second to meters per second
|
||||||
float rpsToMs = 1.0f;
|
float rpsToMs = 1.0f;
|
||||||
|
|
||||||
|
/// @brief The left wheel
|
||||||
Thing* leftWheel = nullptr;
|
Thing* leftWheel = nullptr;
|
||||||
|
/// @brief The right wheel
|
||||||
Thing* rightWheel = nullptr;
|
Thing* rightWheel = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user