Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
22 changes: 6 additions & 16 deletions src/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use std::{
};
use tracing::{debug, error, info, warn};

use crate::config::Config;

use crate::imu::{self, ImuManager};

use crate::robot_description::{self, ActuatorId, RobotDescription};
Expand Down Expand Up @@ -38,15 +40,9 @@ pub struct Store {
kb_manager: crate::keyboard::KeyboardManager,
}

impl Default for Store {
fn default() -> Self {
Self::new()
}
}

impl Store {
pub fn new() -> Self {
let robot_description = RobotDescription::new();
pub fn new(config: &Config) -> Self {
let robot_description = RobotDescription::new(config);

let model_manager = ModelManager::new("model.kinfer", &robot_description)
.expect("Failed to create model manager");
Expand Down Expand Up @@ -617,17 +613,11 @@ pub struct BehaviorManager {
pending_fut: Option<StateFut>,
}

impl Default for BehaviorManager {
fn default() -> Self {
Self::new()
}
}

impl BehaviorManager {
pub fn new() -> Self {
pub fn new(config: &Config) -> Self {
Self {
state: Some(StateStore::Reset(Reset {
shared_state: Box::pin(Store::new()),
shared_state: Box::pin(Store::new(config)),
})),
target: None,
pending_fut: None,
Expand Down
9 changes: 9 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use std::path::PathBuf;

#[derive(Debug)]
pub struct Config {
pub policy_scale: f64,
pub kp_scale: f64,
pub kd_scale: f64,
pub log_path: PathBuf,
}
29 changes: 24 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod actuator;
pub mod actuator_manager;
pub mod behavior;
pub mod bytestream_fd;
pub mod config;
pub mod imu;
pub mod inference;
pub mod keyboard;
Expand All @@ -28,6 +29,8 @@ use std::task::{Context, Poll};

use crate::robstride::{ObtainIdRequest, ObtainIdResponse};

use config::Config;

use socketcan::CanFrame;
pub mod robstride;
pub mod robstride_utils;
Expand All @@ -44,10 +47,11 @@ use tracing::{Level, Metadata, debug, error, info, trace, warn};
use tracing_subscriber::{EnvFilter, Layer, fmt, layer::SubscriberExt};

use std::sync::mpsc;
use std::path::PathBuf;

use clap::Parser;
#[derive(Debug, Parser)]
#[command(name = "faux-rtos", about = "Parse three floats")]
#[command(name = "faux-rtos", about = "Parse three floats and a path")]
pub struct Args {
/// scale factor for the policy
#[arg(long, value_name = "FLOAT", default_value_t = 1.0)]
Expand All @@ -60,10 +64,14 @@ pub struct Args {
/// derivative gain scale
#[arg(long, value_name = "FLOAT", default_value_t = 1.0)]
kd_scale: f64,

/// path to log file
#[arg(long, value_name = "PATH", default_value = "events.log")]
kinfer_log_path: PathBuf,
}

async fn driver() -> std::io::Result<()> {
let mut behavior_manager = behavior::BehaviorManager::new();
async fn driver(config: &Config) -> std::io::Result<()> {
let mut behavior_manager = behavior::BehaviorManager::new(config);
let mut pinned = unsafe { Pin::new_unchecked(&mut behavior_manager) };
loop {
// iterate over each SlowCounter in sc_vec
Expand Down Expand Up @@ -95,11 +103,22 @@ async fn driver() -> std::io::Result<()> {
}

fn main() {
// Parse command line arguments
let args = Args::parse();

let config = Config {
policy_scale: args.policy_scale,
kp_scale: args.kp_scale,
kd_scale: args.kd_scale,
log_path: args.kinfer_log_path,
};


// Setup telemetry before we do anything else
let (tx, rx) = mpsc::sync_channel::<EventRecord>(1024 * 1024);

// Spawn the thread that will format and log our data
let jh = start_pipeline(rx, "events.log").expect("Failed to start telemetry pipeline");
let jh = start_pipeline(rx, &config.log_path).expect("Failed to start telemetry pipeline");

let trace_only_filter = tracing_subscriber::filter::FilterFn::new(|metadata: &Metadata| {
metadata.level() == &Level::TRACE && metadata.target().starts_with("faux_rtos")
Expand Down Expand Up @@ -137,7 +156,7 @@ fn main() {
// handle driver and SIGINT
let drv = async {
tokio::select! {
result = driver() => {
result = driver(&config) => {
match result {
Ok(_) => {
info!("Driver finished successfully");
Expand Down
35 changes: 6 additions & 29 deletions src/robot_description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use heapless::Deque;
use nalgebra as na;
use tracing::info;

use crate::config::Config;

pub fn normalize_actuator_qpos(mut qpos: f64) -> f64 {
const TWO_PI: f64 = 2.0 * std::f64::consts::PI;
// rem_euclid gives a value in [0, 2π)
Expand Down Expand Up @@ -310,23 +312,6 @@ pub enum DataType {
Time,
}

use clap::Parser;
#[derive(Debug, Parser)]
#[command(name = "faux-rtos", about = "Parse three floats")]
pub struct Args {
/// scale factor for the policy
#[arg(long, value_name = "FLOAT", default_value_t = 1.0)]
policy_scale: f64,

/// proportional gain scale
#[arg(long, value_name = "FLOAT", default_value_t = 1.0)]
kp_scale: f64,

/// derivative gain scale
#[arg(long, value_name = "FLOAT", default_value_t = 1.0)]
kd_scale: f64,
}

pub struct RobotDescription {
pub actuators: ActuatorStateStore,
pub imu: ImuData,
Expand All @@ -339,24 +324,16 @@ pub struct RobotDescription {
pub kd_scale: f64,
}

impl Default for RobotDescription {
fn default() -> Self {
Self::new()
}
}

impl RobotDescription {
pub fn new() -> Self {
let args = Args::parse();
info!("Args; {:?}", args);
pub fn new(config: &Config) -> Self {
Self {
actuators: ActuatorStateStore::new(),
imu: ImuData::default(),
initial_imu: ImuData::default(),
kb_pending_events: Deque::new(),
kp_scale: args.kp_scale,
kd_scale: args.kd_scale,
policy_scale: args.policy_scale,
kp_scale: config.kp_scale,
kd_scale: config.kd_scale,
policy_scale: config.policy_scale,
home_position: enum_map! {
ActuatorId::Lsp => ActuatorCommand { qpos: 0.0, kp: 100.0, kd: 8.284, ..Default::default() },
ActuatorId::Lsr => ActuatorCommand { qpos: (10.0_f64).to_radians(), kp: 100.0, kd: 8.257, ..Default::default() },
Expand Down
12 changes: 10 additions & 2 deletions src/telemetry/telemetry_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ use std::os::unix::io::{AsFd, AsRawFd, OwnedFd};
use std::net::UdpSocket;
use std::os::unix::fs::OpenOptionsExt;

use std::path::Path;
use std::ptr;
use std::thread;
use std::time::Instant;

use nix::libc;
use std::sync::mpsc::Receiver;
use tracing::error;
use tracing::{info, error};

use crate::telemetry::forwarder::{EventRecord, FieldValue};
use crate::telemetry::multi_fd_writer::MultiFdWriter;
Expand All @@ -22,8 +23,15 @@ const BUF_SIZE: usize = 4 * 4096;
/// spawns a thread that will handle the I/O operations (read events and write to disk)
pub fn start_pipeline(
rx: Receiver<EventRecord>,
log_path: &str,
log_path: &Path,
) -> io::Result<thread::JoinHandle<()>> {

if let Some(parent) = log_path.parent() {
std::fs::create_dir_all(parent)?;
}

info!("Logging to: {:?}", log_path);

// Open log file for writing
let file = OpenOptions::new()
.create(true)
Expand Down