SamuelNoesslboeck / syact

A library to control motors (mainly stepper motors), define actuators and their interactions with each other
https://crates.io/crates/syact
MIT License
8 stars 1 forks source link

How does one choose acceleration. #27

Open MiniMinerX opened 2 months ago

MiniMinerX commented 2 months ago

For 13.7, I got no clue how to control the acceleration of my motor. Its like kind of based on intertia, but i dont know why we don't have some simple ramp making functions. I can determine the distance, but having the velocity be a factor of maximum velocity is wierd. I like the idea of checking forces and loads for maximum motion allowed, but it makes accelerations and specifying an actual velocity almost impossible. I plan on inlcuding these motors with bevy. use core::f32::consts::PI;

use rppal::gpio::Gpio; // Include the library use syact::prelude::*;

// Pin declerations (BCM on raspberry pi) const GPIO_DIR_PIN : u8 = 27; const GPIO_STEP_PIN : u8 = 19;

// Define distance and max speed const DELTA : Delta = Delta( 1.0);

pub const MOT_23HS45_4204S: syact::StepperConst = StepperConst { default_current: 3.8, inductance: 0.0034, resistance: 0.88, number_steps: 400, torque_stall: Force(3.0), // This unit is Nm inertia_motor: Inertia(0.000_068) };

fn main() -> Result<(), syact::Error> {

let step_pin = Gpio::new()?.get(GPIO_STEP_PIN)?.into_output();
let dir_pin = Gpio::new()?.get(GPIO_DIR_PIN)?.into_output();

let ctrl = GenericPWM::new(step_pin, dir_pin)?;

let mut stepper = ComplexStepper::new(ctrl, MOT_23HS45_4204S)?;

let mut gear = Gear::new(stepper, 0.05);

let stepper_config: StepperConfig = StepperConfig {
    voltage: 24.0,
    overload_current: None,
};

gear.set_config(stepper_config);

//stepper.setup();

let max_vel = Velocity(12.0 * 20.0 * 2.0 * std::f32::consts::PI);
//radians per second
// if at 2pi, then max speed is 1 revolution of the motor per second
// if at 20 * 2pi, then max speed is at 1 revolution of the gearbox per second,
// which is 50mm diameter, so 5cm per second,
// lets use 60 cm / per second max, ~2ft / sec, which divided by 5cm sections is
// 60/5 = 12, so 12 * 20 * 2pi

//stepper.set_velocity_max(max_vel);
//stepper.set_microsteps(MicroSteps::from(1));

// Apply some loads
gear.apply_inertia(Inertia(1.0));
gear.apply_gen_force(Force(100.0));

println!("Staring to move");
gear.drive_rel(DELTA, Factor::new(1.0));      // Move the motor
println!("Distance {}rad with max speed {:?}rad/s done", DELTA, max_vel);

Ok(())

}

SamuelNoesslboeck commented 1 month ago

Hi there!

First thanks for giving the library a try and for your comment, sorry for answering this late too, Github gave me no notification for some reason.

Yeah you are right! The acceleration is based on a number of factors, calculated by the loads you define, and you are also right that in the current version it is not possible to define a maximum acceleration.

This library was kind of a school project of mine, I loved to do some calculations around stepper motors and tried to build a library that could do all of that stuff.

I haven't been working on it for months now (as I graduated earlier this year), but decided to come back to it and actually document it as good as possible and get all the issues out of the way. Currently working on 0.14.0 with massive improvments to async behaivor and I will try to implement your desired feature asap, as it stands on my todo list too.

Again thanks for actually giving it a try, have a nice day!