Skip to content

Commit 417bf53

Browse files
Refactoring, utility funcs, simplification, bug fixes
Added logging functions. Moved stepper motor target calculations into its own file. Added comments. Used DayTime class for RATime and HATime. Ensured RATime works correctly. When moving an axis, the LCD displays progress via percent complete. Fixed bug in LCD menu functions that didn't correctly print floats and caused a hang (!). Re-added the old calculation code to verify new code works teh same way.
1 parent f7c5502 commit 417bf53

File tree

10 files changed

+248
-83
lines changed

10 files changed

+248
-83
lines changed

Software/Arduino code/OpenAstroTracker/a_inits.ino

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ String logString;
9999
//unsigned long timeWait;
100100
boolean isPulseGuiding = true;
101101

102-
float onehour;
102+
unsigned long Zeit;
103+
float stepsPerHour;
103104

104105
boolean pcControl = false;
105106
int currentSecs;
@@ -116,14 +117,14 @@ int tracking = 1;
116117
float trackingspeed;
117118

118119
//RA stuff
119-
//float hourRA;
120-
//float minRA = 0;
121-
//float secRA = 0;
120+
int hourRA=0;
121+
int minRA = 0;
122+
int secRA = 0;
122123
float moveRA;
123124
int RAselect;
124-
//int hourRAprint;
125-
//int minRAprint;
126-
//int secRAprint;
125+
int hourRAprint;
126+
int minRAprint;
127+
int secRAprint;
127128

128129
//DEC stuff
129130
int degreeDEC;
@@ -133,10 +134,15 @@ float moveDEC;
133134
int DECselect;
134135
int printdegDEC;
135136

137+
//Hour correction
138+
int hourHA = 0;
139+
int minHA = 0;
136140
int HAselect;
137-
//int hHAcorrection;
138-
//int mHAcorrection = -5 ; //wenn minus dann hHAcorrection -1
139-
//int sHAcorrection;
141+
int hHAcorrection;
142+
int mHAcorrection = -5 ; //wenn minus dann hHAcorrection -1
143+
int sHAcorrection;
144+
int hourHAzeit;
145+
int minHAzeit;
140146

141147
int heatselect;
142148
int RAheat = 0;
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
void log(const char* input) {
2+
Serial.println(input);
3+
Serial.flush();
4+
}
5+
6+
void logv(const char* input, ...) {
7+
va_list argp;
8+
va_start(argp, input);
9+
// Serial.println("HERE");
10+
Serial.println(formatArg(input, argp));
11+
Serial.flush();
12+
va_end(argp);
13+
14+
}
15+
16+
String format(const char* input, ...) {
17+
va_list argp;
18+
va_start(argp, input);
19+
String ret = formatArg(input, argp);
20+
va_end(argp);
21+
return ret;
22+
}
23+
24+
String formatArg(const char* input, va_list args) {
25+
char achBuffer[256];
26+
char*p = achBuffer;
27+
28+
for (const char* i = input; *i != 0; i++) {
29+
if (*i != '%') {
30+
*p++ = *i;
31+
continue;
32+
}
33+
i++;
34+
switch (*i) {
35+
case '%': {
36+
*p++ = '%';
37+
}
38+
break;
39+
40+
case 'c': {
41+
byte b = (byte) (va_arg(args, int) && 0x00FF);
42+
*p++ = (char)b;
43+
}
44+
break;
45+
46+
case 's': {
47+
char* s = va_arg(args, char*);
48+
strcpy(p, s);
49+
p += strlen(s);
50+
}
51+
break;
52+
53+
case 'd': {
54+
String s = String((int)va_arg(args, int));
55+
strcpy(p, s.c_str());
56+
p += s.length();
57+
}
58+
break;
59+
60+
case 'l': {
61+
String s = String((long)va_arg(args, long));
62+
strcpy(p, s.c_str());
63+
p += s.length();
64+
}
65+
break;
66+
67+
case 'f': {
68+
float num = (float)va_arg(args, double);
69+
String s = String(num, 4);
70+
strcpy(p, s.c_str());
71+
p += s.length();
72+
}
73+
break;
74+
75+
default: {
76+
*p++ = *i;
77+
}
78+
break;
79+
}
80+
}
81+
82+
*p = '\0';
83+
return String(achBuffer);
84+
}

