diff options
Diffstat (limited to 'linz/glasslathe/scurve/calc.cpp')
| -rw-r--r-- | linz/glasslathe/scurve/calc.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/linz/glasslathe/scurve/calc.cpp b/linz/glasslathe/scurve/calc.cpp new file mode 100644 index 0000000..1371577 --- /dev/null +++ b/linz/glasslathe/scurve/calc.cpp | |||
| @@ -0,0 +1,78 @@ | |||
| 1 | #include "stdio.h" | ||
| 2 | #include <cmath> | ||
| 3 | #include <algorithm> | ||
| 4 | |||
| 5 | struct { | ||
| 6 | int direction = 0; | ||
| 7 | int frequency = 0; | ||
| 8 | int potValue = 1023; | ||
| 9 | } current, target; | ||
| 10 | |||
| 11 | const int frequency_max = 6000; | ||
| 12 | int newPotValue; | ||
| 13 | bool timer_enabled = false; | ||
| 14 | |||
| 15 | int x = 0; | ||
| 16 | bool loop() | ||
| 17 | { | ||
| 18 | const int step_delay = 300; | ||
| 19 | if (current.frequency != target.frequency || current.direction != target.direction) | ||
| 20 | { | ||
| 21 | if (!timer_enabled && target.frequency != 0) | ||
| 22 | { | ||
| 23 | timer_enabled = true; | ||
| 24 | fprintf(stderr, "enable motor\n"); | ||
| 25 | x += step_delay; | ||
| 26 | printf("[%d, %d],\n", x, 0); | ||
| 27 | } | ||
| 28 | |||
| 29 | /*int step = (current.potValue < 20) ? 1 | ||
| 30 | : (current.potValue < 100) ? 2 | ||
| 31 | : (current.potValue < 300) ? 3 | ||
| 32 | : (current.potValue < 400) ? 5 | ||
| 33 | : (current.potValue < 500) ? 10 | ||
| 34 | : 20;*/ | ||
| 35 | int step = (current.frequency > 5000) ? 1 | ||
| 36 | : (current.frequency > 4500) ? 2 | ||
| 37 | : (current.frequency > 4000) ? 5 | ||
| 38 | : (current.frequency > 3000) ? 10 | ||
| 39 | : (current.frequency > 1000) ? 25 | ||
| 40 | : 50; | ||
| 41 | int frequency = (current.direction == target.direction) ? target.frequency : 0; | ||
| 42 | step = std::min(abs(frequency - current.frequency), step); | ||
| 43 | if (frequency < current.frequency) | ||
| 44 | step *= -1; | ||
| 45 | current.frequency += step; | ||
| 46 | |||
| 47 | x += step_delay; | ||
| 48 | printf("[%d, %d],\n", x, current.frequency); | ||
| 49 | |||
| 50 | if (current.frequency == 0 && current.direction != target.direction) | ||
| 51 | { | ||
| 52 | timer_enabled = false; | ||
| 53 | current.direction = target.direction; | ||
| 54 | fprintf(stderr, "disable motor. changed direction to %d\n", current.direction); | ||
| 55 | x += step_delay; | ||
| 56 | printf("[%d, %d],\n", x, 0); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | else if (timer_enabled && target.potValue == 0) | ||
| 60 | { | ||
| 61 | timer_enabled = false; | ||
| 62 | fprintf(stderr, "disable motor\n"); | ||
| 63 | x += step_delay; | ||
| 64 | printf("[%d, %d],\n", x, 0); | ||
| 65 | } | ||
| 66 | else | ||
| 67 | return false; | ||
| 68 | return true; | ||
| 69 | } | ||
| 70 | |||
| 71 | |||
| 72 | int main(int argc, char *argv[]) | ||
| 73 | { | ||
| 74 | target.frequency = frequency_max; | ||
| 75 | target.potValue = 1023; | ||
| 76 | while(loop()); | ||
| 77 | return 0; | ||
| 78 | } | ||
