Initial rough-in of motor controller

This commit is contained in:
2024-08-17 15:10:06 -04:00
parent 3633f636c6
commit 3185746213
11 changed files with 653 additions and 62 deletions

View File

@ -1,47 +1,99 @@
//use esp_idf_hal::peripherals::Peripherals;
const BUTTON_HOLD_TIME_MS: u64 = 500;
const STOP_SAFETY_TIME_MS: u64 = 2000;
use esp_idf_svc::timer::EspTaskTimerService;
use core::time::Duration;
use log::{info, error};
// Debug crates
//use esp_idf_svc::timer::EspTaskTimerService;
//use core::time::Duration;
// Crates used in release
use log::*; //{trace, debug, info, warn, error}
use anyhow::Result;
use async_executor::Executor;
use futures_lite::future;
use std::boxed::Box;
// Debug modules
mod test_console;
//mod test_driver_tattle;
// Release modules
mod commands;
mod dispatch;
mod motor_controller;
mod motor_driver;
mod message_timer;
use crate::message_timer::MessageTimer;
use crate::commands::Commands;
//TODO: limit switch driver, would be good in long run if it checked limit switches periodically (every 1s?) to ensure they are still functioning
fn main() {
// Do basic initialization
esp_idf_svc::sys::link_patches();
esp_idf_svc::log::EspLogger::initialize_default();
info!("Logging initialized");
//let peripherals = Peripherals::take().unwrap();
log::set_max_level(log::LevelFilter::Info);
match future::block_on(main_loop()) {
Ok(_) => {error!("Exited main loop normally, but this should be impossible.")}
Err(e) => {error!("Exited main loop with error {}", e)}
};
//TODO: can we restart the microcontroller here?
}
/*
async fn test_loop() -> Result<()> {
let timer_service = EspTaskTimerService::new()?;
let mut async_timer = timer_service.timer_async()?;
loop {
async_timer.after(Duration::from_secs(2)).await?;
async_timer.after(Duration::from_secs(5)).await?;
debug!("Tick ");
}
}
} */
async fn main_loop() -> Result<()> {
info!("Entering main loop");
// Create dispatch early so it can outlive most other things
let mut dp = dispatch::Dispatch::new();
// Debug Drivers
let motor_driver = Box::new(motor_driver::MotorDriverDebug {});
// Setup of various drivers that need to out-live the executor
let m_chan = motor_controller::Controller::prepare_controller(&mut dp);
let mut motor_control = motor_controller::Controller::new(m_chan, dp.get_cmd_channel(), motor_driver);
// Setup callback timers
let mut button_timer = MessageTimer::<Commands, Commands>::new_on_dispatch(
Commands::ButtonTimerRestart,
Commands::ButtonTimerClear,
Commands::ButtonTimerExpired,
BUTTON_HOLD_TIME_MS,
&mut dp);
let mut stopping_timer = MessageTimer::<Commands, Commands>::new_on_dispatch(
Commands::StopTimerRestart,
Commands::StopTimerClear,
Commands::StopTimerExpired,
STOP_SAFETY_TIME_MS,
&mut dp);
let executor = Executor::new();
let mut tasks:Vec<_> = Vec::new();
//Queueu up our async tasks
tasks.push(executor.spawn(test_console::start_cli()));
tasks.push(executor.spawn(test_loop()));
//Queue Up debug tasks (TODO: remove)
let cli_endpoint = dp.get_cmd_channel();
tasks.push(executor.spawn(test_console::start_cli(cli_endpoint)));
//tasks.push(executor.spawn(test_loop()));
//let t_chan = test_driver_tattle::prepare_tattle(&mut dp).expect("Failed to set up debugging copycat driver");
//tasks.push(executor.spawn(test_driver_tattle::start_tattle(t_chan)));
// Queueu up our async tasks
tasks.push(executor.spawn(motor_control.run()));
tasks.push(executor.spawn(button_timer.run()));
tasks.push(executor.spawn(stopping_timer.run()));
tasks.push(executor.spawn(dp.cmd_loop()));
//Once we have all our tasks, await on them all to run them in parallel.
for task in tasks {