Skip to content

Commit

Permalink
improved structure and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelNoesslboeck committed Apr 21, 2023
1 parent 9ad9533 commit 7c7f1e3
Show file tree
Hide file tree
Showing 18 changed files with 313 additions and 273 deletions.
62 changes: 33 additions & 29 deletions src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,60 @@ mod json;
pub use json::*;

#[derive(Debug)]
pub struct MachineConfig<const N : usize, const D : usize, const A : usize>
pub struct MachineConfig<const C : usize>
{
pub name: String,

pub lk : Arc<LinkedData>,

pub anchor : Vec3,
pub dims : [Vec3; D],
pub axes : [Vec3; A],
pub dims : Vec<Vec3>,
pub axes : Vec<Vec3>,

pub tools : Vec<Box<dyn Tool + Send>>,

pub vels : [Omega; N],
pub home : [Gamma; N],
pub meas_dist : [Delta; N],
pub vels : [Omega; C],
pub home : [Gamma; C],
pub meas_dist : [Delta; C],

pub ang : [AngleData; N],
pub sim : [SimData; N],
pub meas : [MeasInstance; N],
pub limit : [LimitDecl; N]
pub ang : [AngleData; C],
pub sim : [SimData; C],
pub meas : [MeasInstance; C],
pub limit : [LimitDecl; C]
}

impl<const N : usize, const D : usize, const A : usize> Default for MachineConfig<N, D, A>
impl<const C : usize> Default for MachineConfig<C>
{
fn default() -> Self {
Self {
name: String::new(),
lk: Default::default(),

anchor: Default::default(),
dims: [Default::default(); D],
axes: [Default::default(); A],
dims: Default::default(),
axes: Default::default(),

tools: vec![],

vels: [Omega::ZERO; N],
home: [Gamma::ZERO; N],
meas_dist: [Delta::ZERO; N],
vels: [Omega::ZERO; C],
home: [Gamma::ZERO; C],
meas_dist: [Delta::ZERO; C],

ang: [Default::default(); N],
sim: [Default::default(); N],
meas: [Default::default(); N],
limit: [Default::default(); N]
ang: [Default::default(); C],
sim: [Default::default(); C],
meas: [Default::default(); C],
limit: [Default::default(); C]
}
}
}

