Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic Traction Demonstration State Machine #123

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cmake_minimum_required(VERSION 3.16.0 FATAL_ERROR)
# -------------------------------------------------- #

# List of all libraries for linking purposes
set(ALL_LIBS "data;brakes;navigation;propulsion;propulsion_can;sensors;state_machine;telemetry;utils;utils_concurrent;utils_io;utils_math")
set(ALL_LIBS "data;brakes;navigation;propulsion;propulsion_can;sensors;state_machine;demo_state_machine;telemetry;utils;utils_concurrent;utils_io;utils_math")
function(make_lib target, include_path)
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
file(GLOB code "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
Expand Down Expand Up @@ -117,6 +117,7 @@ include_directories(
# building all the libraries and tests
add_subdirectory(${CMAKE_SOURCE_DIR}/src)


if (CROSS)
else()
add_subdirectory(${CMAKE_SOURCE_DIR}/test)
Expand All @@ -142,6 +143,23 @@ target_link_libraries(${target}
utils_math
)

set(target "hyped_demo")
add_executable(${target} ${CMAKE_SOURCE_DIR}/run/demo.cpp)
target_link_libraries(${target}
data
brakes
navigation
propulsion
propulsion_can
sensors
demo_state_machine
telemetry
utils
utils_concurrent
utils_io
utils_math
)

set(target "debugger")
add_executable(${target} ${CMAKE_SOURCE_DIR}/run/debugger.cpp)
target_link_libraries(${target}
Expand Down
59 changes: 59 additions & 0 deletions configurations/demo/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"fake_trajectory": {
"maximum_acceleration": 1000.0,
"braking_deceleration": 2000.0,
"cruising_deceleration": 0.01
},
"system": {
"log_level": 0,
"use_fake_trajectory": true,
"use_fake_batteries": true,
"use_fake_batteries_fail": false,
"use_fake_temperature": true,
"use_fake_temperature_fail": false,
"use_fake_pressure": true,
"use_fake_pressure_fail": false,
"use_fake_brakes": true,
"use_fake_controller": true,
"use_fake_high_power": true,
"axis": 0
},
"brakes": {
"pins": [
{
"command_pin": 87,
"button_pin": 66
},
{
"command_pin": 10,
"button_pin": 69
}
]
},
"sensors": {
"bms_startup_time_micros": 5000000,
"imu_pins": [22, 27, 47, 86],
"temperature_pins": [3, 4, 5, 6],
"ambient_pressure_pins": {
"pressure_pin": 10,
"temperature_pin": 11
}
},
"telemetry": {
"server_ip": "127.0.01",
"server_port": "9090"
},
"fake_imu": [
{
"noise": 0.01
},
{
"noise": 0.02
},
{
"noise": 0.005
},
{
"noise": 0.001
} ]
}
53 changes: 53 additions & 0 deletions run/demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <fstream>
#include <iostream>
#include <memory>

#include <brakes/main.hpp>
#include <navigation/main.hpp>
#include <propulsion/main.hpp>
#include <sensors/main.hpp>
#include <demo_state_machine/main.hpp>
#include <demo_state_machine/UI.hpp>
#include <telemetry/main.hpp>
#include <utils/system.hpp>

int main(int argc, char *argv[])
{
hyped::utils::System::parseArgs(argc, argv);
// print HYPED logo at system startup
std::ifstream file("main_logo.txt");
if (file.is_open()) {
std::string buffer;
file >> buffer;
std::cout << buffer;
}

// Initalise the threads here
hyped::brakes::Main brakes;
hyped::navigation::Main navigation;
hyped::propulsion::Main propulsion;
hyped::sensors::Main sensors;
hyped::demo_state_machine::Main demo_state_machine;
hyped::telemetry::Main telemetry;

// Start the threads here
brakes.start();
navigation.start();
propulsion.start();
sensors.start();
demo_state_machine.start();
telemetry.start();

hyped::demo_state_machine::Ui Ui;
Ui.run();

// Join the threads here
brakes.join();
navigation.join();
propulsion.join();
sensors.join();
demo_state_machine.join();
telemetry.join();

return 0;
}
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ add_subdirectory(navigation)
add_subdirectory(propulsion)
add_subdirectory(sensors)
add_subdirectory(state_machine)
add_subdirectory(demo_state_machine)
add_subdirectory(telemetry)
add_subdirectory(utils)
1 change: 1 addition & 0 deletions src/data/data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ std::optional<State> stateFromString(const std::string &state_name);
struct StateMachine {
bool critical_failure;
State current_state;
static constexpr int64_t kAccelerationTime = 0.12e+06; // microseconds
};

// -------------------------------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions src/demo_state_machine/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set(target "demo_state_machine")
make_lib(${target} "${CMAKE_CURRENT_SOURCE_DIR}/..")
132 changes: 132 additions & 0 deletions src/demo_state_machine/UI.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#include "UI.hpp"
mifrandir marked this conversation as resolved.
Show resolved Hide resolved

#include <fstream>
#include <iostream>
#include <sstream>

namespace hyped::demo_state_machine {

Ui::Ui()
: system_(utils::System::getSystem()),
log_("UI", utils::System::getSystem().config_.log_level),
data_(data::Data::getInstance())
{
addCalibrationCommand();
addLaunchCommand();
addBrakingCommand();
addShutDownCommand();
addHelpCommand();
addQuitCommand();
}

void Ui::run()
{
printAvailableCommands();
while (system_.isRunning()) {
printCurrentState();
readAndHandleCommand();
}
}

void Ui::printCurrentState()
{
auto const current_state = data_.getStateMachineData().current_state;
std::cout << "Current State: " << ::hyped::data::stateToString(current_state)->c_str()
<< std::endl;
std::cout << "------------------------------" << std::endl;
}

void Ui::printAvailableCommands()
{
std::cout << "Available options:" << std::endl;
for (auto &option : commands_) {
std::cout << " * `" << option.identifier;
std::cout << "' - " << option.description;
std::cout << std::endl;
}
}

void Ui::readAndHandleCommand()
{
auto const current_state = data_.getStateMachineData().current_state;
mifrandir marked this conversation as resolved.
Show resolved Hide resolved
if (current_state == data::State::kPreCalibrating || current_state == data::State::kReady
|| current_state == data::State::kAccelerating || current_state == data::State::kCruising
|| current_state == data::State::kFinished) {
std::cout << "> ";
std::string command;
std::getline(std::cin, command);
auto identifier_and_handler = handlers_by_identifier_.find(command);
if (identifier_and_handler == handlers_by_identifier_.end()) {
std::cout << "Unknown command: " << command << std::endl;
return;
}
identifier_and_handler->second();
}
}

void Ui::addCommand(const Command &command)
{
commands_.push_back(command);
handlers_by_identifier_.emplace(command.identifier, command.handler);
}

void Ui::addQuitCommand()
{
addCommand({"quit", "Turn the system off", [this]() { system_.stop(); }});
}

void Ui::addHelpCommand()
{
addCommand({"help", "Print all options", [this]() { printAvailableCommands(); }});
}

void Ui::giveCalibrationCommand()
{
telemetry_data_ = data_.getTelemetryData();
telemetry_data_.calibrate_command = true;
data_.setTelemetryData(telemetry_data_);
}

void Ui::addCalibrationCommand()
{
addCommand(
{"calibrate", "Allows pod to enter Calibrating", [this]() { giveCalibrationCommand(); }});
}

void Ui::giveLaunchCommand()
{
telemetry_data_ = data_.getTelemetryData();
telemetry_data_.launch_command = true;
data_.setTelemetryData(telemetry_data_);
}

void Ui::addLaunchCommand()
{
addCommand({"launch", "Allows pod to enter Accelerating", [this]() { giveLaunchCommand(); }});
}

void Ui::giveBrakingCommand()
{
telemetry_data_ = data_.getTelemetryData();
telemetry_data_.emergency_stop_command = true;
data_.setTelemetryData(telemetry_data_);
}

void Ui::addBrakingCommand()
{
addCommand({"brake", "Allows pod to enter Pre-Braking", [this]() { giveBrakingCommand(); }});
}

void Ui::giveShutDownCommand()
{
telemetry_data_ = data_.getTelemetryData();
telemetry_data_.shutdown_command = true;
data_.setTelemetryData(telemetry_data_);
}

void Ui::addShutDownCommand()
{
addCommand({"shut", "Allows pod to turn off", [this]() { giveShutDownCommand(); }});
}

} // namespace hyped::demo_state_machine
54 changes: 54 additions & 0 deletions src/demo_state_machine/UI.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

#include <atomic>
#include <functional>
#include <optional>
#include <string>

#include <data/data.hpp>
#include <utils/logger.hpp>
#include <utils/system.hpp>

namespace hyped::demo_state_machine {

class Ui {
public:
using Handler = std::function<void(void)>;
void printAvailableCommands();
void printCurrentState();
void readAndHandleCommand();

struct Command {
std::string identifier;
std::string description;
Handler handler;
};

Ui();

void run();

private:
utils::System &system_;
utils::Logger log_;
data::Data &data_;
std::vector<Command> commands_;
std::unordered_map<std::string, Handler> handlers_by_identifier_;

data::Telemetry telemetry_data_;

void addCommand(const Command &command);
void addQuitCommand();
void addHelpCommand();
void addCalibrationCommand();
void addLaunchCommand();
void addBrakingCommand();
void addShutDownCommand();

void giveCalibrationCommand();
void giveLaunchCommand();
void giveBrakingCommand();
void giveShutDownCommand();
};

} // namespace hyped::demo_state_machine
Loading