台形制御用ライブラリ

Dependents:   kisoken_PenPlotter

Committer:
Akito914
Date:
Thu Aug 03 06:50:03 2017 +0000
Revision:
0:a7573f3f2207
Child:
1:15adfb7ee64c
???????????????????;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Akito914 0:a7573f3f2207 1
Akito914 0:a7573f3f2207 2 #include "mbed.h"
Akito914 0:a7573f3f2207 3 #include "trapezoidalMotionCal.h"
Akito914 0:a7573f3f2207 4
Akito914 0:a7573f3f2207 5
Akito914 0:a7573f3f2207 6 trapezoidalMotionCal::trapezoidalMotionCal(double f_start, double f_stop, double f_max, double a_up, double a_down){
Akito914 0:a7573f3f2207 7 _f_start = f_start;
Akito914 0:a7573f3f2207 8 _f_stop = f_stop;
Akito914 0:a7573f3f2207 9 _f_max = f_max;
Akito914 0:a7573f3f2207 10 _a_up = a_up;
Akito914 0:a7573f3f2207 11 _a_down = a_down;
Akito914 0:a7573f3f2207 12
Akito914 0:a7573f3f2207 13 shortMode = 0;
Akito914 0:a7573f3f2207 14 revMode = 0;
Akito914 0:a7573f3f2207 15
Akito914 0:a7573f3f2207 16 step_accel = (f_max * f_max - f_start * f_start) / (2 * a_up);
Akito914 0:a7573f3f2207 17 step_decel = (f_stop * f_stop - f_max * f_max) / (2 * a_down);
Akito914 0:a7573f3f2207 18
Akito914 0:a7573f3f2207 19
Akito914 0:a7573f3f2207 20 }
Akito914 0:a7573f3f2207 21
Akito914 0:a7573f3f2207 22 void trapezoidalMotionCal::setTarg(int32_t targ){
Akito914 0:a7573f3f2207 23
Akito914 0:a7573f3f2207 24 targSteps = targ;
Akito914 0:a7573f3f2207 25
Akito914 0:a7573f3f2207 26 if(targSteps < 0){
Akito914 0:a7573f3f2207 27 revMode = 1;
Akito914 0:a7573f3f2207 28 targSteps *= -1;
Akito914 0:a7573f3f2207 29 }
Akito914 0:a7573f3f2207 30
Akito914 0:a7573f3f2207 31 step_const = (double)targSteps - step_accel - step_decel;
Akito914 0:a7573f3f2207 32
Akito914 0:a7573f3f2207 33 if (step_const <= 0) {
Akito914 0:a7573f3f2207 34 shortMode = 1;
Akito914 0:a7573f3f2207 35 step_accel = (2 * _a_down * targSteps + _f_start * _f_start - _f_stop * _f_stop) / (2 * (_a_down - _a_up));
Akito914 0:a7573f3f2207 36 step_decel = targSteps - step_accel;
Akito914 0:a7573f3f2207 37 step_const = 0;
Akito914 0:a7573f3f2207 38 f_reach = sqrt(2 * _a_up * step_accel + _f_start * _f_start);
Akito914 0:a7573f3f2207 39 }
Akito914 0:a7573f3f2207 40
Akito914 0:a7573f3f2207 41 }
Akito914 0:a7573f3f2207 42
Akito914 0:a7573f3f2207 43 uint32_t trapezoidalMotionCal::calStepDelay(int32_t steps){
Akito914 0:a7573f3f2207 44 uint32_t period_us;
Akito914 0:a7573f3f2207 45
Akito914 0:a7573f3f2207 46 if (steps < step_accel) {
Akito914 0:a7573f3f2207 47 period_us = 1000000 * pow(_f_start * _f_start + 2 * _a_up * steps, -0.5);
Akito914 0:a7573f3f2207 48 }
Akito914 0:a7573f3f2207 49 else if (steps < step_accel + step_const) {
Akito914 0:a7573f3f2207 50 period_us = 1000000 / _f_max;
Akito914 0:a7573f3f2207 51 }
Akito914 0:a7573f3f2207 52 else {
Akito914 0:a7573f3f2207 53 if (shortMode == 0) {
Akito914 0:a7573f3f2207 54 period_us = 1000000 * pow(_f_max * _f_max + 2 * _a_down * (steps - step_accel - step_const), -0.5);
Akito914 0:a7573f3f2207 55 }
Akito914 0:a7573f3f2207 56 else {
Akito914 0:a7573f3f2207 57 period_us = 1000000 * pow(f_reach * f_reach + 2 * _a_down * (steps - step_accel - step_const), -0.5);
Akito914 0:a7573f3f2207 58 }
Akito914 0:a7573f3f2207 59
Akito914 0:a7573f3f2207 60 }
Akito914 0:a7573f3f2207 61
Akito914 0:a7573f3f2207 62 return period_us;
Akito914 0:a7573f3f2207 63 }
Akito914 0:a7573f3f2207 64