impl<const N : usize, const D : usize, const A : usize> MachineConfig<N, D, A>
impl<const C : usize> MachineConfig<C>
{
pub fn get_axes(&self, angles : &[Phi; A]) -> Vec<Mat3> {
pub fn get_axes<const A : usize>(&self, angles : &[Phi; A]) -> Vec<Mat3> {
if A > self.axes.len() {
panic!("Bad number of axes! (Required: {}, Present: {})", A, self.axes.len());
}

let mut matr = vec![];

for i in 0 .. A {
Expand All @@ -86,12 +90,12 @@ impl<const N : usize, const D : usize, const A : usize> MachineConfig<N, D, A>
}
}

impl<const N : usize, const D : usize, const A : usize> MachineConfig<N, D, A>
impl<const C : usize> MachineConfig<C>
{
pub fn gammas_from_phis(&self, phis : [Phi; N]) -> [Gamma; N] {
let mut gammas = [Gamma::ZERO; N];
pub fn gammas_from_phis(&self, phis : [Phi; C]) -> [Gamma; C] {
let mut gammas = [Gamma::ZERO; C];

for i in 0 .. N {
for i in 0 .. C {
gammas[i] = if self.ang[i].counter {
-phis[i].force_to_gamma() + self.ang[i].offset
} else {
Expand All @@ -102,10 +106,10 @@ impl<const N : usize, const D : usize, const A : usize> MachineConfig<N, D, A>
gammas
}

pub fn phis_from_gammas(&self, gammas : [Gamma; N]) -> [Phi; N] {
let mut phis = [Phi::ZERO; N];
pub fn phis_from_gammas(&self, gammas : [Gamma; C]) -> [Phi; C] {
let mut phis = [Phi::ZERO; C];

for i in 0 .. N {
for i in 0 .. C {
phis[i] = (if self.ang[i].counter {
-gammas[i]
} else {
Expand Down
30 changes: 15 additions & 15 deletions src/conf/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use stepper_lib::data::LinkedData;
use crate::conf::{ConfigElement, MachineConfig};
use crate::partlib;


/// Struct for parsing a JSON-configuration file (".conf.json") and
#[derive(Serialize, Deserialize)]
pub struct JsonConfig
{
Expand Down Expand Up @@ -75,13 +75,13 @@ impl JsonConfig
Ok(comp_group)
}

pub fn get_machine<const N : usize, const D : usize, const A : usize>(&self) -> Result<MachineConfig<N, D, A>, std::io::Error> {
pub fn get_machine<const N : usize>(&self, dim : usize, rot : usize) -> Result<MachineConfig<N>, std::io::Error> {
if self.comps.len() != N {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData,
format!("Not enough components for machine! [Required: {}, Given: {}]", N, self.comps.len())))
}

let mut mach : MachineConfig<N, D, A> = Default::default();
let mut mach : MachineConfig<N> = Default::default();

// Init
mach.name = self.name.clone();
Expand All @@ -93,23 +93,23 @@ impl JsonConfig
};

mach.dims = match &self.dims {
Some(dims) => match dims.iter().map(|axis_raw| Vec3::from(*axis_raw)).collect::<Vec<Vec3>>().try_into() {
Ok(val) => val,
Err(_) => return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData, format!("Not enough dimensions defined for machine! [Required: {}, Given: {}]", D, dims.len())))
},
Some(dims) => dims.iter().map(
|axis_raw| Vec3::from(*axis_raw)
).collect::<Vec<Vec3>>(),
None => return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData, format!("Not enough dimensions defined for machine! [Required: {}, Given: 0]", D)))
std::io::ErrorKind::InvalidData,
format!("Not enough dimensions defined for machine! [Required: {}, Given: 0]", dim )
))
};

mach.axes = match &self.axes {
Some(axes) => match axes.iter().map(|axis_raw| Vec3::from(*axis_raw)).collect::<Vec<Vec3>>().try_into() {
Ok(val) => val,
Err(_) => return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData, format!("Not enough axes defined for machine! [Required: {}, Given: {}]", A, axes.len())))
},
Some(axes) => axes.iter().map(
|axis_raw| Vec3::from(*axis_raw)
).collect::<Vec<Vec3>>(),
None => return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData, format!("Not enough axes defined for machine! [Required: {}, Given: 0]", A)))
std::io::ErrorKind::InvalidData,
format!("Not enough axes defined for machine! [Required: {}, Given: 0]", rot )
))
};

