Browse Source

update

master
zhaohe 2 years ago
parent
commit
1b8ed02b9d
  1. 378
      components/step_motor_ctrl_module/step_motor_ctrl_module.cpp
  2. 36
      components/step_motor_ctrl_module/step_motor_ctrl_module.hpp
  3. 165
      components/step_motor_ctrl_module/step_motor_speed_ctrl_module.cpp
  4. 44
      components/step_motor_ctrl_module/step_motor_speed_ctrl_module.hpp
  5. 2
      components/zprotocols/zcancmder

378
components/step_motor_ctrl_module/step_motor_ctrl_module.cpp

@ -14,234 +14,265 @@ void StepMotorCtrlModule::initialize(int id, IStepperMotor* stepM, ZGPIO* zero_g
m_statu_lock.init(); m_statu_lock.init();
m_thread.init("XYRobotCtrlModule", 1024, osPriorityNormal); m_thread.init("XYRobotCtrlModule", 1024, osPriorityNormal);
} }
int32_t StepMotorCtrlModule::move_to(int32_t tox, function<void(move_to_cb_status_t& status)> status_cb) { //
int32_t StepMotorCtrlModule::set_acc(int32_t acc) {
zlock_guard l(m_lock);
ZLOGI(TAG, "m%d set_acc %d", m_id, acc);
m_cfg_acc = acc;
return 0;
}
int32_t StepMotorCtrlModule::set_dec(int32_t dec) {
zlock_guard l(m_lock);
ZLOGI(TAG, "m%d set_dec %d", m_id, dec);
m_cfg_dec = dec;
return 0;
}
int32_t StepMotorCtrlModule::set_break_dec(int32_t dec) {
zlock_guard l(m_lock);
ZLOGI(TAG, "m%d set_break_dec %d", m_id, dec);
m_cfg_break_dec = dec;
return 0;
}
int32_t StepMotorCtrlModule::set_speed(int32_t speed) {
zlock_guard l(m_lock);
ZLOGI(TAG, "m%d set_speed %d", m_id, speed);
m_cfg_velocity = speed;
return 0;
}
#if 0
int32_t StepMotorCtrlModule::set_zero_robottype(RobotType_t robot_type) {
zlock_guard l(m_lock);
ZLOGI(TAG, "m%d set_zero_robottype %d", m_id, robot_type);
m_robot_type = robot_type;
return 0;
}
#endif
int32_t StepMotorCtrlModule::set_shaft(bool shaft) {
zlock_guard l(m_lock);
ZLOGI(TAG, "m%d set_shaft %d", m_id, shaft);
m_x_shaft = shaft;
m_stepM1->setMotorShaft(shaft);
return 0;
}
int32_t StepMotorCtrlModule::set_zero_shift(int32_t shift) {
zlock_guard l(m_lock);
ZLOGI(TAG, "m%d set_zero_shift %d", m_id, shift);
m_zero_shift_x = shift;
return 0;
}
int32_t StepMotorCtrlModule::set_runtozero_max_distance(int32_t distance) {
zlock_guard l(m_lock);
ZLOGI(TAG, "m%d set_runtozero_max_distance %d", m_id, distance);
m_cfg_runtozero_maxX = distance;
return 0;
}
int32_t StepMotorCtrlModule::set_runtozero_speed(int32_t speed) {
zlock_guard l(m_lock);
ZLOGI(TAG, "m%d set_runtozero_speed %d", m_id, speed);
m_cfg_runtozero_speed = speed;
return 0;
}
int32_t StepMotorCtrlModule::set_runtozero_dec(int32_t dec) {
zlock_guard l(m_lock);
ZLOGI(TAG, "m%d set_runtozero_dec %d", m_id, dec);
m_cfg_runtozero_dec = dec;
return 0;
}
int32_t StepMotorCtrlModule::set_runtozero_leave_away_zero_distance(int32_t distance) {
zlock_guard l(m_lock);
ZLOGI(TAG, "m%d set_runtozero_leave_away_zero_distance %d", m_id, distance);
m_cfg_runtozero_leave_away_zero_max_distance = distance;
return 0;
}
int32_t StepMotorCtrlModule::set_distance_scale(float distance_scale) {
zlock_guard l(m_lock);
ZLOGI(TAG, "m%d set_distance_scale %f", m_id, distance_scale);
m_cfg_distance_scale = distance_scale;
m_stepM1->setScale(distance_scale);
return 0;
}
int32_t StepMotorCtrlModule::set_current_pos(int32_t xpos) {
zlock_guard l(m_lock);
ZLOGI(TAG, "m%d set_current_pos %d", m_id, xpos);
int32_t motor_pos = 0;
forward_kinematics(xpos, motor_pos);
m_stepM1->setXACTUAL(motor_pos);
return 0;
}
int32_t StepMotorCtrlModule::set_ihold_irun_iholddelay(uint8_t ihold, uint8_t irun, uint16_t iholddelay) {
zlock_guard l(m_lock);
ZLOGI(TAG, "m%d set_ihold_irun_iholddelay %d %d %d", m_id, ihold, irun, iholddelay);
m_stepM1->setIHOLD_IRUN(ihold, irun, iholddelay);
return 0;
}
void StepMotorCtrlModule::dumpcfg(const char* tag) {}
int32_t StepMotorCtrlModule::enable(bool venable) {
zlock_guard l(m_lock);
ZLOGI(TAG, "m%d enable %d", m_id, venable);
m_stepM1->enable(venable);
return 0;
}
int32_t StepMotorCtrlModule::stop() {
zlock_guard l(m_lock);
ZLOGI(TAG, "m%d stop", m_id);
m_thread.stop();
m_stepM1->stop();
return 0;
}
int32_t StepMotorCtrlModule::brake() {
zlock_guard l(m_lock);
ZLOGI(TAG, "m%d brake", m_id);
m_stepM1->setDeceleration(m_cfg_break_dec);
m_stepM1->stop();
m_thread.stop();
m_stepM1->stop();
return 0;
}
/*******************************************************************************
* ACTION *
*******************************************************************************/
int32_t StepMotorCtrlModule::move_to(int32_t xnow, function<void(int32_t exec_status, int32_t toxnow)> status_cb) {
zlock_guard lock(m_lock); zlock_guard lock(m_lock);
ZLOGI(TAG, "m%d move_to %d", m_id, xnow);
ZLOGI(TAG, "m%d move_to %d", m_id, tox);
m_thread.stop(); m_thread.stop();
updateStatus(1); updateStatus(1);
m_thread.start([this, xnow, status_cb]() {
_motor_move_to(xnow, m_cfg_velocity, m_cfg_acc, m_cfg_dec);
m_thread.start([this, tox, status_cb]() {
_motor_move_to(tox, m_cfg_velocity, m_cfg_acc, m_cfg_dec);
while (!_motor_is_reach_target()) { while (!_motor_is_reach_target()) {
if (m_thread.getExitFlag()) break; if (m_thread.getExitFlag()) break;
vTaskDelay(10); vTaskDelay(10);
} }
_motor_stop(); _motor_stop();
int32_t xnow = 0;
getnowpos(xnow);
if (status_cb) status_cb(0, xnow);
int32_t nowx = 0;
getnowpos(nowx);
move_to_cb_status_t status;
status.exec_status = 0;
status.tox = nowx;
if (status_cb) status_cb(status);
updateStatus(0); updateStatus(0);
}); });
return 0; return 0;
} }
int32_t StepMotorCtrlModule::move_by(int32_t dpos, function<void(int32_t exec_status, int32_t topos)> status_cb) { //
int32_t StepMotorCtrlModule::move_by(int32_t dx, function<void(move_by_cb_status_t& status)> status_cb) {
zlock_guard lock(m_lock); zlock_guard lock(m_lock);
ZLOGI(TAG, "m%d move_by %d", m_id, dpos);
ZLOGI(TAG, "m%d move_by %d", m_id, dx);
m_thread.stop(); m_thread.stop();
int32_t xnow = 0; int32_t xnow = 0;
getnowpos(xnow); getnowpos(xnow);
int32_t toxnow = xnow + dpos;
move_to(toxnow, status_cb);
int32_t toxnow = xnow + dx;
move_to(toxnow, [this, status_cb, xnow](move_to_cb_status_t& status) {
move_by_cb_status_t movebycb_status;
movebycb_status.exec_status = status.exec_status;
movebycb_status.dx = status.tox - xnow;
if (status_cb) status_cb(movebycb_status);
});
return 0; return 0;
} }
int32_t StepMotorCtrlModule::move_to_zero(function<void(int32_t exec_status, int32_t dpos)> status_cb) {
int32_t StepMotorCtrlModule::move_to_zero(function<void(move_to_zero_cb_status_t& status)> status_cb) {
zlock_guard lock(m_lock); zlock_guard lock(m_lock);
ZLOGI(TAG, "m%d move_to_zero", m_id);
ZLOGI(TAG, "move_to_zero");
m_thread.stop(); m_thread.stop();
updateStatus(1);
m_thread.start([this, status_cb]() {
int32_t dx;
move_to_zero_cb_status_t ret_status = {0};
int32_t erro = exec_move_to_zero_task(dx);
if (erro != 0) {
ZLOGI(TAG, "exec_move_to_zero_task failed:%d", erro);
_motor_stop();
ret_status.exec_status = erro;
if (status_cb) status_cb(ret_status);
updateStatus(0);
return;
}
m_stepM1->setXACTUAL(0);
ret_status.exec_status = 0;
if (status_cb) status_cb(ret_status);
updateStatus(0);
return;
});
return 0; return 0;
} }
int32_t StepMotorCtrlModule::move_to_zero_with_calibrate(int32_t nowx, function<void(int32_t exec_status, int32_t zero_shift_pos)> status_cb) {
int32_t StepMotorCtrlModule::move_to_zero_with_calibrate(int32_t nowx, function<void(move_to_zero_with_calibrate_cb_status_t& status)> status_cb) {
zlock_guard lock(m_lock); zlock_guard lock(m_lock);
ZLOGI(TAG, "move_to_zero_with_calibrate x:%d", nowx); ZLOGI(TAG, "move_to_zero_with_calibrate x:%d", nowx);
m_thread.stop(); m_thread.stop();
updateStatus(1);
updateStatus(1);
m_thread.start([this, nowx, status_cb]() { m_thread.start([this, nowx, status_cb]() {
int32_t dx;
int32_t dx;
move_to_zero_with_calibrate_cb_status_t ret_status = {0};
int32_t erro = exec_move_to_zero_task(dx); int32_t erro = exec_move_to_zero_task(dx);
if (erro != 0) { if (erro != 0) {
ZLOGI(TAG, "exec_move_to_zero_task failed:%d", erro); ZLOGI(TAG, "exec_move_to_zero_task failed:%d", erro);
_motor_stop(); _motor_stop();
if (status_cb) status_cb(erro, 0);
ret_status.exec_status = erro;
if (status_cb) status_cb(ret_status);
updateStatus(0); updateStatus(0);
return; return;
} }
int32_t calibrate_x, calibrate_y; int32_t calibrate_x, calibrate_y;
calibrate_x = dx + nowx;
calibrate_x = dx + nowx;
m_zero_shift_x = calibrate_x; m_zero_shift_x = calibrate_x;
m_stepM1->setXACTUAL(0); m_stepM1->setXACTUAL(0);
if (status_cb) status_cb(erro, m_zero_shift_x);
ret_status.exec_status = 0;
ret_status.zero_shift_x = m_zero_shift_x;
if (status_cb) status_cb(ret_status);
updateStatus(0); updateStatus(0);
return; return;
}); });
return 0; return 0;
} }
// int32_t StepMotorCtrlModule::rotate(uint8_t direction, int32_t velocity, int32_t acc) { return 0; }
int32_t StepMotorCtrlModule::enable(bool venable) {
zlock_guard l(m_lock);
ZLOGI(TAG, "m%d enable %d", m_id, venable);
m_stepM1->enable(venable);
return 0;
}
int32_t StepMotorCtrlModule::stop(uint8_t stopType) {
zlock_guard l(m_lock);
ZLOGI(TAG, "m%d stop", m_id);
m_thread.stop();
m_stepM1->stop();
return 0;
}
int32_t StepMotorCtrlModule::force_change_current_pos(int32_t xpos) {
zlock_guard l(m_lock);
ZLOGI(TAG, "m%d force_change_current_pos %d", m_id, xpos);
int32_t motor_pos = 0;
forward_kinematics(xpos, motor_pos);
m_stepM1->setXACTUAL(motor_pos);
return 0;
}
int32_t StepMotorCtrlModule::rotate(int32_t speed, int32_t lastforms, function<void(rotate_cb_status_t& status)> status_cb) { //
zlock_guard l(m_lock);
ZLOGI(TAG, "rotate speed:%d lastforms:%d acc:%d dec:%d", speed, lastforms, m_cfg_acc, m_cfg_dec);
/*******************************************************************************
* READ *
*******************************************************************************/
if (lastforms < 0) {
ZLOGW(TAG, "lastforms:%d < 0", lastforms);
return err::kcommon_error_param_out_of_range;
}
int32_t StepMotorCtrlModule::read_status(uint8_t* module_statu, int32_t* pos, int32_t* velocity) {
zlock_guard lock(m_statu_lock);
*module_statu = m_status;
getnowpos(*pos);
*velocity = m_stepM1->getVACTUAL();
if (abs(speed) > m_cfg_max_speed) {
ZLOGW(TAG, "speed:%d > m_cfg_max_speed:%d", speed, m_cfg_max_speed);
speed = m_cfg_max_speed;
}
m_thread.stop();
updateStatus(1);
m_thread.start([this, lastforms, speed, status_cb]() {
rotate_cb_status_t status_report;
m_stepM1->setAcceleration(m_cfg_acc);
m_stepM1->setDeceleration(m_cfg_dec);
m_stepM1->rotate(speed);
int32_t startticket = zos_get_tick();
bool reachtime = false;
while (!m_thread.getExitFlag()) {
if (zos_haspassedms(startticket) > lastforms || lastforms == 0) {
reachtime = true;
m_stepM1->stop();
break;
}
osDelay(1);
}
status_report.exec_status = 0;
status_report.lastforms = zos_haspassedms(startticket);
if (status_cb) status_cb(status_report);
m_stepM1->stop();
updateStatus(0);
return;
});
return 0; return 0;
} }
int32_t StepMotorCtrlModule::get_zero_shift(int32_t* pos) {
zlock_guard lock(m_lock);
*pos = m_zero_shift_x;
int32_t StepMotorCtrlModule::read_version(version_t& version) { return 0; }
int32_t StepMotorCtrlModule::read_status(status_t& status) {
zlock_guard l(m_statu_lock);
status.status = m_status;
getnowpos(status.x);
return 0;
}
int32_t StepMotorCtrlModule::read_debug_info(debug_info_t& debug_info) {
debug_info.status = m_status;
getnowpos(debug_info.x);
return 0; return 0;
} }
/*******************************************************************************
* PRIVATE *
*******************************************************************************/
void StepMotorCtrlModule::updateStatus(uint8_t status) {
zlock_guard lock(m_statu_lock);
m_status = status;
int32_t StepMotorCtrlModule::set_run_param(uint8_t operation, const run_param_t& param, run_param_t& ack) {
if (operation == kset_cmd_type_set) {
m_cfg_distance_scale = param.distance_scale / 1000.0;
m_cfg_acc = param.acc;
m_cfg_dec = param.dec;
m_cfg_max_speed = param.maxspeed;
m_cfg_min_x = param.min_x;
m_cfg_max_x = param.max_x;
m_cfg_ihold = param.ihold;
m_cfg_irun = param.irun;
m_cfg_iholddelay = param.iholddelay;
m_cfg_x_shaft = param.x_shaft;
m_cfg_shift_x = param.shift_x;
ZLOGI(TAG, "=========== run param ============");
ZLOGI(TAG, "= distance_scale :%f", m_cfg_distance_scale);
ZLOGI(TAG, "= acc :%d", m_cfg_acc);
ZLOGI(TAG, "= dec :%d", m_cfg_dec);
ZLOGI(TAG, "= max_speed :%d", m_cfg_max_speed);
ZLOGI(TAG, "= min_x :%d", m_cfg_min_x);
ZLOGI(TAG, "= max_x :%d", m_cfg_max_x);
ZLOGI(TAG, "= ihold :%d", m_cfg_ihold);
ZLOGI(TAG, "= irun :%d", m_cfg_irun);
ZLOGI(TAG, "= iholddelay :%d", m_cfg_iholddelay);
ZLOGI(TAG, "= x_shaft :%d", m_cfg_x_shaft);
ZLOGI(TAG, "= shift_x :%d", m_cfg_shift_x);
ZLOGI(TAG, "==================================");
m_stepM1->setIHOLD_IRUN(m_cfg_ihold, m_cfg_irun, m_cfg_iholddelay);
m_stepM1->setScale(m_cfg_distance_scale);
}
ack.distance_scale = m_cfg_distance_scale * 1000;
ack.acc = m_cfg_acc;
ack.dec = m_cfg_dec;
ack.maxspeed = m_cfg_max_speed;
ack.min_x = m_cfg_min_x;
ack.max_x = m_cfg_max_x;
ack.ihold = m_cfg_ihold;
ack.irun = m_cfg_irun;
ack.iholddelay = m_cfg_iholddelay;
ack.x_shaft = m_cfg_x_shaft;
ack.shift_x = m_cfg_shift_x;
return 0;
} }
int32_t StepMotorCtrlModule::exec_move_to_zero_task(int32_t& dx) {
int32_t xnow;
getnowpos(xnow);
int32_t StepMotorCtrlModule::set_run_to_zero_param(uint8_t operation, const run_to_zero_param_t& param, run_to_zero_param_t& ack) {
if (operation == kset_cmd_type_set) {
m_cfg_runtozero_maxX = param.move_to_zero_max_d;
m_cfg_runtozero_leave_away_zero_max_distance = param.leave_from_zero_max_d;
m_cfg_runtozero_speed = param.speed;
m_cfg_runtozero_dec = param.dec;
int32_t ret = exec_move_to_zero_task();
ZLOGI(TAG, "=========== run_to_zero_param ============");
ZLOGI(TAG, "= move_to_zero_max_d :%d", m_cfg_runtozero_maxX);
ZLOGI(TAG, "= leave_from_zero_max_d :%d", m_cfg_runtozero_leave_away_zero_max_distance);
ZLOGI(TAG, "= speed :%d", m_cfg_runtozero_speed);
ZLOGI(TAG, "= dec :%d", m_cfg_runtozero_dec);
ZLOGI(TAG, "===========================================");
m_stepM1->setAcceleration(m_cfg_runtozero_dec);
m_stepM1->setDeceleration(m_cfg_runtozero_dec);
}
int32_t xnow2;
getnowpos(xnow2);
ack.move_to_zero_max_d = m_cfg_runtozero_maxX;
ack.leave_from_zero_max_d = m_cfg_runtozero_leave_away_zero_max_distance;
ack.speed = m_cfg_runtozero_speed;
ack.dec = m_cfg_runtozero_dec;
dx = xnow2 - xnow;
return ret;
return 0;
}
int32_t StepMotorCtrlModule::set_warning_limit_param(uint8_t operation, const warning_limit_param_t& param, warning_limit_param_t& ack) {
if (operation == kset_cmd_type_set) {
ZLOGI(TAG, "set warning limit param");
}
return 0;
} }
int32_t StepMotorCtrlModule::exec_move_to_zero_task() { int32_t StepMotorCtrlModule::exec_move_to_zero_task() {
/******************************************************************************* /*******************************************************************************
* ÔÀëXÁãµã * * ÔÀëXÁãµã *
@ -305,13 +336,22 @@ void StepMotorCtrlModule::_motor_move_to(int32_t pos, int32_t maxv, int32_t acc,
ZLOGI(TAG, "m%d _motor_move_to %d maxv:%d acc:%d dec:%d", m_id, pos, maxv, acc, dec); ZLOGI(TAG, "m%d _motor_move_to %d maxv:%d acc:%d dec:%d", m_id, pos, maxv, acc, dec);
m_stepM1->setAcceleration(acc); m_stepM1->setAcceleration(acc);
m_stepM1->setDeceleration(dec); m_stepM1->setDeceleration(dec);
m_stepM1->moveTo(pos, maxv);
int32_t motor_pos = 0;
forward_kinematics(pos, motor_pos);
m_stepM1->moveTo(motor_pos, maxv);
} }
void StepMotorCtrlModule::_motor_move_by(int32_t dpos, int32_t maxv, int32_t acc, int32_t dec) { void StepMotorCtrlModule::_motor_move_by(int32_t dpos, int32_t maxv, int32_t acc, int32_t dec) {
ZLOGI(TAG, "m%d _motor_move_by %d maxv:%d acc:%d dec:%d", m_id, dpos, maxv, acc, dec); ZLOGI(TAG, "m%d _motor_move_by %d maxv:%d acc:%d dec:%d", m_id, dpos, maxv, acc, dec);
m_stepM1->setAcceleration(acc); m_stepM1->setAcceleration(acc);
m_stepM1->setDeceleration(dec); m_stepM1->setDeceleration(dec);
m_stepM1->moveBy(dpos, maxv);
int32_t nowpos = 0;
getnowpos(nowpos);
int32_t topos = nowpos + dpos;
int motorpos = 0;
forward_kinematics(topos, motorpos);
m_stepM1->moveTo(motorpos, maxv);
} }
void StepMotorCtrlModule::_motor_stop(int32_t dec) { void StepMotorCtrlModule::_motor_stop(int32_t dec) {
ZLOGI(TAG, "m%d _motor_stop %d", m_id, dec); ZLOGI(TAG, "m%d _motor_stop %d", m_id, dec);
@ -324,11 +364,11 @@ bool StepMotorCtrlModule::_motor_is_reach_target() { return m_stepM1->isReachTar
void StepMotorCtrlModule::inverse_kinematics(int32_t motor_pos, int32_t& x) { void StepMotorCtrlModule::inverse_kinematics(int32_t motor_pos, int32_t& x) {
// m_zero_shift_x // m_zero_shift_x
x = motor_pos; x = motor_pos;
if (m_x_shaft) x = -x;
if (m_cfg_x_shaft != 0) x = -x;
x += m_zero_shift_x; x += m_zero_shift_x;
} }
void StepMotorCtrlModule::forward_kinematics(int32_t x, int32_t& motor_pos) { void StepMotorCtrlModule::forward_kinematics(int32_t x, int32_t& motor_pos) {
if (m_x_shaft) x = -x;
if (m_cfg_x_shaft != 0) x = -x;
x -= m_zero_shift_x; x -= m_zero_shift_x;
motor_pos = x; motor_pos = x;
} }

36
components/step_motor_ctrl_module/step_motor_ctrl_module.hpp

@ -3,9 +3,9 @@
#include "sdk/os/zos.hpp" #include "sdk/os/zos.hpp"
#include "sdk\components\tmc\basic\tmc_ic_interface.hpp" #include "sdk\components\tmc\basic\tmc_ic_interface.hpp"
#include "sdk\components\zcancmder\zcanreceiver.hpp" #include "sdk\components\zcancmder\zcanreceiver.hpp"
#include "sdk\components\zprotocols\zcancmder\api\i_step_motor_ctrl_module.hpp"
namespace iflytop { namespace iflytop {
class StepMotorCtrlModule {
class StepMotorCtrlModule : public I_StepMotorCtrlModule {
public: public:
private: private:
IStepperMotor* m_stepM1; IStepperMotor* m_stepM1;
@ -27,18 +27,26 @@ class StepMotorCtrlModule {
int32_t m_cfg_dec = 300000; int32_t m_cfg_dec = 300000;
int32_t m_cfg_break_dec = 300000; int32_t m_cfg_break_dec = 300000;
int32_t m_cfg_velocity = 300000; int32_t m_cfg_velocity = 300000;
int32_t m_cfg_max_speed = 300000;
u8 m_cfg_ihold = 0;
u8 m_cfg_irun = 0;
u16 m_cfg_iholddelay = 0;
u8 m_cfg_x_shaft = 0;
int32_t m_cfg_shift_x = 0;
int32_t m_cfg_min_x = 0;
int32_t m_cfg_max_x = 0;
int32_t m_cfg_runtozero_maxX = 5120000; int32_t m_cfg_runtozero_maxX = 5120000;
int32_t m_cfg_runtozero_speed = 30000; int32_t m_cfg_runtozero_speed = 30000;
int32_t m_cfg_runtozero_dec = 600000; int32_t m_cfg_runtozero_dec = 600000;
int32_t m_cfg_runtozero_leave_away_zero_max_distance = 51200; int32_t m_cfg_runtozero_leave_away_zero_max_distance = 51200;
float m_cfg_distance_scale = 1.0f; float m_cfg_distance_scale = 1.0f;
bool m_x_shaft = false;
// bool m_x_shaft = false;
public: public:
void initialize(int id, IStepperMotor* stepM, ZGPIO* zero_gpio, ZGPIO* end_gpio); void initialize(int id, IStepperMotor* stepM, ZGPIO* zero_gpio, ZGPIO* end_gpio);
#if 0
int32_t set_current_pos(int32_t pos); int32_t set_current_pos(int32_t pos);
int32_t set_distance_scale(float distance_scale); int32_t set_distance_scale(float distance_scale);
int32_t set_ihold_irun_iholddelay(uint8_t ihold, uint8_t irun, uint16_t iholddelay); int32_t set_ihold_irun_iholddelay(uint8_t ihold, uint8_t irun, uint16_t iholddelay);
@ -75,6 +83,24 @@ class StepMotorCtrlModule {
int32_t read_status(uint8_t* module_statu, int32_t* pos, int32_t* velocity); int32_t read_status(uint8_t* module_statu, int32_t* pos, int32_t* velocity);
int32_t read_gpio_status(bool* zeroio, bool* endio); int32_t read_gpio_status(bool* zeroio, bool* endio);
int32_t get_zero_shift(int32_t* pos); int32_t get_zero_shift(int32_t* pos);
#endif
virtual int32_t move_to(int32_t x, function<void(move_to_cb_status_t& status)> status_cb) override;
virtual int32_t move_by(int32_t dx, function<void(move_by_cb_status_t& status)> status_cb) override;
virtual int32_t move_to_zero(function<void(move_to_zero_cb_status_t& status)> status_cb) override;
virtual int32_t move_to_zero_with_calibrate(int32_t x, function<void(move_to_zero_with_calibrate_cb_status_t& status)> status_cb) override;
virtual int32_t enable(bool venable) override;
virtual int32_t stop(uint8_t stopType) override;
virtual int32_t force_change_current_pos(int32_t x) override;
virtual int32_t rotate(int32_t speed, int32_t lastforms, function<void(rotate_cb_status_t& status)> status_cb) override;
virtual int32_t read_version(version_t& version) override;
virtual int32_t read_status(status_t& status) override;
virtual int32_t read_debug_info(debug_info_t& debug_info) override;
virtual int32_t set_run_param(uint8_t operation, const run_param_t& param, run_param_t& ack) override;
virtual int32_t set_run_to_zero_param(uint8_t operation, const run_to_zero_param_t& param, run_to_zero_param_t& ack) override;
virtual int32_t set_warning_limit_param(uint8_t operation, const warning_limit_param_t& param, warning_limit_param_t& ack) override;
private: private:
void updateStatus(uint8_t status); void updateStatus(uint8_t status);

165
components/step_motor_ctrl_module/step_motor_speed_ctrl_module.cpp

@ -1,165 +0,0 @@
#include "step_motor_speed_ctrl_module.hpp"
#include "sdk/components/errorcode/errorcode.hpp"
using namespace iflytop;
using namespace iflytop::zcr;
#define TAG "StepMotorSpeedCtrlModule"
void StepMotorSpeedCtrlModule::initialize(int id, IStepperMotor *stepM1) {
m_id = id;
m_stepM1 = stepM1;
m_lock.init();
m_thread.init("StepMotorSpeedCtrlModule", 1024, osPriorityNormal);
}
int32_t StepMotorSpeedCtrlModule::set_acc(uint8_t act, int32_t &acc) {
zlock_guard l(m_lock);
ZLOGI(TAG, "set_acc act:%d acc:%d", act, acc);
if (act == 0) { // read
acc = m_cfg_acc;
} else if (act == 1) { // set
m_cfg_acc = acc;
} else {
return err::kcommon_error_operation_not_support;
}
return 0;
}
int32_t StepMotorSpeedCtrlModule::set_dec(uint8_t act, int32_t &dec) {
zlock_guard l(m_lock);
ZLOGI(TAG, "set_dec act:%d dec:%d", act, dec);
if (act == 0) { // read
dec = m_cfg_dec;
} else if (act == 1) { // set
m_cfg_dec = dec;
} else {
return err::kcommon_error_operation_not_support;
}
return 0;
}
int32_t StepMotorSpeedCtrlModule::set_break_dec(uint8_t act, int32_t &dec) {
zlock_guard l(m_lock);
ZLOGI(TAG, "set_break_dec act:%d dec:%d", act, dec);
if (act == 0) { // read
dec = m_cfg_break_dec;
} else if (act == 1) { // set
m_cfg_break_dec = dec;
} else {
return err::kcommon_error_operation_not_support;
}
return 0;
}
int32_t StepMotorSpeedCtrlModule::set_max_speed(uint8_t act, int32_t &speed) {
zlock_guard l(m_lock);
ZLOGI(TAG, "set_max_speed act:%d speed:%d", act, speed);
if (act == 0) { // read
speed = m_cfg_max_speed;
} else if (act == 1) { // set
m_cfg_max_speed = abs(speed);
} else {
return err::kcommon_error_operation_not_support;
}
return 0;
}
int32_t StepMotorSpeedCtrlModule::set_distance_scale(uint8_t act, float &distance_scale) {
zlock_guard l(m_lock);
ZLOGI(TAG, "set_distance_scale act:%d distance_scale:%f", act, distance_scale);
if (act == 0) { // read
distance_scale = m_cfg_distance_scale;
} else if (act == 1) { // set
m_cfg_distance_scale = distance_scale;
} else {
return err::kcommon_error_operation_not_support;
}
return 0;
}
int32_t StepMotorSpeedCtrlModule::set_shaft(uint8_t act, bool &shaft) {
zlock_guard l(m_lock);
ZLOGI(TAG, "set_shaft act:%d shaft:%d", act, shaft);
if (act == 0) { // read
shaft = m_x_shaft;
} else if (act == 1) { // set
m_x_shaft = shaft;
m_stepM1->setMotorShaft(shaft);
} else {
return err::kcommon_error_operation_not_support;
}
return 0;
}
int32_t StepMotorSpeedCtrlModule::enable(bool venable) {
zlock_guard l(m_lock);
ZLOGI(TAG, "enable venable:%d", venable);
m_stepM1->enable(venable);
return 0;
}
int32_t StepMotorSpeedCtrlModule::stop() {
zlock_guard l(m_lock);
ZLOGI(TAG, "stop");
m_thread.stop();
m_stepM1->stop();
return 0;
}
int32_t StepMotorSpeedCtrlModule::brake() {
zlock_guard l(m_lock);
ZLOGI(TAG, "brake");
m_thread.stop();
m_stepM1->setDeceleration(m_cfg_break_dec);
m_stepM1->rotate(0);
return 0;
}
int32_t StepMotorSpeedCtrlModule::rotate(int32_t velocity, int32_t acc, int32_t dec) {
zlock_guard l(m_lock);
ZLOGI(TAG, "rotate velocity:%d acc:%d dec:%d", velocity, acc, dec);
if (acc <= 0) acc = m_cfg_acc;
if (dec <= 0) dec = m_cfg_dec;
if (abs(velocity) > m_cfg_max_speed) {
ZLOGW(TAG, "velocity:%d > m_cfg_max_speed:%d", velocity, m_cfg_max_speed);
velocity = m_cfg_max_speed;
}
m_stepM1->setAcceleration(acc);
m_stepM1->setDeceleration(dec);
m_stepM1->rotate(velocity);
return 0;
}
int32_t StepMotorSpeedCtrlModule::rotate_auto_stop(int32_t velocity, int32_t duration, int32_t acc, int32_t dec,
function<void(bool reachtime, int32_t has_rotate_duration)> callback) {
zlock_guard l(m_lock);
ZLOGI(TAG, "rotate_auto_stop velocity:%d duration:%d acc:%d dec:%d", velocity, duration, acc, dec);
if (duration < 0) {
ZLOGW(TAG, "duration:%d < 0", duration);
return err::kcommon_error_param_out_of_range;
}
if (acc <= 0) acc = m_cfg_acc;
if (dec <= 0) dec = m_cfg_dec;
if (abs(velocity) > m_cfg_max_speed) {
ZLOGW(TAG, "velocity:%d > m_cfg_max_speed:%d", velocity, m_cfg_max_speed);
velocity = m_cfg_max_speed;
}
m_thread.stop();
m_thread.start([this, duration, acc, dec, velocity, callback]() {
m_stepM1->setAcceleration(acc);
m_stepM1->setDeceleration(dec);
m_stepM1->rotate(velocity);
int32_t startticket = zos_get_tick();
bool reachtime = false;
while (!m_thread.getExitFlag()) {
if (zos_haspassedms(startticket) > duration) {
reachtime = true;
m_stepM1->stop();
break;
}
osDelay(1);
}
if (callback) callback(reachtime, zos_haspassedms(startticket));
m_stepM1->stop();
return;
});
return 0;
}

44
components/step_motor_ctrl_module/step_motor_speed_ctrl_module.hpp

@ -1,44 +0,0 @@
#pragma once
//
#include "sdk/os/zos.hpp"
#include "sdk\components\tmc\basic\tmc_ic_interface.hpp"
#include "sdk\components\zcancmder\zcanreceiver.hpp"
namespace iflytop {
class StepMotorSpeedCtrlModule {
public:
private:
IStepperMotor *m_stepM1;
int m_id = 0;
ZThread m_thread;
zmutex m_lock;
int32_t m_cfg_acc = 350;
int32_t m_cfg_dec = 350;
int32_t m_cfg_break_dec = 350;
int32_t m_cfg_max_speed = 1000;
float m_cfg_distance_scale = 853.3333f; // ²½½øµç»ú step/s ת»»³É rpm
bool m_x_shaft = false;
public:
void initialize(int id, IStepperMotor *stepM1);
int32_t set_acc(uint8_t act, int32_t &acc);
int32_t set_dec(uint8_t act, int32_t &dec);
int32_t set_break_dec(uint8_t act, int32_t &dec);
int32_t set_max_speed(uint8_t act, int32_t &speed);
int32_t set_distance_scale(uint8_t act, float &distance_scale);
int32_t set_shaft(uint8_t act, bool &shaft);
int32_t enable(bool venable);
int32_t stop();
int32_t brake();
int32_t rotate(int32_t velocity, int32_t acc, int32_t dec);
int32_t rotate_auto_stop(int32_t velocity, int32_t duration, int32_t acc, int32_t dec,
function<void(bool reachtime, int32_t has_rotate_duration)> callback);
};
} // namespace iflytop

2
components/zprotocols/zcancmder

@ -1 +1 @@
Subproject commit f051569d8988645cdb1ffd5429a3b72629df6020
Subproject commit ba0b50468b776c0bf4182300d80d5d973ebea058
Loading…
Cancel
Save