-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
255 lines (182 loc) · 5.47 KB
/
main.c
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <msp430.h>
#include <stdint.h>
#include <stdlib.h>
#include <headers/headers.h>
int spa_update();
void interrupt_handler();
#define CALCULATION_DELTA_T_DAY_MODE 60
#define CALCULATION_DELTA_T_BEFORE_SUNRISE 1800
#define CALCULATION_DELTA_T_NIGHT_MODE 7200
#define CALCULATION_SUNRISE_MODE_FACTOR 1.1
/*
* Stores the time and date
* that is returned by rtc_get_time_date()
*/
static uint8_t time_date_array[7] = {};
/*
* SPA structure that holds the angular data
* */
static spa_data spa;
/*
* Handles actions after interrupt, see ISR
* */
static uint8_t interruptAction = 0;
/*
* Defines the second distance between spa calculations
*
* Is changed after sunset to go into "sleep"
* */
static uint16_t calculation_delta_t = 15;
/*
* M A I N
* */
int main(void)
{
// Stop watchdog timer
WDTCTL = WDTPW | WDTHOLD;
// Global interrupt enable
__bis_SR_register(GIE);
/*
* Initialization
* */
clock_init();
port_init();
i2c_init();
rtc_init();
stepper_init();
/* Comment this line if you want to
* set the date/time using button P1.1
* (port1_isr())
* */
RTC_TIMESET_IE &= ~RTC_TIMESET_BIT;
// wait for a bit to settle
for(int i = 0; i < 100; i++) {
_delay_cycles(60000);
}
/*
* Main Loop
* */
while(1) {
interrupt_handler();
LPM3;
}
}
/*
* on-board push button P1.1 to set the current time and date
*
* */
#pragma vector=PORT1_VECTOR
__interrupt void port1_isr() {
// check which pin caused the interrupt
if( P1IV == 0x04 ) { // RTC_TIMESET Switch (P1.1)
if(MOTOR_NOT_RUNNING) {
interruptAction = 1;
// reset IFG
P1IFG &= ~RTC_TIMESET_BIT;
LPM3_EXIT;
}
}
}
/*
* Handles interrupts from limit switches
* and home button as well as second counter
* */
#pragma vector=PORT2_VECTOR
__interrupt void port2_isr() {
uint16_t interrupt_vector_value = P2IV;
if(interrupt_vector_value == 0x10) { // every second
static uint16_t second_counter = 0;
if(MOTOR_NOT_RUNNING) {
second_counter++;
if(second_counter == calculation_delta_t) {
interruptAction = 2;
second_counter = 0;
LPM3_EXIT;
}
}
} else if(interrupt_vector_value == 0x08) { // P2.3 --> AZM Limit Switch
azm_home_switch_isrAction();
LPM3_EXIT;
} else if(interrupt_vector_value == 0x0E) { // P2.6 --> ELV Limit Switch
elv_home_switch_isrAction();
LPM3_EXIT;
} else if(interrupt_vector_value == 0x04) { // P2.1 --> on-board home button
home_button_isrAction();
}
}
/*
* Handles the actions that happen in the main()-loop on interrupt
* based on value of interruptAction
*
* */
void interrupt_handler() {
static uint8_t sleep_flag = 1;
switch(interruptAction) {
case 1: // P1.1 Switch, set time/date of RTC module
// year, month, day, hour, minute, second
rtc_set_time_date(20, 8, 21, 14, 13, 40);
rtc_get_time_date(time_date_array);
break;
case 2: // second_counter reached calculation_delta_t
CALC_LED_OUT |= CALC_LED_BIT;
spa_update();
spa_calculate(&spa);
if(spa.zenith < (90 - ELV_SUNRISE_ANGLE)) {
// Track mode
if(sleep_flag == 1) {
home_azm();
home_elv();
calculation_delta_t = CALCULATION_DELTA_T_DAY_MODE;
sleep_flag = 0;
}
elv_set_angle(90 - spa.zenith);
azm_set_angle(spa.azimuth - 90);
} else {
// Sleep mode
home_azm();
home_elv();
STEPPER_AZM_SLEEP;
STEPPER_ELV_SLEEP;
sleep_flag = 1;
calculation_delta_t = CALCULATION_DELTA_T_NIGHT_MODE;
}
CALC_LED_OUT &= ~CALC_LED_BIT;
break;
}
interruptAction = 0;
}
/*
* Initializes SPA structure
* Has to go after rtc_init, i2c_init, etc
*
* */
int spa_update() {
rtc_get_time_date(time_date_array);
// check format of time & date
if(
(time_date_array[0] > 60) ||
(time_date_array[1] > 60) ||
(time_date_array[2] > 24) ||
(time_date_array[4] > 31) || (time_date_array[4] == 0) ||
(time_date_array[5] > 12) || (time_date_array[5] == 0) ||
(time_date_array[6] > 99)
) return 1;
spa.year = 2000 + time_date_array[6];
spa.month = time_date_array[5];
spa.day = time_date_array[4];
spa.hour = time_date_array[2];
spa.minute = time_date_array[1];
spa.second = time_date_array[0];
spa.timezone = 2;
spa.delta_ut1 = 0;
spa.delta_t = delta_t_polynomial(spa.year-2000);
spa.longitude = 11.66474;
spa.latitude = 54.02720;
spa.elevation = 10;
spa.pressure = 1015;
spa.temperature = 15;
spa.slope = 0;
spa.azm_rotation = 0; // ?
spa.atmos_refract = 0.5667;
spa.function = SPA_ZA;
}