-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.cpp
More file actions
138 lines (129 loc) · 3.48 KB
/
Controller.cpp
File metadata and controls
138 lines (129 loc) · 3.48 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "Controller.h"
Controller::Controller(CustomStepper *stepperH,CustomStepper *stepperM,Clock *clock)
{
_clock = clock;
_stepperH = stepperH;
_stepperM = stepperM;
//initialise positions to match those on the clock face 0 = 12
_positions[0] = 300;
_positions[1] = 120;
_positions[2] = 270;
_positions[3] = 180;
_positions[4] = 330;
_positions[5] = 60;
_positions[6] = 0;
_positions[7] = 90;
_positions[8] = 150;
_positions[9] = 240;
_positions[10] = 30;
_positions[11] = 210;
}
void Controller::init()
{
_clock->init();
_stepperH->setRPM(10);
_stepperM->setRPM(10);
_stepperH->home();
_stepperM->home();
this->running = true;
}
void Controller::Adjust()
{
//Fine tune positions
if (this->commandbuffer.charAt(1) == 'H') {
this->hourAdjust += this->commandbuffer.substring(2).toInt();
}
if (this->commandbuffer.charAt(1) == 'M') {
this->minuteAdjust += this->commandbuffer.substring(2).toInt();
}
//Todo: Save these to Eeprom
}
void Controller::readinput()
{
if (Serial.available() > 0)
{
this->commandbuffer = Serial.readString();
if (this->commandbuffer.length() > 0)
{
switch(this->commandbuffer.charAt(0)){
case '?':
Serial.println("? - Instructions");
Serial.println("H - Home and stop");
Serial.println("Thh:mm - Set time");
Serial.println("Ddd/mm/yyyy - Set date");
Serial.println("R - Run");
Serial.println("S - Stop");
break;
case 'A':
Serial.println("Motor Adjustments");
Adjust();
break;
case 'T':
if (this->_clock->parseTime(this->commandbuffer.substring(1))){
Serial.println("Setting Time");
}
else {
Serial.println("Invalid Time");
}
break;
case 'D':
if (this->_clock->parseDate(this->commandbuffer.substring(1))){
Serial.println("Setting Date");
}
else {
Serial.println("Invalid Date");
}
break;
case 'R':
this->running = true;
break;
case 'S':
this->running = false;
break;
case 'H':
Serial.println("Homing and stop");
this->_stepperH->home();
this->_stepperM->home();
this->running = false;
break;
default:
this->_clock->display();
}
}
}
}
float Controller::CalcMinutePos(int m,int s)
{
return _positions[(int)(m / 5.0)] + ((m % 5) * 6.0);
}
float Controller::CalcHourPos(int h,int m)
{
return _positions[(h % 12)] + (m * 0.5);
}
void Controller::run()
{
if (this->running) {
if (this->_stepperM->isDone() && this->_stepperH->isDone()) {
this->_stepperH->rotateToDegrees(CalcHourPos(this->_clock->chour(),this->_clock->cminute()) + this->hourAdjust);
this->_stepperM->rotateToDegrees(CalcMinutePos(this->_clock->cminute(),this->_clock->csecond()) + this->minuteAdjust);
}
}
this->_stepperH->run();
this->_stepperM->run();
}
void Controller::dump()
{
Serial.println("================");
Serial.println(this->running ? "Running" : "Stopped");
Serial.print("Hour Adjust:");
Serial.println(this->hourAdjust);
Serial.print("Minute Adjust:");
Serial.println(this->minuteAdjust);
this->_clock->display();
Serial.println("------------------");
Serial.println("Hour Motor");
this->_stepperH->dump();
Serial.println("------------------");
Serial.println("Minute Motor");
this->_stepperM->dump();
}