Initial commit

main
Joey Hines 2024-05-26 15:44:41 -06:00
commit c34d5dfa9a
Signed by: joeyahines
GPG Key ID: 995E531F7A569DDB
7 changed files with 1233 additions and 0 deletions

View File

@ -0,0 +1,2 @@
[registries.jojo-dev]
index = "https://git.jojodev.com/joeyahines/_cargo-index.git"

2
.gitignore vendored 100644
View File

@ -0,0 +1,2 @@
/target
/.idea

1113
Cargo.lock generated 100644

File diff suppressed because it is too large Load Diff

17
Cargo.toml 100644
View File

@ -0,0 +1,17 @@
[package]
name = "roll_bot"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
structopt = "0.3.26"
config = "0.14.0"
rppal = "0.18.0"
raas_types = { version = "0.0.1", registry = "jojo-dev"}
log = "0.4.21"
env_logger = "0.11.3"
thiserror = "1.0.61"

9
Cross.toml 100644
View File

@ -0,0 +1,9 @@
[build]
pre-build = [
"apt-get --assume-yes install curl unzip",
"curl -LO https://github.com/protocolbuffers/protobuf/releases/download/v25.1/protoc-25.1-linux-x86_64.zip",
"unzip protoc-25.1-linux-x86_64.zip -d /opt/protoc",
]
[build.env]
passthrough = ["PROTOC=/opt/protoc/bin/protoc"]

43
src/main.rs 100644
View File

@ -0,0 +1,43 @@
mod roll_bot;
use structopt::StructOpt;
use std::error::Error;
use std::time::Duration;
use log::info;
use crate::roll_bot::RollBot;
use rppal::pwm::{Channel, Polarity, Pwm};
const PERIOD_MS: u64 = 20;
const PULSE_MIN_US: u64 = 1000;
const PULSE_NEUTRAL_US: u64 = 1500;
const PULSE_MAX_US: u64 = 2000;
fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();
info!("Initializing PWM");
let pwm = Pwm::with_period(
Channel::Pwm0,
Duration::from_millis(PERIOD_MS),
Duration::from_micros(PULSE_MAX_US),
Polarity::Normal,
true,
)?;
info!("Initializing Roll Bot");
let roll_bot = RollBot {
pwm,
move_time: Duration::from_millis(1000),
};
roll_bot.set_pwm_output(Duration::from_micros(PULSE_MIN_US))?;
loop {
roll_bot.roll(5)?;
std::thread::sleep(Duration::from_millis(1000));
}
Ok(())
}

47
src/roll_bot.rs 100644
View File

@ -0,0 +1,47 @@
use crate::{PULSE_MAX_US, PULSE_MIN_US, PULSE_NEUTRAL_US};
use log::info;
use rppal::pwm::Pwm;
use std::thread;
use std::time::Duration;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
#[error("PWM error")]
PWMError(#[from] rppal::pwm::Error),
}
pub struct RollBot {
pub pwm: Pwm,
pub move_time: Duration,
}
impl RollBot {
pub fn set_pwm_output(&self, pulse: Duration) -> Result<(), Error> {
info!("Going to {:?}", pulse);
self.pwm
.set_pulse_width(pulse)?;
Ok(())
}
pub fn roll(&self, tumble_times: u32) -> Result<(), Error> {
for tumble in 1..=tumble_times {
info!("Doing Tumble {}", tumble);
for step in (PULSE_MIN_US..=PULSE_MAX_US).step_by(1000) {
self.set_pwm_output(Duration::from_micros(step))?;
thread::sleep(self.move_time);
}
for step in (PULSE_MIN_US..=PULSE_MAX_US).rev().step_by(1000) {
self.set_pwm_output(Duration::from_micros(step))?;
thread::sleep(self.move_time);
}
}
info!("Roll complete!");
Ok(())
}
}