Open XingYunFei580 opened 4 months ago
Thanks for the suggestion. Linear motion I find is a subset of geared drives, where the move amount is multiplied by some factor. I had been considering adding the geared motor functionality separate from the rest but I am still unconvinced just to avoid a multiplication with move().
For the linear reciprocating motion of the lead screw nut driven by the motor, according to the expected linear displacement (s) and the known screw lead (d), we can add a public function "goback(double s, int d)" in the head file to achieve the goal more simply and intuitively. Of course, other necessary data, such as the motor speed and pulse number, have been completed through other settings. in the file "BasicStepperDriver.h", add: class BasicStepperDriver {
Public
void goback(double s, int d); long calcStepsForGoBack(double s, int d) { return s/d motor_steps microsteps; }
};
in the file"BasicStepperDriver.cpp",add:
void BasicStepperDriver::goback(double s, int d){ move(calcStepsForGoBack(s,d)); }
An example program as below:
define MOTOR_STEPS 300
define RPM 200
define DIR_PIN 7
define STEP_PIN 8
define SLEEP 9
define MICROSTEPS 1
BasicStepperDriver stepper(MOTOR_STEPS, DIR_PIN, STEP_PIN, SLEEP)
int d=5; //screw lead 5mm double s=30.4; //nut moving distance 30.4mm
void setup( ) { stepper.begin(RPM, MICROSTEPS); }
void loop( ){
stepper.goback(s, d); delay(100); stepper.goback(-s, d); delay(100); }