-
Notifications
You must be signed in to change notification settings - Fork 1
/
rocker.cpp
66 lines (53 loc) · 1.16 KB
/
rocker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "rocker.h"
int8_t delta = 0;
unsigned long next_rock = 0;
int8_t last_intensity = 0;
// apply the motor delta for one step
void rocker_delta(Stepper motor)
{
int8_t dir = delta<0?-1:delta>0?1:0;
switch(dir) {
case -1:
//Serial.println("step");
motor.step(delta);
delta=0;
break;
case 1:
//Serial.println("step-1");
motor.step(-delta);
delta=0;
break;
}
}
void rocker_set_delta(int8_t d)
{
Serial.println(String("set delta:") + String(d));
delta = d;
}
void rocker_init()
{
next_rock = millis();
}
// state machine step
void rocker_step(Stepper motor, uint8_t intensity, uint8_t dutycycle, float frequency)
{
unsigned long now = millis();
//Serial.println(String(now) + ", " + String(next_rock));
if(now>next_rock)
{
rock(motor, intensity);
unsigned long time_delta = int(500./frequency);
while(next_rock<now)
next_rock = next_rock + time_delta * (unsigned long)dutycycle;
}
//rocker_delta(motor);
}
void rock(Stepper motor, uint8_t intensity)
{
if(last_intensity<0) {
last_intensity = intensity;
} else {
last_intensity = -intensity;
}
motor.step(last_intensity);
}