-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoroLog.ino
227 lines (165 loc) · 4.35 KB
/
GoroLog.ino
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
/*
This example code is in the public domain.
*/
// estos vienen definidos en el master
#define LASTCHANNEL 8
#define LASTMOTOR 4
#include <SD.h>
#include "GoroLog.h"
#include "Logger.h"
#define PINLED 8 // PB0(ICP) - pata 14 del 328p
#define PINBOTON 7 // PD7(AIN1) - pata 13 del 328p
//#define BAUDIOS 115200 <--- a esta velocidad patina de vez en cuando
#define BAUDIOS 57600
// TODO : puede ser alverre estos dos:
#define ANALOG_BAT A1 // PC1(ADC1) - pata 24 del 328p
#define ANALOG_CUR A2 // PC2(ADC2) - pata 25 del 328p
// CONFIG
#define LOG_BATA 1
//#define SEND_STATUS 1
//#define DEBUG 1
// sensores
int estadoBoton;
int bat;
int cur;
// para el loop
unsigned long previous_time;
unsigned long current_time;
unsigned long delta_time;
byte frame_counter=0;
// objetos
Logger logger;
// para el dato del comando
binary_data_t bd;
// para contar lo ocupado
long veces_esperando;
void setup() {
//
veces_esperando=0;
// inicializo el serie
Serial.begin(BAUDIOS);
#ifdef DEBUG
Serial.println("M:Iniciando");
#endif
// initialize the digital pin as an output.
// pinMode(PINLED, OUTPUT);
// pinMode(PINBOTON, INPUT);
// inicializacion de sd y demás yerbas
logger.init(PINLED,PINBOTON);
// leo los primeros valores analógicos (TODO: para que?)
bat=analogRead(ANALOG_BAT);
cur=analogRead(ANALOG_CUR);
// lo último a setupear es previous_time porque lo siguiente es el loop
previous_time=millis();
}
void loop() {
// leemos el serie
int c = lee_byte_serie();
#ifdef DEBUG
Serial.print("COMANDO:");
Serial.write(c);
Serial.println();
#endif
// interpretacion del comando
switch ( c ) {
case GOROL_COMPASS: // datos del magnetometro
lee_bytes(sizeof(compass_data_t));
logger.log_compass(&(bd.compass));
break;
case GOROL_MOTORES_ARMADOS:
//motores armados
logger.motores_armados();
break;
case GOROL_MOTORES_DESARMADOS:
//motores desarmados
logger.motores_desarmados();
break;
case GOROL_QUERY_ESTADO:
Serial.write(logger.estado());
break;
case GOROL_UPTIME:
lee_bytes(4);
logger.log_uptime(bd.largo_sin_signo);
break;
case GOROL_GPS:
lee_bytes(sizeof(gps_data_t));
logger.log_gps(&(bd.myGps));
break;
default: //comando no interpretado
inner_loop();
}
}
void lee_bytes(const uint8_t cantidad) { //FIXME: no limito la cantidad
uint8_t offset=0;
#ifdef DEBUG
Serial.print("POR LEER ");
Serial.print(cantidad,DEC);
Serial.println(" BYTES");
#endif
// mientas tenga datos por leer, leo
while ( cantidad > offset ) {
bd.dato[offset]=lee_byte_serie();
offset++;
}
#ifdef DEBUG
Serial.println("LEIDOS");
#endif
}
int lee_byte_serie() {
int c = Serial.read();
veces_esperando--;
// para asegurarme que aunque saturado, genera log igual
inner_loop();
while ( c==-1 ) {
veces_esperando++;
inner_loop();
c = Serial.read();
}
return c;
}
void inner_loop() {
// loops
current_time = millis();
delta_time = current_time - previous_time;
// main loop in 8Hz
if ( delta_time > 125 ) {
//previous_time=previous_time+125;
previous_time = current_time;
// ciclo de 8Hz --------------------------
{
logger.log_veces_esperando(veces_esperando);
veces_esperando=0;
logger.check_button();
logger.actualiza_led(frame_counter);
#ifdef LOG_BATA
// levanto el estado de bat
bat=analogRead(ANALOG_BAT);
cur=analogRead(ANALOG_CUR);
// y logeo
logger.log_bat(bat,cur, frame_counter); //ToDo: pasar punteros?
#endif
}
// ciclo de 4Hz --------------------------
if ( frame_counter % 2 == 0 ) {
// logger.change_led(frame_counter>>1);
}
// ciclo de 2Hz --------------------------
if ( frame_counter % 4 == 0 ) {
//
}
// ciclo de 1Hz --------------------------
if ( frame_counter % 8 == 0 ) {
#ifdef SEND_STATUS
// le envio al shield el estado
Serial.write(logger.estado());
#endif
}
// actualiza el contador de frames
if ( frame_counter == 7 ) {
frame_counter=0;
}
else {
frame_counter++;
}
}
}