diff --git a/CMakeLists.txt b/CMakeLists.txt index 1bf4fc6..89fb8ad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,8 +5,9 @@ if(ESP_PLATFORM) INCLUDE_DIRS "." ) else() - project(RoboidCOntrol) + project(RoboidControl) add_subdirectory(LinearAlgebra) + add_subdirectory(Examples) set(CMAKE_CXX_STANDARD 17) # Enable c++11 standard set(CMAKE_POSITION_INDEPENDENT_CODE ON) diff --git a/Examples/BB2B.cpp b/Examples/BB2B.cpp new file mode 100644 index 0000000..5f8ed37 --- /dev/null +++ b/Examples/BB2B.cpp @@ -0,0 +1,41 @@ +#include + +#include "Thing.h" +#include "Things/DifferentialDrive.h" +#include "Things/TouchSensor.h" + +using namespace RoboidControl; + +void CollisionSteering(); + +DifferentialDrive* bb2b = nullptr; +TouchSensor* touchLeft = nullptr; +TouchSensor* touchRight = nullptr; + +int main() { + bb2b = new DifferentialDrive(); + + touchLeft = new TouchSensor(); + touchLeft->SetParent(bb2b); + + touchRight = new TouchSensor(); + touchRight->SetParent(bb2b); + + while (true) { + CollisionSteering(); + } + + return 0; +} + +void CollisionSteering() { + if (touchLeft->touchedSomething) + bb2b->SetWheelVelocity(bb2b->rightWheel, -0.5f); + else + bb2b->SetWheelVelocity(bb2b->rightWheel, 0.5f); + + if (touchRight->touchedSomething) + bb2b->SetWheelVelocity(bb2b->leftWheel, -0.5f); + else + bb2b->SetWheelVelocity(bb2b->leftWheel, 0.5f); +} \ No newline at end of file diff --git a/Examples/CMakeLists.txt b/Examples/CMakeLists.txt new file mode 100644 index 0000000..8594c06 --- /dev/null +++ b/Examples/CMakeLists.txt @@ -0,0 +1,22 @@ +# examples/CMakeLists.txt + +# Specify the minimum CMake version +cmake_minimum_required(VERSION 3.10) + +# Specify the path to the main project directory +set(MAIN_PROJECT_DIR "${CMAKE_SOURCE_DIR}/..") + +# Set the project name +project(Examples) + +include_directories(..) + +# Add the executable for the main project +#add_executable(MainExecutable ${SOURCES}) +# Find the main project library (assuming it's defined in the root CMakeLists.txt) +#find_package(RoboidControl REQUIRED) # Replace MyLibrary with your actual library name + +# Add example executables +add_executable(BB2B BB2B.cpp) +target_link_libraries(BB2B RoboidControl) # Link against your library + diff --git a/Things/DifferentialDrive.cpp b/Things/DifferentialDrive.cpp index 854c34a..f41dc0c 100644 --- a/Things/DifferentialDrive.cpp +++ b/Things/DifferentialDrive.cpp @@ -2,14 +2,18 @@ namespace RoboidControl { -RoboidControl::DifferentialDrive::DifferentialDrive(Participant* participant) : Thing(participant) { +RoboidControl::DifferentialDrive::DifferentialDrive(Participant* participant) + : Thing(participant) { // this->leftWheel = new Thing(participant); // this->rightWheel = new Thing(participant); } -void DifferentialDrive::SetDimensions(float wheelDiameter, float wheelSeparation) { - this->wheelRadius = wheelDiameter > 0 ? wheelDiameter / 2 : -wheelDiameter / 2; - this->wheelSeparation = wheelSeparation > 0 ? wheelSeparation : -wheelSeparation; +void DifferentialDrive::SetDimensions(float wheelDiameter, + float wheelSeparation) { + this->wheelRadius = + wheelDiameter > 0 ? wheelDiameter / 2 : -wheelDiameter / 2; + this->wheelSeparation = + wheelSeparation > 0 ? wheelSeparation : -wheelSeparation; this->rpsToMs = wheelDiameter * Passer::LinearAlgebra::pi; float distance = this->wheelSeparation / 2; @@ -30,6 +34,10 @@ void DifferentialDrive::SetMotors(Thing* leftWheel, Thing* rightWheel) { this->rightWheel->SetPosition(Spherical(distance, Direction::right)); } +void DifferentialDrive::SetLeftWheelVelocity(float angularSpeed) {} + +void DifferentialDrive::SetRightWheelVelocity(float angularSpeed) {} + void DifferentialDrive::Update(unsigned long currentMs) { // this assumes forward velocity only.... float linearVelocity = this->GetLinearVelocity().distance; @@ -41,16 +49,22 @@ void DifferentialDrive::Update(unsigned long currentMs) { angularSpeed = -angularSpeed; // wheel separation can be replaced by this->leftwheel->position->distance - float speedLeft = (linearVelocity + angularSpeed * this->wheelSeparation / 2) / this->wheelRadius * Rad2Deg; + float speedLeft = + (linearVelocity + angularSpeed * this->wheelSeparation / 2) / + this->wheelRadius * Rad2Deg; if (this->leftWheel != nullptr) this->leftWheel->SetAngularVelocity(Spherical(speedLeft, Direction::left)); - float speedRight = (linearVelocity - angularSpeed * this->wheelSeparation / 2) / this->wheelRadius * Rad2Deg; + float speedRight = + (linearVelocity - angularSpeed * this->wheelSeparation / 2) / + this->wheelRadius * Rad2Deg; if (this->rightWheel != nullptr) - this->rightWheel->SetAngularVelocity(Spherical(speedRight, Direction::right)); + this->rightWheel->SetAngularVelocity( + Spherical(speedRight, Direction::right)); - // std::cout << "lin. speed " << linearVelocity << " ang. speed " << angularVelocity.distance << " left wheel " - // << speedLeft << " right wheel " << speedRight << "\n"; + // std::cout << "lin. speed " << linearVelocity << " ang. speed " << + // angularVelocity.distance << " left wheel " + // << speedLeft << " right wheel " << speedRight << "\n"; } } // namespace RoboidControl \ No newline at end of file diff --git a/Things/DifferentialDrive.h b/Things/DifferentialDrive.h index 0614c3b..6b47b7b 100644 --- a/Things/DifferentialDrive.h +++ b/Things/DifferentialDrive.h @@ -12,6 +12,9 @@ class DifferentialDrive : public Thing { void SetDimensions(float wheelDiameter, float wheelSeparation); void SetMotors(Thing* leftWheel, Thing* rightWheel); + void SetLeftWheelVelocity(float angularSpeed); + void SetRightWheelVelocity(float angularSpeed); + virtual void Update(unsigned long currentMs) override; protected: