Added firmware

This commit is contained in:
Sebastian 2024-07-28 21:41:42 +02:00
parent f2fe686b0e
commit 4fb87dbae1
15 changed files with 2170 additions and 163 deletions

1103
daemon/Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

18
daemon/Cargo.toml Normal file
View file

@ -0,0 +1,18 @@
[package]
name = "radomctld"
version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = "1.0.83"
axum = { version = "0.7.5", features = ["macros"] }
fern = { version = "0.6.2", features = ["colored"] }
humantime = "2.1.0"
log = "0.4.21"
nom = "7.1.3"
serde_json = "1.0.118"
tokio = {version = "1.37.0", features = ["full"]}
tokio-macros = { version = "0.2.0-alpha.6" }
tower-http = { version = "0.5.2", features = ["fs", "trace"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"

12
daemon/assets/index.html Normal file
View file

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>radomctl</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
</body>
</html>

33
daemon/src/logger.rs Normal file
View file

@ -0,0 +1,33 @@
use anyhow::Result;
use fern::colors::{Color, ColoredLevelConfig};
use log::{debug, error, info, warn};
use std::io;
pub fn setup_logger() -> Result<()> {
let colors = ColoredLevelConfig::new()
.info(Color::Green)
.error(Color::Red)
.warn(Color::Yellow)
.debug(Color::Blue);
fern::Dispatch::new()
// Perform allocation-free log formatting
.format(move |out, message, record| {
out.finish(format_args!(
"[{} {} {}] {}",
humantime::format_rfc3339_millis(std::time::SystemTime::now()),
colors.color(record.level()),
record.target(),
message
))
})
// Add blanket level filter -
.level(log::LevelFilter::Debug)
// - and per-module overrides
.chain(std::io::stdout())
.chain(fern::log_file("output.log")?)
// Apply globally
.apply()?;
Ok(())
}

150
daemon/src/main.rs Normal file
View file

@ -0,0 +1,150 @@
mod logger;
mod rotctlprotocol;
mod rotor;
use anyhow::Result;
use fern::colors::{Color, ColoredLevelConfig};
use log::{debug, error, info, warn, Level};
use serde_json::{json, Value};
use std::{borrow::Borrow, io};
use tokio::{
self,
io::{AsyncBufReadExt, AsyncWriteExt, BufStream},
net::{TcpListener, TcpStream},
sync::{mpsc, watch},
task::JoinSet,
};
use axum::{
extract::State,
http::StatusCode,
routing::{get, post},
Json, Router,
};
use tower_http::{
services::{ServeDir, ServeFile},
trace::TraceLayer,
};
use logger::setup_logger;
use rotor::control_rotor;
use rotctlprotocol::{parse_command, Command};
async fn process_socket(
socket: TcpStream,
cmd_tx: mpsc::Sender<Command>,
mut pos_rx: watch::Receiver<(f32, f32)>,
) {
let mut stream = BufStream::new(socket);
let mut line = String::new();
loop {
if let Ok(n) = stream.read_line(&mut line).await {
if n == 0 {
return;
}
debug!("Received: {}", line.strip_suffix("\n").unwrap());
match parse_command(&line) {
Ok(cmd) => match cmd {
Command::GetPos => {
let (az, el) = pos_rx.borrow().clone();
stream
.write_all(format!("{}\n{}\n", az, el).as_bytes())
.await
.unwrap();
stream.flush().await.unwrap();
}
Command::Exit => {
stream.write_all("RPRT 0\n".as_bytes()).await.unwrap();
stream.flush().await.unwrap();
return;
}
cmd => {
cmd_tx.send(cmd).await.unwrap();
stream.write_all("RPRT 0\n".as_bytes()).await.unwrap();
stream.flush().await.unwrap();
}
},
Err(msg) => {
error!("Unable to parse input:\n{}", msg);
stream.write_all("RPRT 6\n".as_bytes()).await.unwrap();
stream.flush().await.unwrap();
}
}
line.clear();
} else {
return;
}
}
}
#[derive(Clone)]
struct AxumAppState {
pos_rx: watch::Receiver<(f32, f32)>,
}
#[tokio::main]
async fn main() -> Result<()> {
setup_logger()?;
let (cmd_tx, cmd_rx) = mpsc::channel::<Command>(16);
let (pos_tx, pos_rx) = watch::channel::<(f32, f32)>((0.0, 0.0));
let mut tasks = JoinSet::new();
tasks.spawn(async move { control_rotor(cmd_rx, pos_tx).await });
let state = AxumAppState {
pos_rx: pos_rx.clone(),
};
tasks.spawn(async move {
let app = Router::new()
.route_service("/", ServeFile::new("assets/index.html"))
.route("/state", get(get_state))
.with_state(state)
.layer(TraceLayer::new_for_http());
let listener = tokio::net::TcpListener::bind("0.0.0.0:8000").await?;
axum::serve(listener, app).await?;
Ok(())
});
tasks.spawn(async move {
let listener = TcpListener::bind("127.0.0.1:1337").await?;
loop {
let (socket, _) = listener.accept().await?;
let cmd_tx = cmd_tx.clone();
let pos_rx = pos_rx.clone();
tokio::spawn(async move {
process_socket(socket, cmd_tx, pos_rx).await;
});
}
});
while let Some(res) = tasks.join_next().await {
res.unwrap();
}
Ok(())
}
async fn get_state(State(state): State<AxumAppState>) -> Json<Value> {
let (az, el) = state.pos_rx.borrow().clone();
Json(json!({
"position" : {
"az": az,
"el": el
}
}))
}

View file

@ -0,0 +1,328 @@
use nom::{
branch::alt,
bytes::complete::tag,
character::complete::{
alphanumeric1, i8, multispace0, multispace1, newline, none_of, not_line_ending, space1,
},
combinator::{all_consuming, map, opt, recognize, rest},
error::{context, convert_error, VerboseError},
multi::{many0, many1},
number::complete::float,
sequence::{preceded, separated_pair, terminated},
Err as NomErr, IResult, Parser,
};
#[derive(PartialEq, Debug)]
pub enum Command {
Exit,
SetPos(f32, f32),
GetPos,
Move(Direction, i8),
Stop,
Park,
SetConf(String, String),
Reset,
GetInfo,
DumpState,
DumpCaps,
SendCmd(String),
}
#[derive(PartialEq, Debug)]
pub enum Direction {
UP,
DOWN,
CW,
CCW,
}
fn exit(input: &str) -> IResult<&str, Command, VerboseError<&str>> {
map(alt((tag("q"), tag("Q"))), |_| Command::Exit).parse(input)
}
fn float_pair(input: &str) -> IResult<&str, (f32, f32), VerboseError<&str>> {
context("float_pair", separated_pair(float, multispace1, float)).parse(input)
}
fn set_pos(input: &str) -> IResult<&str, Command, VerboseError<&str>> {
context(
"set_pos",
map(
preceded(
terminated(alt((tag("set_pos"), tag("P"))), multispace1),
float_pair,
),
|pair: (f32, f32)| Command::SetPos(pair.0, pair.1),
),
)
.parse(input)
}
fn get_pos(input: &str) -> IResult<&str, Command, VerboseError<&str>> {
map(alt((tag("get_pos"), tag("p"))), |_| Command::GetPos).parse(input)
}
fn direction(input: &str) -> IResult<&str, Direction, VerboseError<&str>> {
context(
"direction",
alt((
map(alt((tag("UP"), tag("2"))), |_| Direction::UP),
map(alt((tag("DOWN"), tag("4"))), |_| Direction::DOWN),
map(alt((tag("CCW"), tag("LEFT"), tag("8"))), |_| Direction::CCW),
map(alt((tag("CW"), tag("RIGHT"), tag("16"))), |_| Direction::CW),
)),
)
.parse(input)
}
fn move_parameters(input: &str) -> IResult<&str, (Direction, i8), VerboseError<&str>> {
context(
"move_parameters",
separated_pair(direction, multispace1, i8),
)
.parse(input)
}
fn move_command(input: &str) -> IResult<&str, Command, VerboseError<&str>> {
context(
"move",
map(
preceded(
terminated(alt((tag("move"), tag("M"))), multispace1),
move_parameters,
),
|pair: (Direction, i8)| Command::Move(pair.0, pair.1),
),
)
.parse(input)
}
fn stop(input: &str) -> IResult<&str, Command, VerboseError<&str>> {
map(alt((tag("stop"), tag("S"))), |_| Command::Stop).parse(input)
}
fn park(input: &str) -> IResult<&str, Command, VerboseError<&str>> {
map(alt((tag("park"), tag("K"))), |_| Command::Park).parse(input)
}
fn string_pair(input: &str) -> IResult<&str, (&str, &str), VerboseError<&str>> {
// TODO: find out if alphanumeric1 is enough. Might need -_ and others
context(
"string_pair",
separated_pair(alphanumeric1, multispace1, alphanumeric1),
)
.parse(input)
}
fn set_conf(input: &str) -> IResult<&str, Command, VerboseError<&str>> {
context(
"set_comf",
map(
preceded(
terminated(alt((tag("set_conf"), tag("C"))), multispace1),
string_pair,
),
|pair: (&str, &str)| Command::SetConf(pair.0.to_owned(), pair.1.to_owned()),
),
)
.parse(input)
}
fn reset(input: &str) -> IResult<&str, Command, VerboseError<&str>> {
map(alt((tag("reset"), tag("R"))), |_| Command::Reset).parse(input)
}
fn get_info(input: &str) -> IResult<&str, Command, VerboseError<&str>> {
map(alt((tag("get_info"), tag("_"))), |_| Command::GetInfo).parse(input)
}
fn dump_state(input: &str) -> IResult<&str, Command, VerboseError<&str>> {
map(tag("dump_state"), |_| Command::DumpState).parse(input)
}
fn dump_caps(input: &str) -> IResult<&str, Command, VerboseError<&str>> {
map(alt((tag("dump_caps"), tag("1"))), |_| Command::DumpCaps).parse(input)
}
fn send_cmd(input: &str) -> IResult<&str, Command, VerboseError<&str>> {
context(
"send_cmd",
map(
preceded(
terminated(alt((tag("send_cmd"), tag("w"))), space1),
many1(none_of("#\n")),
),
|rest: Vec<char>| Command::SendCmd(rest.iter().collect()),
),
)
.parse(input)
}
fn command(input: &str) -> IResult<&str, Command, VerboseError<&str>> {
context(
"command",
alt((
exit,
set_pos,
park,
get_pos,
move_command,
stop,
set_conf,
reset,
get_info,
dump_state,
dump_caps,
send_cmd,
)),
)
.parse(input)
}
fn comment(input: &str) -> IResult<&str, &str, VerboseError<&str>> {
context(
"comment",
recognize(preceded(multispace0, preceded(tag("#"), not_line_ending))),
)
.parse(input)
}
fn line(input: &str) -> IResult<&str, Command, VerboseError<&str>> {
context(
"line",
all_consuming(terminated(terminated(command, opt(comment)), newline)),
)
.parse(input)
}
pub fn parse_command(input: &str) -> Result<Command, String> {
let result = line(input);
match result {
Ok(("", cmd)) => Ok(cmd),
Ok((rest, _)) => Err("Unable to parse rest".to_owned()),
Err(err) => match err {
NomErr::Incomplete(_) => Err("Command was incomplete".to_owned()),
NomErr::Error(err) | NomErr::Failure(err) => Err(convert_error(input, err)),
},
}
}
#[cfg(test)]
mod tests {
use super::*;
use nom::Err as NomErr;
fn assert_command(input: &str, expected: Command) {
let result = line(input);
match result {
Ok(("", cmd)) => assert_eq!(cmd, expected),
Err(err) => match err {
NomErr::Incomplete(_) => panic!("Command was incomplete"),
NomErr::Error(err) | NomErr::Failure(err) => {
panic!("{}", convert_error(input, err))
}
},
_ => {}
}
}
#[test]
fn test_exit() {
assert_command("Q\n", Command::Exit);
assert_command("q\n", Command::Exit);
}
#[test]
fn test_set_pos() {
assert_command("set_pos 180.0 10.0\n", Command::SetPos(180.0, 10.0));
assert_command("P 180.0 10.0\n", Command::SetPos(180.0, 10.0));
}
#[test]
fn test_get_pos() {
assert_command("get_pos\n", Command::GetPos);
assert_command("p\n", Command::GetPos);
}
#[test]
fn test_move() {
assert_command("M UP 100\n", Command::Move(Direction::UP, 100));
assert_command("M DOWN -1\n", Command::Move(Direction::DOWN, -1));
assert_command("M CCW 42\n", Command::Move(Direction::CCW, 42));
assert_command("M LEFT 42\n", Command::Move(Direction::CCW, 42));
assert_command("M CW 42\n", Command::Move(Direction::CW, 42));
assert_command("M RIGHT 42\n", Command::Move(Direction::CW, 42));
assert_command("move UP 100\n", Command::Move(Direction::UP, 100));
}
#[test]
fn test_stop() {
assert_command("stop\n", Command::Stop);
assert_command("S\n", Command::Stop);
}
#[test]
fn test_park() {
assert_command("park\n", Command::Park);
assert_command("K\n", Command::Park);
}
#[test]
fn test_reset() {
assert_command("reset\n", Command::Reset);
assert_command("R\n", Command::Reset);
}
#[test]
fn test_get_info() {
assert_command("get_info\n", Command::GetInfo);
assert_command("_\n", Command::GetInfo);
}
#[test]
fn test_dump_state() {
assert_command("dump_state\n", Command::DumpState);
}
#[test]
fn test_dump_caps() {
assert_command("dump_caps\n", Command::DumpCaps);
assert_command("1\n", Command::DumpCaps);
}
#[test]
fn test_set_conf() {
assert_command(
"set_conf foo bar\n",
Command::SetConf("foo".to_owned(), "bar".to_owned()),
);
assert_command(
"C foo bar\n",
Command::SetConf("foo".to_owned(), "bar".to_owned()),
);
}
#[test]
fn test_send_cmd() {
assert_command("send_cmd foo bar\n", Command::SendCmd("foo bar".to_owned()));
assert_command("w foo bar\n", Command::SendCmd("foo bar".to_owned()));
}
#[test]
fn test_comments() {
assert_command(
"set_pos 180.0 10.0 # fooo bar lol test\n",
Command::SetPos(180.0, 10.0),
);
assert_command(
"P 180.0 10.0 # fooo bar lol test\n",
Command::SetPos(180.0, 10.0),
);
// TODO: figure out if the trailing space is a problem
assert_command(
"send_cmd foo bar # this is a comment\n",
Command::SendCmd("foo bar ".to_owned()),
);
}
}

53
daemon/src/rotor.rs Normal file
View file

@ -0,0 +1,53 @@
use anyhow::Result;
use log::{debug, error, info, warn};
use tokio::{
self,
io::{AsyncBufReadExt, AsyncWriteExt, BufStream},
net::{TcpListener, TcpStream},
sync::{self, mpsc, watch},
time,
};
use crate::rotctlprotocol::{parse_command, Command};
pub async fn control_rotor(
mut rx_cmd: mpsc::Receiver<Command>,
pos_tx: watch::Sender<(f32, f32)>,
) -> Result<()> {
let mut actual_az = 0.0;
let mut actual_el = 0.0;
let mut target_az = 0.0;
let mut target_el = 0.0;
loop {
tokio::select! {
Some(command) = rx_cmd.recv() => {
match command {
Command::SetPos(az, el) => {
info!("Received set pos {} {}", az, el);
target_az = az;
target_el = el;
}
_ => {}
}
},
_ = time::sleep(time::Duration::from_millis(100)) => {
if target_az < actual_az {
actual_az -= 1.0;
} else if target_az > actual_az {
actual_az += 1.0;
}
if target_el < actual_el {
actual_el -= 1.0;
} else if target_el > actual_el {
actual_el += 1.0;
}
pos_tx.send((actual_az, actual_el)).unwrap();
},
else => return Ok(())
};
}
}