Software/Arduino code/OpenAstroTracker/b1_menu.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class LcdMenu {
157157
break;
158158
case 's': _lcd->print(va_arg(args, char*)); break;
159159
case 'd': _lcd->print((int)va_arg(args, int)); break;
160-
case 'f': _lcd->print((float)va_arg(args, float)); break;
160+
case 'f': _lcd->print((float)va_arg(args, double)); break;
161161
default: _lcd->print((char)*i); break;
162162
}
163163
}
@@ -173,10 +173,10 @@ class LcdMenu {
173173
{
174174
int spaces = _columns - line.length();
175175
for (int i = 0; i < line.length(); i++) {
176-
if (line[i] == ' > ') {
176+
if (line[i] == '>') {
177177
_lcd->write(_rightArrow);
178178
}
179-
else if (line[i] == ' < ') {
179+
else if (line[i] == '<') {
180180
_lcd->write(_leftArrow);
181181
}
182182
else {
@@ -187,6 +187,7 @@ class LcdMenu {
187187
// Clear the rest of the display
188188
while (spaces > 0) {
189189
_lcd->print(" ");
190+
spaces--;
190191
}
191192
}
192193

Software/Arduino code/OpenAstroTracker/b2_daytime.ino

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ class DayTime {
3535
return secs;
3636
}
3737

38+
float getTotalHours() {
39+
return 1.0f * getHours() + ((float)getMinutes() / 60.0f) + ((float)getSeconds() / 3600.0f);
40+
}
41+
42+
float getTotalMinutes() {
43+
return 60.0f * getHours() + (float)getMinutes() + ((float)getSeconds() / 60.0f);
44+
}
45+
46+
float getTotalSeconds() {
47+
return 3600.0f * getHours() + (float)getMinutes() * 60.0f + (float)getSeconds();
48+
}
49+
3850
int getTime(int& h, int& m, int& s) {
3951
h = hours;
4052
m = mins;

Software/Arduino code/OpenAstroTracker/b_setup.ino

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,13 @@ void setup() {
1010
Serial.begin(9600);
1111
//BT.begin(9600);
1212

13-
Serial.println("Hello World!");
14-
13+
log("Hello World!");
14+
1515
lcd.begin(16, 2);
1616
lcd.createChar(0, DEG);
1717
lcd.createChar(1, min);
1818
lcd.createChar(2, sec);
1919

20-
pinMode(A1, OUTPUT);
21-
pinMode(A2, OUTPUT);
22-
pinMode(A3, OUTPUT);
23-
pinMode(A4, OUTPUT);
24-
2520
stepperRA.setMaxSpeed(RAspeed);
2621
stepperRA.setAcceleration(RAacceleration);
2722
stepperDEC.setMaxSpeed(DECspeed);
@@ -33,6 +28,10 @@ void setup() {
3328

3429
inputcal = EEPROM.read(0);
3530
HATime = DayTime(EEPROM.read(1), EEPROM.read(2), 0);
31+
hourHA = HATime.getHours();
32+
minHA = HATime.getMinutes();
33+
34+
logv("HATime = %s oldHA = %d:%d",HATime.ToString().c_str(), hourHA, minHA);
3635

3736
lcdMenu.addItem("RAs", RA_Menu);
3837
lcdMenu.addItem("DEC", DEC_Menu);
@@ -44,5 +43,5 @@ void setup() {
4443
lcdMenu.addItem("CTRL", Control_Menu);
4544
lcdMenu.addItem("CAL", Calibration_Menu);
4645

47-
lcdMenu.setActive(HA_Menu);
46+
lcdMenu.setActive(RA_Menu);
4847
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
void handleDECandRACalculations()
2+
{
3+
float hourPos = RATime.getTotalHours();
4+
// Map [0 to 24] range to [-12 to +12] range
5+
if (hourPos > 12) {
6+
hourPos = hourPos - 24;
7+
}
8+
9+
// How many steps moves the RA ring one hour along (15degrees?)
10+
stepsPerHour = (float(RAsteps) / 288.0) * RevSteps;
11+
12+
// Where do ww want to move RA to?
13+
float moveRA = hourPos * stepsPerHour / 2;
14+
15+
// Where do we want to move DEC to?
16+
moveDEC = (degreeDEC * float(DECsteps) + minDEC * (float(DECsteps) / 60.0f) + secDEC * (float(DECsteps) / 3600.0f));
17+
18+
// We can move 6 hours in either direction. Outside of that we need to flip directions.
19+
float RALimit = (6.0f * stepsPerHour / 2);
20+
21+
// If we reach the limit in the positive direction ...
22+
if (moveRA > RALimit) {
23+
// ... turn both RA and DEC axis around
24+
float oldRA = moveRA;
25+
moveRA -= long(12.0f * stepsPerHour / 2);
26+
moveDEC = -moveDEC;
27+
//Serial.println(format("> %s HRPos: %f MoveRA: %f newMoveRA: %f RALimit: %f", RATime.ToString().c_str(), hourPos, oldRA, moveRA, RALimit));
28+
}
29+
// If we reach the limit in the negative direction...
30+
else if (moveRA < -RALimit) {
31+
// ... turn both RA and DEC axis around
32+
float oldRA = moveRA;
33+
moveRA += long(12.0f * stepsPerHour / 2);
34+
moveDEC = -moveDEC;
35+
//Serial.println(format("< %s HRPos: %f MoveRA: %f newMoveRA: %f RALimit: %f", RATime.ToString().c_str(), hourPos, oldRA, moveRA, RALimit));
36+
} else {
37+
//Serial.println(format("= %s HRPos: %f MoveRA: %f newMoveRA: %f RALimit: %f", RATime.ToString().c_str(), hourPos, moveRA, moveRA, RALimit));
38+
}
39+
40+
// Since the Control menu does it's own stepping, don't move the axes.
41+
if (lcdMenu.getActive() != Control_Menu) {
42+
stepperRA.moveTo(-moveRA);
43+
stepperDEC.moveTo(moveDEC);
44+
}
45+
}

0 commit comments

Comments
 (0)