diff --git a/app/raspberry_pi/src/car/system/CarSystem.cpp b/app/raspberry_pi/src/car/system/CarSystem.cpp index c637779b..d1c6a20e 100644 --- a/app/raspberry_pi/src/car/system/CarSystem.cpp +++ b/app/raspberry_pi/src/car/system/CarSystem.cpp @@ -1,7 +1,12 @@ #include "CarSystem.h" namespace car::system { - CarSystem::CarSystem(const std::string& websocket_url, std::unique_ptr lidar_device, std::unique_ptr messaging_system) : lidar_device(std::move(lidar_device)), messaging_system(std::move(messaging_system)) + CarSystem::CarSystem( + const std::string& websocket_url, + std::unique_ptr lidar_device, + std::unique_ptr messaging_system, + std::unique_ptr movement_system + ) : lidar_device(std::move(lidar_device)), messaging_system(std::move(messaging_system)), movement_system(std::move(movement_system)) { } @@ -62,5 +67,6 @@ namespace car::system { void CarSystem::turn(const TurnCommand& turn_command) { + this->movement_system->turn(turn_command); } } \ No newline at end of file diff --git a/app/raspberry_pi/src/car/system/CarSystem.h b/app/raspberry_pi/src/car/system/CarSystem.h index 2ae993ba..aa40f8f3 100644 --- a/app/raspberry_pi/src/car/system/CarSystem.h +++ b/app/raspberry_pi/src/car/system/CarSystem.h @@ -9,17 +9,19 @@ #include "lidar/LidarDevice.hpp" #include "messaging/MessagingSystem.hpp" +#include "movement/MovementSystem.hpp" using json = nlohmann::json; using namespace car::system::lidar; using namespace car::system::messaging; +using namespace car::system::movement; namespace car::system { class CarSystem { public: - CarSystem(const std::string& websocket_url, std::unique_ptr lidar_device, std::unique_ptr messaging_system); + CarSystem(const std::string& websocket_url, std::unique_ptr lidar_device, std::unique_ptr messaging_system, std::unique_ptr movement_system); ~CarSystem(); void initalize(); @@ -34,6 +36,7 @@ namespace car::system { private: std::unique_ptr lidar_device; std::unique_ptr messaging_system; + std::unique_ptr movement_system; bool terminated = false; }; diff --git a/app/raspberry_pi/src/car/system/movement/MovementSystem.hpp b/app/raspberry_pi/src/car/system/movement/MovementSystem.hpp new file mode 100644 index 00000000..d4224743 --- /dev/null +++ b/app/raspberry_pi/src/car/system/movement/MovementSystem.hpp @@ -0,0 +1,72 @@ +#ifndef MOVEMENTSYSTEM_HPP +#define MOVEMENTSYSTEM_HPP + +#pragma once + +#include + +#include "PCA9685.h" + +#include "../messaging/commands/MoveCommand.hpp" +#include "../messaging/commands/TurnCommand.hpp" + +using namespace car::system::messaging::commands; + +namespace car::system::movement { + constexpr int MIN_PULSE_WIDTH = 900; + constexpr int MAX_PULSE_WIDTH = 2100; + constexpr int FREQUENCY = 50; + + class MovementSystem + { + public: + MovementSystem() { + }; + + void initalize() + { + this->pwn->init(1, 0x40); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + this->pwn->setPWMFreq(FREQUENCY); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + } + + void start() + { + } + + void terminate() { + } + //def map(self, x, in_min, in_max, out_min, out_max): + int map(int x, int in_min, int in_max, int out_min, int out_max) { + return ((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min); + } + + //def _angle_to_analog(self, angle): + int setAngleToAnalog(int angle) { + float pulse_wide; + int analog_value; + + pulse_wide = map(angle, 0, 180, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH); + analog_value = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096); + return (analog_value); + } + + void turn(const TurnCommand& command) { + const float angle = std::clamp(command.angle, 1.0f, 179.0f); + for (int channel = 0; channel < 2; channel++) { + int val = 0; + val = setAngleToAnalog(angle); + this->pwm->setPWM(channel, 0, val); + } + } + + ~MovementSystem() { + }; + + private: + std::unique_ptr pwm; + }; +}; + +#endif \ No newline at end of file diff --git a/app/raspberry_pi/xmake.lua b/app/raspberry_pi/xmake.lua index 8ea0b3c4..60fabc96 100644 --- a/app/raspberry_pi/xmake.lua +++ b/app/raspberry_pi/xmake.lua @@ -12,11 +12,8 @@ add_requires("nod") add_requires("ftxui") add_requires("nlohmann_json") - add_requires("spdlog") - add_requires("fmt") - add_requires("imath") if is_plat("linux", "macosx") then @@ -31,17 +28,24 @@ add_requires("rplidar") target("raspberry_pi") set_kind("binary") - + set_license("LGPL-2.1") + add_packages("spdlog") add_packages("fmt") + add_packages("nlohmann_json") add_packages("imath") + add_packages("tl_expected") + -- Messaging System add_packages("ixwebsocket") add_packages("nod") + + -- UI add_packages("ftxui") - add_packages("nlohmann_json") + + -- For the SunFounder Car add_packages("rplidar") - add_packages("tl_expected") + add_packages("pca9685") add_headerfiles("include/**.h") @@ -59,12 +63,13 @@ target("raspberry_pi") -- From xmake sample code: -if is_plat("linux", "macosx") then +if is_plat("linux") then for _, file in ipairs(os.files("tests/pca9685/test_*.cpp")) do local name = path.basename(file) target(name) set_kind("binary") set_default(false) + set_license("LGPL-2.1") add_packages("pca9685")