Cleaned up BB2B example
This commit is contained in:
parent
c7e8b0e7a7
commit
a74671b8f4
@ -1,38 +1,40 @@
|
|||||||
#include <iostream>
|
|
||||||
|
|
||||||
#include "Thing.h"
|
#include "Thing.h"
|
||||||
#include "Things/DifferentialDrive.h"
|
#include "Things/DifferentialDrive.h"
|
||||||
#include "Things/TouchSensor.h"
|
#include "Things/TouchSensor.h"
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
using namespace RoboidControl;
|
using namespace RoboidControl;
|
||||||
|
|
||||||
void CollisionSteering();
|
|
||||||
|
|
||||||
DifferentialDrive* bb2b = nullptr;
|
|
||||||
TouchSensor* touchLeft = nullptr;
|
|
||||||
TouchSensor* touchRight = nullptr;
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
bb2b = new DifferentialDrive();
|
// The robot's propulsion is a differential drive
|
||||||
|
DifferentialDrive* bb2b = new DifferentialDrive();
|
||||||
touchLeft = new TouchSensor(bb2b);
|
// Is has a touch sensor at the front left of the roboid
|
||||||
touchRight = new TouchSensor(bb2b);
|
TouchSensor* touchLeft = new TouchSensor(bb2b);
|
||||||
|
// and other one on the right
|
||||||
|
TouchSensor* touchRight = new TouchSensor(bb2b);
|
||||||
|
|
||||||
|
// Do forever:
|
||||||
while (true) {
|
while (true) {
|
||||||
CollisionSteering();
|
// 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;
|
||||||
|
// The right wheel does the same, but instead is controlled by
|
||||||
|
// touches on the left side
|
||||||
|
float rightWheelSpeed = (touchLeft->touchedSomething) ? -0.5f : 0.5f;
|
||||||
|
// 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();
|
||||||
|
|
||||||
|
// and sleep for 100ms
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
|
|
||||||
void CollisionSteering() {
|
|
||||||
if (touchLeft->touchedSomething)
|
|
||||||
bb2b->SetRightWheelVelocity(-0.5f);
|
|
||||||
else
|
|
||||||
bb2b->SetRightWheelVelocity(0.5f);
|
|
||||||
|
|
||||||
if (touchRight->touchedSomething)
|
|
||||||
bb2b->SetLeftWheelVelocity(-0.5f);
|
|
||||||
else
|
|
||||||
bb2b->SetLeftWheelVelocity(0.5f);
|
|
||||||
}
|
}
|
@ -4,10 +4,7 @@ namespace RoboidControl {
|
|||||||
DifferentialDrive::DifferentialDrive() : Thing() {}
|
DifferentialDrive::DifferentialDrive() : Thing() {}
|
||||||
|
|
||||||
RoboidControl::DifferentialDrive::DifferentialDrive(Participant* participant)
|
RoboidControl::DifferentialDrive::DifferentialDrive(Participant* participant)
|
||||||
: Thing(participant) {
|
: Thing(participant) {}
|
||||||
// this->leftWheel = new Thing(participant);
|
|
||||||
// this->rightWheel = new Thing(participant);
|
|
||||||
}
|
|
||||||
|
|
||||||
void DifferentialDrive::SetDimensions(float wheelDiameter,
|
void DifferentialDrive::SetDimensions(float wheelDiameter,
|
||||||
float wheelSeparation) {
|
float wheelSeparation) {
|
||||||
@ -35,11 +32,17 @@ void DifferentialDrive::SetMotors(Thing* leftWheel, Thing* rightWheel) {
|
|||||||
this->rightWheel->SetPosition(Spherical(distance, Direction::right));
|
this->rightWheel->SetPosition(Spherical(distance, Direction::right));
|
||||||
}
|
}
|
||||||
|
|
||||||
void DifferentialDrive::SetLeftWheelVelocity(float angularSpeed) {}
|
void DifferentialDrive::SetWheelVelocity(float speedLeft, float speedRight) {
|
||||||
|
if (this->leftWheel != nullptr)
|
||||||
void DifferentialDrive::SetRightWheelVelocity(float angularSpeed) {}
|
this->leftWheel->SetAngularVelocity(Spherical(speedLeft, Direction::left));
|
||||||
|
if (this->rightWheel != nullptr)
|
||||||
|
this->rightWheel->SetAngularVelocity(
|
||||||
|
Spherical(speedRight, Direction::right));
|
||||||
|
}
|
||||||
|
|
||||||
void DifferentialDrive::Update(unsigned long currentMs) {
|
void DifferentialDrive::Update(unsigned long currentMs) {
|
||||||
|
if (this->linearVelocityUpdated == false)
|
||||||
|
return;
|
||||||
// this assumes forward velocity only....
|
// this assumes forward velocity only....
|
||||||
float linearVelocity = this->GetLinearVelocity().distance;
|
float linearVelocity = this->GetLinearVelocity().distance;
|
||||||
|
|
||||||
@ -53,15 +56,11 @@ void DifferentialDrive::Update(unsigned long currentMs) {
|
|||||||
float speedLeft =
|
float speedLeft =
|
||||||
(linearVelocity + angularSpeed * this->wheelSeparation / 2) /
|
(linearVelocity + angularSpeed * this->wheelSeparation / 2) /
|
||||||
this->wheelRadius * Rad2Deg;
|
this->wheelRadius * Rad2Deg;
|
||||||
if (this->leftWheel != nullptr)
|
|
||||||
this->leftWheel->SetAngularVelocity(Spherical(speedLeft, Direction::left));
|
|
||||||
|
|
||||||
float speedRight =
|
float speedRight =
|
||||||
(linearVelocity - angularSpeed * this->wheelSeparation / 2) /
|
(linearVelocity - angularSpeed * this->wheelSeparation / 2) /
|
||||||
this->wheelRadius * Rad2Deg;
|
this->wheelRadius * Rad2Deg;
|
||||||
if (this->rightWheel != nullptr)
|
|
||||||
this->rightWheel->SetAngularVelocity(
|
this->SetWheelVelocity(speedLeft, speedRight);
|
||||||
Spherical(speedRight, Direction::right));
|
|
||||||
|
|
||||||
// std::cout << "lin. speed " << linearVelocity << " ang. speed " <<
|
// std::cout << "lin. speed " << linearVelocity << " ang. speed " <<
|
||||||
// angularVelocity.distance << " left wheel "
|
// angularVelocity.distance << " left wheel "
|
||||||
|
@ -13,8 +13,7 @@ class DifferentialDrive : public Thing {
|
|||||||
void SetDimensions(float wheelDiameter, float wheelSeparation);
|
void SetDimensions(float wheelDiameter, float wheelSeparation);
|
||||||
void SetMotors(Thing* leftWheel, Thing* rightWheel);
|
void SetMotors(Thing* leftWheel, Thing* rightWheel);
|
||||||
|
|
||||||
void SetLeftWheelVelocity(float angularSpeed);
|
void SetWheelVelocity(float speedLeft, float speedRight);
|
||||||
void SetRightWheelVelocity(float angularSpeed);
|
|
||||||
|
|
||||||
virtual void Update(unsigned long currentMs) override;
|
virtual void Update(unsigned long currentMs) override;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user