First trajectory

This commit is contained in:
Pascal Serrarens 2023-11-24 17:11:18 +01:00
parent 28ee532700
commit 6058d27079
9 changed files with 119 additions and 25 deletions

View File

@ -1,7 +1,8 @@
#include "ControlledMotor.h" #include "ControlledMotor.h"
ControlledMotor::ControlledMotor() { ControlledMotor::ControlledMotor() {
this->isControlledMotor = true; // this->isControlledMotor = true;
this->type = Type::ControlledMotor;
} }
ControlledMotor::ControlledMotor(Motor* motor, Encoder* encoder) { ControlledMotor::ControlledMotor(Motor* motor, Encoder* encoder) {

View File

@ -1,7 +1,11 @@
#include "Motor.h" #include "Motor.h"
#include <time.h>
#include <Arduino.h>
Motor::Motor() { Motor::Motor() {
this->isMotor = true; // this->isMotor = true;
type = Type::Motor;
} }
// Motor::Motor(uint8_t pinIn1, uint8_t pinIn2) { // Motor::Motor(uint8_t pinIn1, uint8_t pinIn2) {
@ -25,9 +29,25 @@ Motor::Motor() {
// } // }
float Motor::GetSpeed() { float Motor::GetSpeed() {
return this->currentSpeed; return this->currentSpeed;
} }
void Motor::SetSpeed(float speed) { void Motor::SetSpeed(float speed) {
this->currentSpeed = speed; this->currentSpeed = speed;
}
bool Motor::Drive(float distance) {
if (!this->driving) {
this->startTime = time(NULL);
this->targetDistance = abs(distance);
this->driving = true;
}
double duration = difftime(time(NULL), this->startTime);
if (duration >= this->targetDistance) {
this->driving = false;
return true;
}
SetSpeed(distance < 0 ? -1 : 1);
return false;
} }

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <time.h>
#include "Thing.h" #include "Thing.h"
class Motor : public Thing { class Motor : public Thing {
@ -14,6 +15,11 @@ class Motor : public Thing {
virtual void SetSpeed(float speed); virtual void SetSpeed(float speed);
float GetSpeed(); float GetSpeed();
bool Drive(float distance);
protected: protected:
float currentSpeed = 0; float currentSpeed = 0;
bool driving = false;
float targetDistance = 0;
time_t startTime = 0;
}; };

View File

@ -19,7 +19,8 @@ void Propulsion::AddMotors(Placement* things, unsigned int thingCount) {
this->motorCount = 0; this->motorCount = 0;
for (unsigned int thingIx = 0; thingIx < thingCount; thingIx++) { for (unsigned int thingIx = 0; thingIx < thingCount; thingIx++) {
Thing* thing = things[thingIx].thing; Thing* thing = things[thingIx].thing;
if (thing->isMotor) // if (thing->isMotor)
if (thing->type == Thing::Type::Motor)
motorCount++; motorCount++;
} }
this->placement = new Placement[motorCount]; this->placement = new Placement[motorCount];
@ -27,7 +28,8 @@ void Propulsion::AddMotors(Placement* things, unsigned int thingCount) {
unsigned int motorIx = 0; unsigned int motorIx = 0;
for (unsigned int thingIx = 0; thingIx < thingCount; thingIx++) { for (unsigned int thingIx = 0; thingIx < thingCount; thingIx++) {
Thing* thing = things[thingIx].thing; Thing* thing = things[thingIx].thing;
if (thing->isMotor) // if (thing->isMotor)
if (thing->type == Thing::Type::Motor)
this->placement[motorIx++] = things[thingIx]; this->placement[motorIx++] = things[thingIx];
} }
} }
@ -45,7 +47,8 @@ Motor* Propulsion::GetMotor(unsigned int motorId) {
return nullptr; return nullptr;
Thing* thing = this->placement[motorId].thing; Thing* thing = this->placement[motorId].thing;
if (thing->isMotor) // if (thing->isMotor)
if (thing->type == Thing::Type::Motor)
return (Motor*)thing; return (Motor*)thing;
return nullptr; return nullptr;

View File

@ -1,5 +1,5 @@
#include "Roboid.h" #include "Roboid.h"
#include <Arduino.h>
Roboid::Roboid() { Roboid::Roboid() {
this->configuration = nullptr; this->configuration = nullptr;
this->thingCount = 0; this->thingCount = 0;
@ -16,16 +16,51 @@ Roboid::Roboid(Placement configuration[], unsigned int thingCount) {
bool Roboid::Drive(float forwardDistance) { bool Roboid::Drive(float forwardDistance) {
bool finished = true; bool finished = true;
for (unsigned int motorIx = 0; motorIx < propulsion.motorCount; motorIx++) { for (unsigned int motorIx = 0; motorIx < propulsion.motorCount; motorIx++) {
Motor* motor = (Motor*)propulsion.placement[motorIx].thing; bool motorFinished = false;
if (motor == nullptr) Thing* thing = propulsion.placement[motorIx].thing;
continue;
if (motor->isControlledMotor == false)
continue;
ControlledMotor* controlledMotor = (ControlledMotor*)motor; switch (thing->type) {
bool motorFinished = controlledMotor->Drive(forwardDistance); case Thing::Type::ControlledMotor: {
if (motorFinished == false) ControlledMotor* controlledMotor = (ControlledMotor*)thing;
motorFinished = controlledMotor->Drive(forwardDistance);
break;
}
case Thing::Type::Motor: {
Motor* motor = (Motor*)thing;
motorFinished = motor->Drive(forwardDistance);
break;
}
default:
break;
}
if (motorFinished == false) {
finished = false; finished = false;
} else
Serial.printf("Motor finished\n");
} }
return finished; return finished;
} }
float waypointArray[] = {1, -1};
void Roboid::FollowTrajectory(Trajectory* Trajectory) {
this->waypoints = waypointArray;
this->waypointCount = 2;
this->waypointIx = 0;
}
void Roboid::Update() {
if (this->waypoints == nullptr)
return;
// Serial.printf("Driving waypoints %d %f\n", this->waypointIx,
// waypoints[waypointIx]);
if (Drive(this->waypoints[this->waypointIx])) {
this->waypointIx++;
if (this->waypointIx == this->waypointCount) {
this->waypoints = nullptr;
this->waypointIx = 0;
this->waypointCount = 0;
propulsion.SetTwistSpeed(0, 0);
}
}
}

View File

@ -5,6 +5,15 @@
#include "Placement.h" #include "Placement.h"
#include "Propulsion.h" #include "Propulsion.h"
class WayPoint {
public:
float distance = 0;
};
class Trajectory {
public:
float* waypoints;
};
class Acceleration { class Acceleration {
public: public:
float GetMagnitude() { return 0; }; float GetMagnitude() { return 0; };
@ -22,5 +31,13 @@ class Roboid {
Placement* configuration; Placement* configuration;
unsigned int thingCount; unsigned int thingCount;
void Update();
bool Drive(float forwardDistance); bool Drive(float forwardDistance);
void FollowTrajectory(Trajectory* Trajectory);
public:
float* waypoints = nullptr;
unsigned int waypointIx = 0;
unsigned int waypointCount = 0;
}; };

View File

@ -29,7 +29,8 @@ void Sensing::AddSensors(Placement* things, unsigned int thingCount) {
sensorCount = 0; sensorCount = 0;
for (unsigned int thingIx = 0; thingIx < thingCount; thingIx++) { for (unsigned int thingIx = 0; thingIx < thingCount; thingIx++) {
Thing* thing = things[thingIx].thing; Thing* thing = things[thingIx].thing;
if (thing->isSensor) // if (thing->isSensor)
if (thing->type == Thing::Type::Sensor)
sensorCount++; sensorCount++;
} }
@ -38,7 +39,8 @@ void Sensing::AddSensors(Placement* things, unsigned int thingCount) {
unsigned int sensorIx = 0; unsigned int sensorIx = 0;
for (unsigned int thingIx = 0; thingIx < thingCount; thingIx++) { for (unsigned int thingIx = 0; thingIx < thingCount; thingIx++) {
Thing* thing = things[thingIx].thing; Thing* thing = things[thingIx].thing;
if (thing->isSensor) // if (thing->isSensor)
if (thing->type == Thing::Type::Sensor)
sensorPlacements[sensorIx++] = things[thingIx]; sensorPlacements[sensorIx++] = things[thingIx];
} }
} }
@ -52,7 +54,8 @@ Sensor* Sensing::GetSensor(unsigned int sensorId) {
return nullptr; return nullptr;
Thing* thing = this->sensorPlacements[sensorId].thing; Thing* thing = this->sensorPlacements[sensorId].thing;
if (thing->isSensor) // if (thing->isSensor)
if (thing->type == Thing::Type::Sensor)
return (Sensor*)thing; return (Sensor*)thing;
return nullptr; return nullptr;
@ -167,7 +170,9 @@ bool Sensing::SwitchOn(float fromAngle, float toAngle) {
} }
unsigned int Sensing::ToDepthMapIndex(float angle) { unsigned int Sensing::ToDepthMapIndex(float angle) {
unsigned int depthMapIx = (unsigned int)(((angle - rangeMinimum) / (rangeMaximum - rangeMinimum)) * (float)resolution); unsigned int depthMapIx =
(unsigned int)(((angle - rangeMinimum) / (rangeMaximum - rangeMinimum)) *
(float)resolution);
return depthMapIx; return depthMapIx;
} }

View File

@ -1,5 +1,6 @@
#include "Sensor.h" #include "Sensor.h"
Sensor::Sensor() { Sensor::Sensor() {
this->isSensor = true; // this->isSensor = true;
type = Type::Sensor;
} }

14
Thing.h
View File

@ -3,8 +3,14 @@
/// @brief A thing is a functional component on a robot /// @brief A thing is a functional component on a robot
class Thing { class Thing {
public: public:
Thing() { isSensor = false, isMotor = false; } Thing() {
bool isSensor; type = Type::Undetermined;
bool isMotor; // isSensor = false, isMotor = false;
bool isControlledMotor; }
enum class Type { Undetermined, Sensor, Motor, ControlledMotor };
Type type = Type::Undetermined;
// bool isSensor;
// bool isMotor;
// bool isControlledMotor;
}; };