台形制御用ライブラリ
Dependents: kisoken_PenPlotter
trapezoidalMotionCal.cpp@1:15adfb7ee64c, 2017-08-03 (annotated)
- Committer:
- Akito914
- Date:
- Thu Aug 03 12:42:12 2017 +0000
- Revision:
- 1:15adfb7ee64c
- Parent:
- 0:a7573f3f2207
- Child:
- 2:a43e90cb271c
?????????????????????
Who changed what in which revision?
User | Revision | Line number | New 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 | } |
Akito914 | 0:a7573f3f2207 | 17 | |
Akito914 | 0:a7573f3f2207 | 18 | void trapezoidalMotionCal::setTarg(int32_t targ){ |
Akito914 | 0:a7573f3f2207 | 19 | |
Akito914 | 0:a7573f3f2207 | 20 | targSteps = targ; |
Akito914 | 0:a7573f3f2207 | 21 | |
Akito914 | 0:a7573f3f2207 | 22 | if(targSteps < 0){ |
Akito914 | 0:a7573f3f2207 | 23 | revMode = 1; |
Akito914 | 0:a7573f3f2207 | 24 | targSteps *= -1; |
Akito914 | 0:a7573f3f2207 | 25 | } |
Akito914 | 0:a7573f3f2207 | 26 | |
Akito914 | 1:15adfb7ee64c | 27 | step_accel = (_f_max * _f_max - _f_start * _f_start) / (2 * _a_up); |
Akito914 | 0:a7573f3f2207 | 28 | step_const = (double)targSteps - step_accel - step_decel; |
Akito914 | 1:15adfb7ee64c | 29 | step_decel = (_f_stop * _f_stop - _f_max * _f_max) / (2 * _a_down); |
Akito914 | 0:a7573f3f2207 | 30 | |
Akito914 | 0:a7573f3f2207 | 31 | if (step_const <= 0) { |
Akito914 | 0:a7573f3f2207 | 32 | shortMode = 1; |
Akito914 | 0:a7573f3f2207 | 33 | step_accel = (2 * _a_down * targSteps + _f_start * _f_start - _f_stop * _f_stop) / (2 * (_a_down - _a_up)); |
Akito914 | 0:a7573f3f2207 | 34 | step_decel = targSteps - step_accel; |
Akito914 | 0:a7573f3f2207 | 35 | step_const = 0; |
Akito914 | 0:a7573f3f2207 | 36 | f_reach = sqrt(2 * _a_up * step_accel + _f_start * _f_start); |
Akito914 | 0:a7573f3f2207 | 37 | } |
Akito914 | 0:a7573f3f2207 | 38 | |
Akito914 | 0:a7573f3f2207 | 39 | } |
Akito914 | 0:a7573f3f2207 | 40 | |
Akito914 | 0:a7573f3f2207 | 41 | uint32_t trapezoidalMotionCal::calStepDelay(int32_t steps){ |
Akito914 | 0:a7573f3f2207 | 42 | uint32_t period_us; |
Akito914 | 0:a7573f3f2207 | 43 | |
Akito914 | 0:a7573f3f2207 | 44 | if (steps < step_accel) { |
Akito914 | 0:a7573f3f2207 | 45 | period_us = 1000000 * pow(_f_start * _f_start + 2 * _a_up * steps, -0.5); |
Akito914 | 0:a7573f3f2207 | 46 | } |
Akito914 | 0:a7573f3f2207 | 47 | else if (steps < step_accel + step_const) { |
Akito914 | 0:a7573f3f2207 | 48 | period_us = 1000000 / _f_max; |
Akito914 | 0:a7573f3f2207 | 49 | } |
Akito914 | 0:a7573f3f2207 | 50 | else { |
Akito914 | 0:a7573f3f2207 | 51 | if (shortMode == 0) { |
Akito914 | 0:a7573f3f2207 | 52 | period_us = 1000000 * pow(_f_max * _f_max + 2 * _a_down * (steps - step_accel - step_const), -0.5); |
Akito914 | 0:a7573f3f2207 | 53 | } |
Akito914 | 0:a7573f3f2207 | 54 | else { |
Akito914 | 0:a7573f3f2207 | 55 | period_us = 1000000 * pow(f_reach * f_reach + 2 * _a_down * (steps - step_accel - step_const), -0.5); |
Akito914 | 0:a7573f3f2207 | 56 | } |
Akito914 | 0:a7573f3f2207 | 57 | |
Akito914 | 0:a7573f3f2207 | 58 | } |
Akito914 | 0:a7573f3f2207 | 59 | |
Akito914 | 0:a7573f3f2207 | 60 | return period_us; |
Akito914 | 0:a7573f3f2207 | 61 | } |
Akito914 | 0:a7573f3f2207 | 62 |