-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpepstep.cpp
57 lines (49 loc) · 1.59 KB
/
pepstep.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
#include "pepstep.h"
#include <CNCShield.h>
#include <Arduino.h>
pep::ScheduleEntry::ScheduleEntry(unsigned int call_every, void (*callback)(void*), void* params):
call_every(call_every),
callback(callback),
params(params) {}
auto pep::ScheduleEntry::reschedule(unsigned int call_every) -> void {
this->call_every = call_every;
call_count = 0;
}
auto pep::ScheduleEntry::poll(unsigned long ellapsed) -> void {
auto tmp = ellapsed / call_every;
if (tmp > call_count) {
call_count = tmp;
callback(params);
}
}
constexpr unsigned long one_second = 10000;
constexpr unsigned long max_steps_per_second = 300;
pep::CNCShieldMotor::CNCShieldMotor(void* motor, int max_steps_per_second): motor((StepperMotor*)motor), max_steps_per_second(max_steps_per_second) {}
auto pep::CNCShieldMotor::step() -> void {
StepperMotor* _motor = (StepperMotor*)motor;
Serial.println(step_dir);
if (step_dir > 0) _motor->step(CLOCKWISE);
if (step_dir < 0) _motor->step(COUNTER);
steps += step_dir;
}
auto pep::CNCShieldMotor::get(double steps_per_unit) -> double {
return double(steps) / steps_per_unit;
}
auto pep::CNCShieldMotor::set(ScheduleEntry& scheduler, double speed) -> void {
step_dir = 0;
if (speed > 0)
step_dir = 1;
if (speed < 0) {
step_dir = -1;
speed *=- 1;
}
auto desired_steps_per_second = (unsigned long)(double(max_steps_per_second) * speed);
scheduler.reschedule(one_second / desired_steps_per_second);
}
auto pep::CNCShieldMotor::stop(ScheduleEntry& scheduler) -> void {
scheduler.reschedule(timeout_default);
step_dir = 0;
}
auto pep::CNCShieldMotor::reset() -> void {
steps = 0;
}