mach.tools = self.tools.iter().map(
Expand Down
32 changes: 16 additions & 16 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ pub mod conf;
pub mod websocket;
//

pub struct AppData<R : ActRobot<COMP, DECO, DIM, ROT>, const COMP : usize, const DECO : usize, const DIM : usize, const ROT : usize> {
pub struct AppData<R : ActRobot<C>, const C : usize> {
pub rob : Rc<RefCell<R>>,
pub intpr : Rc<RefCell<dyn Interpreter<R, Result<serde_json::Value, R::Error>>>>,

pub phis : [Phi; COMP],
pub phis : [Phi; C],
pub pos : Vec3
}

impl<R : ActRobot<COMP, DECO, DIM, ROT>, const COMP : usize, const DECO : usize, const DIM : usize, const ROT : usize> PushRemote<COMP>
for AppData<R, COMP, DECO, DIM, ROT>
impl<R : ActRobot<C>, const C : usize> PushRemote<C>
for AppData<R, C>
{
fn pub_phis(&mut self, phis : &[Phi; COMP]) -> Result<(), crate::Error> {
fn pub_phis(&mut self, phis : &[Phi; C]) -> Result<(), crate::Error> {
self.phis = *phis;

Ok(())
Expand All @@ -41,8 +41,8 @@ impl<R : ActRobot<COMP, DECO, DIM, ROT>, const COMP : usize, const DECO : usize,
{
use super::*;

pub async fn conf<R : ActRobot<COMP, DECO, DIM, ROT>, const COMP : usize, const DECO : usize, const DIM : usize, const ROT : usize>
(data_mutex : web::Data<Mutex<AppData<R, COMP, DECO, DIM, ROT>>>) -> impl Responder
pub async fn conf<R : ActRobot<C>, const C : usize>
(data_mutex : web::Data<Mutex<AppData<R, C>>>) -> impl Responder
{
let data = data_mutex.lock().unwrap();
let rob = data.rob.borrow();
Expand All @@ -51,23 +51,23 @@ impl<R : ActRobot<COMP, DECO, DIM, ROT>, const COMP : usize, const DECO : usize,
)
}

pub async fn pos<R : ActRobot<COMP, DECO, DIM, ROT>, const COMP : usize, const DECO : usize, const DIM : usize, const ROT : usize>
(_ : web::Data<Mutex<AppData<R, COMP, DECO, DIM, ROT>>>) -> impl Responder {
pub async fn pos<R : ActRobot<C>, const C : usize>
(_ : web::Data<Mutex<AppData<R, C>>>) -> impl Responder {
// let data = data_mutex.lock().unwrap();
let point = glam::Vec3::ZERO;
HttpResponse::Ok().content_type("application/json").body(serde_json::to_string_pretty(&point.to_array()).unwrap())
}
}

async fn intpr<R : ActRobot<COMP, DECO, DIM, ROT, Error = std::io::Error> + 'static, const COMP : usize, const DECO : usize, const DIM : usize, const ROT : usize>
(data_mutex : web::Data<Mutex<AppData<R, COMP, DECO, DIM, ROT>>>, req: HttpRequest, stream: web::Payload) -> impl Responder {
async fn intpr<R : ActRobot<C, Error = std::io::Error> + 'static, const C : usize>
(data_mutex : web::Data<Mutex<AppData<R, C>>>, req: HttpRequest, stream: web::Payload) -> impl Responder {
ws::start(websocket::WSHandler {
data: data_mutex.into_inner().clone()
}, &req, stream)
}
//

pub fn create_robot_webserver<R : SafeRobot<COMP, DECO, DIM, ROT, Error = std::io::Error> + 'static, T, const COMP : usize, const DECO : usize, const DIM : usize, const ROT : usize>
pub fn create_robot_webserver<R : SafeRobot<C, Error = std::io::Error> + 'static, T, const C : usize>
(rob : Rc<RefCell<R>>, intp : Rc<RefCell<dyn Interpreter<R, Result<serde_json::Value, R::Error>>>>, app : App<T>) -> App<T>
where
T : ServiceFactory<ServiceRequest, Config = (), Error = actix_web::Error, InitError = ()>
Expand All @@ -76,18 +76,18 @@ pub fn create_robot_webserver<R : SafeRobot<COMP, DECO, DIM, ROT, Error = std::i
rob: rob.clone(),
intpr: intp.clone(),

phis: [Phi::ZERO; COMP],
phis: [Phi::ZERO; C],
pos: Vec3::ZERO
}));

rob.borrow_mut().add_remote(Box::new(data.clone().into_inner()));

app.app_data(data)
.service(web::scope("/")
.route("/intpr", web::get().to(intpr::<R, COMP, DECO, DIM, ROT>))
.route("/intpr", web::get().to(intpr::<R, C>))
)
.service(web::scope("/api")
.route("/conf", web::get().to(api::conf::<R, COMP, DECO, DIM, ROT>))
.route("/pos", web::get().to(api::pos::<R, COMP, DECO, DIM, ROT>))
.route("/conf", web::get().to(api::conf::<R, C>))
.route("/pos", web::get().to(api::pos::<R, C>))
)
}
10 changes: 5 additions & 5 deletions src/http/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ use crate::ActRobot;

use super::AppData;

pub struct WSHandler<R : ActRobot<COMP, DECO, DIM, ROT>, const COMP : usize, const DECO : usize, const DIM : usize, const ROT : usize> {
pub data : Arc<Mutex<AppData<R, COMP, DECO, DIM, ROT>>>
pub struct WSHandler<R : ActRobot<C>, const C : usize> {
pub data : Arc<Mutex<AppData<R, C>>>
}

impl<R : ActRobot<COMP, DECO, DIM, ROT> + 'static, const COMP : usize, const DECO : usize, const DIM : usize, const ROT : usize> Actor for WSHandler<R, COMP, DECO, DIM, ROT> {
impl<R : ActRobot<C> + 'static, const C : usize> Actor for WSHandler<R, C> {
type Context = ws::WebsocketContext<Self>;
}

impl<R : ActRobot<COMP, DECO, DIM, ROT, Error = std::io::Error> + 'static, const COMP : usize, const DECO : usize, const DIM : usize, const ROT : usize>
StreamHandler<Result<ws::Message, ws::ProtocolError>> for WSHandler<R, COMP, DECO, DIM, ROT>
impl<R : ActRobot<C, Error = std::io::Error> + 'static, const C : usize>
StreamHandler<Result<ws::Message, ws::ProtocolError>> for WSHandler<R, C>
{
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
match msg {
Expand Down
5 changes: 5 additions & 0 deletions src/intpr.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
// Submodules
/// Helper functions for GCode processing
pub mod gcode;

/// Basic GCode applied for the `SafeRobot` trait
pub mod gfuncs;

/// Interpreters convert a string prompt into actions for the robot
pub trait Interpreter<ROB, RES> {
/// Interpret a code string for a given robot
fn interpret(&self, rob : &mut ROB, code : &str) -> Vec<RES>;

/// Interpret a file for a given robot
fn interpret_file(&self, rob : &mut ROB, path : &str) -> Vec<RES> {
self.interpret(rob, std::fs::read_to_string(path).unwrap().as_str())
}
Expand Down
34 changes: 17 additions & 17 deletions src/intpr/gcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,31 +127,31 @@ impl<ROB, RES> Interpreter<ROB, RES> for GCodeIntpr<ROB, RES> {
}
//

pub fn init_intpr<R : SafeRobot<COMP, DECO, DIM, ROT, Error = stepper_lib::Error>,
const COMP : usize, const DECO : usize, const DIM : usize, const ROT : usize>()
pub fn init_intpr<R : SafeRobot<C, Error = stepper_lib::Error>,
const C : usize>()
-> GCodeIntpr<R, Result<serde_json::Value, R::Error>>
{
let funcs = LetterEntries::from([
(Letter::General, NumEntries::from([
(0, gfuncs::g0::<R, COMP, DECO, DIM, ROT> as GCodeFunc<R, Result<serde_json::Value, R::Error>>),
(4, gfuncs::g4::<R, COMP, DECO, DIM, ROT>),
(8, gfuncs::g8::<R, COMP, DECO, DIM, ROT>),
(28, gfuncs::g28::<R, COMP, DECO, DIM, ROT>),
// (29, gfuncs::g29::<R, COMP, DECO, DIM, ROT>),
(100, gfuncs::g100::<R, COMP, DECO, DIM, ROT>),
(1000, gfuncs::g1000::<R, COMP, DECO, DIM, ROT>),
(1100, gfuncs::g1100::<R, COMP, DECO, DIM, ROT>)
(0, gfuncs::g0::<R, C> as GCodeFunc<R, Result<serde_json::Value, R::Error>>),
(4, gfuncs::g4::<R, C>),
(8, gfuncs::g8::<R, C>),
(28, gfuncs::g28::<R, C>),
// (29, gfuncs::g29::<R, C>),
(100, gfuncs::g100::<R, C>),
(1000, gfuncs::g1000::<R, C>),
(1100, gfuncs::g1100::<R, C>)
])),
(Letter::Miscellaneous, NumEntries::from([
(3, gfuncs::m3::<R, COMP, DECO, DIM, ROT> as GCodeFunc<R, Result<serde_json::Value, R::Error>>),
(4, gfuncs::m4::<R, COMP, DECO, DIM, ROT>),
(5, gfuncs::m5::<R, COMP, DECO, DIM, ROT>),
(30, gfuncs::m30::<R, COMP, DECO, DIM, ROT>),
(119, gfuncs::m119::<R, COMP, DECO, DIM, ROT>),
(1006, gfuncs::m1006::<R, COMP, DECO, DIM, ROT>),
(3, gfuncs::m3::<R, C> as GCodeFunc<R, Result<serde_json::Value, R::Error>>),
(4, gfuncs::m4::<R, C>),
(5, gfuncs::m5::<R, C>),
(30, gfuncs::m30::<R, C>),
(119, gfuncs::m119::<R, C>),
(1006, gfuncs::m1006::<R, C>),
])),
(Letter::ProgramNumber, NumEntries::from([
(0, gfuncs::o0::<R, COMP, DECO, DIM, ROT> as GCodeFunc<R, Result<serde_json::Value, R::Error>>)
(0, gfuncs::o0::<R, C> as GCodeFunc<R, Result<serde_json::Value, R::Error>>)
]))
]);

Expand Down
Loading

0 comments on commit 7c7f1e3

Please sign in to comment.