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.
Diff: lin_step_mtr.cpp
- Revision:
- 7:0d941d1140ad
- Parent:
- 6:0c5418e05145
- Child:
- 8:f1d869d9b8df
--- a/lin_step_mtr.cpp Mon Apr 27 14:42:19 2020 +0000
+++ b/lin_step_mtr.cpp Mon Apr 27 17:26:51 2020 +0000
@@ -13,11 +13,10 @@
dir = CW;
cur_step = ONE;
- stop_mtr = true;
- terminate = false;
- rotate_th = NULL;
+ min_rev_cnt = MIN_DOUBLE_VAL;
+ max_rev_cnt = MAX_DOUBLE_VAL;
- cur_state = STOP_MOTOR;
+ pc.printf("Init:\nmin: %f\nmax: %f\n",min_rev_cnt,max_rev_cnt);
}
LinStepMtr::LinStepMtr(PinName A_f, PinName A_r, PinName B_f, PinName B_r)
@@ -27,16 +26,10 @@
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();
}
@@ -45,117 +38,95 @@
return (float) speed * 3 / 10;
}
+void LinStepMtr::set_speed(float rpm) {
+ speed = floor((float)rpm * 10/3);
+}
+
double LinStepMtr::get_rev()
{
return rev_cnt;
}
+void LinStepMtr::set_min_rev_cnt(double rc)
+{
+ if(rc < MIN_DOUBLE_VAL || rc > MAX_DOUBLE_VAL)
+ min_rev_cnt = MIN_DOUBLE_VAL;
+ else
+ min_rev_cnt = rc;
+
+ pc.printf("min: %f\n", min_rev_cnt);
+}
+
+double LinStepMtr::get_min_rev_cnt()
+{
+ return min_rev_cnt;
+}
+
+void LinStepMtr::set_max_rev_cnt(double rc)
+{
+ if(rc < MIN_DOUBLE_VAL || rc > MAX_DOUBLE_VAL)
+ max_rev_cnt = MAX_DOUBLE_VAL;
+ else
+ max_rev_cnt = rc;
+
+ pc.printf("max: %f\n",max_rev_cnt);
+}
+
+double LinStepMtr::get_max_rev_cnt()
+{
+ return max_rev_cnt;
+}
+
+void LinStepMtr::RESET_rev_cnts()
+{
+ max_rev_cnt = MAX_DOUBLE_VAL;
+ min_rev_cnt = MIN_DOUBLE_VAL;
+}
+
+
+
+
+
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::start(Direction d) {
- if(dir != d){
- dir = d;
- }
- 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) {
+double LinStepMtr::rotate(Direction d, float rev)
+{
+ int steps = floor(200 * rev);
+ float w = 1/(float)speed;
+ switch(d){
+ case CW:
+ for (int i = 0; i < steps; ++i) {
+ if(rev_cnt > max_rev_cnt -.005){
+ break;
+ }
+ mtr_ctrl = ++cur_step;
+ rev_cnt += .005;
+ wait(w);
+ }
+ break;
+ case CCW:
+ for (int i = 0; i < steps; ++i) {
+ if (rev_cnt < min_rev_cnt + .005){
+ break;
+ }
+ mtr_ctrl = --cur_step;
+ rev_cnt -= .005;
+ wait(w);
+ }
+ break;
- 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();
- }
}
-
+ mtr_ctrl = STOP;
+ return rev_cnt;
}
#define SPIN_INCR 83 // steps
#define SPIN_WAIT 10 // ms
void LinStepMtr::spin_up(float rpm) {
+/*
int end_speed;
if(rpm == -1) {
@@ -168,10 +139,11 @@
Thread::wait(SPIN_WAIT);
}
speed = end_speed;
-
+*/
}
int LinStepMtr::spin_down(float rpm) {
+/*
int end_speed;
int s = speed;
@@ -187,6 +159,9 @@
}
speed = end_speed;
return s;
+*/
+
+ return -1;
}
// Private Step Class functions