Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
lin_step_mtr.cpp
- Committer:
- mikermarza
- Date:
- 2020-04-27
- Revision:
- 4:1dc350268172
- Parent:
- 3:2138b69ee3bd
- Child:
- 5:955bbc08ee78
File content as of revision 4:1dc350268172:
// Code for the lin_step_mtr driver
#include "lin_step_mtr.h"
//#include "debug.h"
//Construtor
LinStepMtr::LinStepMtr(PinName A_f, PinName A_r, PinName B_f, PinName B_r, int m_rpm=MAX_RPM)
:mtr_ctrl(B_r, A_r, B_f,A_f), max_speed((m_rpm > MAX_RPM) ? floor((float)MAX_RPM * 10/3):floor((float)m_rpm * 10/3)),
max_rpm(m_rpm)
{
mtr_ctrl = 0x0;
speed = floor((float)DEFAULT_RPM * 10 / 3);
dir = CW;
cur_step = ONE;
stop_mtr = true;
terminate = false;
rotate_th = NULL;
cur_state = STOP_MOTOR;
}
LinStepMtr::LinStepMtr(PinName A_f, PinName A_r, PinName B_f, PinName B_r)
:mtr_ctrl(B_r,A_r,B_f,A_f), max_speed(floor((float)MAX_RPM * 10/3)), max_rpm(MAX_RPM)
{
mtr_ctrl = 0x0;
speed = floor((double)DEFAULT_RPM * 10 / 3);
dir = CW;
cur_step = ONE;
stop_mtr = true;
terminate = false;
cur_state = STOP_MOTOR;
}
LinStepMtr::~LinStepMtr()
{
this->end();
}
float LinStepMtr::get_speed()
{
return (float) speed * 3 / 10;
}
LinStepMtr::Direction LinStepMtr::get_dir()
{
return dir;
}
void LinStepMtr::init(double rpm, Direction d)
{
terminate = false;
speed = floor(rpm * 10 / 3);
dir = d;
if (!rotate_th) {
rotate_th = new Thread(LinStepMtr::rotate_help, this);
}
}
void LinStepMtr::end() {
terminate = true;
Thread::wait(100);
if (rotate_th) {
delete rotate_th;
rotate_th = NULL;
}
}
void LinStepMtr::start() {
stop_mtr = false;
spin_up();
}
void LinStepMtr::stop() {
int s = spin_down();
stop_mtr = true;
speed = s;
}
void LinStepMtr::rotate_help(void const *args)
{
LinStepMtr *instPtr = static_cast<LinStepMtr *>(const_cast<void *>(args));
instPtr->rotate();
}
void LinStepMtr::change_dir(Direction d)
{
if(dir != d) {
if(stop_mtr) {
dir = d;
} else {
int s = spin_down();
stop_mtr = true;
dir = d;
stop_mtr = false;
spin_up(s);
}
}
}
void LinStepMtr::change_speed(float rpm)
{
speed = floor(rpm * 10 / 3);
}
void LinStepMtr::rotate()
{
// pc.printf("Called rotate()\n");
/*
while(1) {
mtr_ctrl = ++cur_step;
rev_cnt +=.005;
wait(pause);
}
*/
while(!terminate) {
if(!stop_mtr){
switch(dir) {
case CW:
mtr_ctrl = ++cur_step;
rev_cnt +=.005;
break;
case CCW:
mtr_ctrl = --cur_step;
rev_cnt-=.005;
break;
}
wait(1/ (float) speed);
} else {
mtr_ctrl = STOP;
Thread::yield();
}
}
}
#define SPIN_INCR 83 // steps
#define SPIN_WAIT 10 // ms
void LinStepMtr::spin_up(float rpm) {
int end_speed;
if(rpm == -1) {
end_speed = speed;
} else {
end_speed = (rpm > max_rpm) ? max_speed:floor(rpm * 10 / 3);
}
for(int i = min_speed; i < end_speed-SPIN_INCR; i+=SPIN_INCR){
speed = i;
Thread::wait(SPIN_WAIT);
}
speed = end_speed;
}
int LinStepMtr::spin_down(float rpm) {
int end_speed;
int s = speed;
if(rpm == -1) {
end_speed = min_speed;
} else {
end_speed = (rpm < min_rpm) ? min_speed:floor(rpm * 10 / 3);
}
for(int i = speed; i > end_speed+SPIN_INCR; i-=SPIN_INCR){
speed = i;
Thread::wait(SPIN_WAIT);
}
speed = end_speed;
return s;
}
// Private Step Class functions
/*
LinStepMtr::Step::Step() : cur_step(ONE)
{
pc.printf("\n\nCalled Constructor\n cur_step: %x\n",cur_step);
pc.printf("Step_Num val:\n ONE = %x\n TWO = %x\n THREE = %x\n FOUR = %x\n\n", ONE, TWO, THREE, FOUR);
};
*/
LinStepMtr::Step_Num LinStepMtr::Step::get_cur_step()
{
return cur_step;
}
LinStepMtr::Step_Num LinStepMtr::Step::operator++()
{
switch(cur_step){
case ONE:
cur_step=TWO;
break;
case TWO:
cur_step = THREE;
break;
case THREE:
cur_step = FOUR;
break;
case FOUR:
cur_step = ONE;
break;
}
//pc.printf(" CUR_STEP = %x\n",cur_step);
return cur_step;
}
LinStepMtr::Step_Num LinStepMtr::Step::operator--()
{
switch(cur_step){
case ONE:
cur_step=FOUR;
break;
case TWO:
cur_step = ONE;
break;
case THREE:
cur_step = TWO;
break;
case FOUR:
cur_step = THREE;
break;
}
return cur_step;
}
void LinStepMtr::Step::operator=(LinStepMtr::Step_Num s)
{
cur_step = s;
}