summaryrefslogtreecommitdiff
path: root/analog/motor-power-avr/src/main.c
blob: b839a72c8f92c5b8027f373b0eb78e47b6f3383c (plain)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* main.c */
/* mp - Position & speed motor control on AVR. {{{
 *
 * Copyright (C) 2005 Nicolas Schodet
 *
 * Robot APB Team/Efrei 2006.
 *        Web: http://assos.efrei.fr/robot/
 *      Email: robot AT efrei DOT fr
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */

#include "common.h"
#include "modules/uart/uart.h"
#include "modules/proto/proto.h"
#include "modules/utils/utils.h"
#include "modules/utils/utils.avr.h"
#include "modules/utils/byte.h"
#include "modules/math/fixed/fixed.h"
#include "modules/spi/spi.h"
#include "io.h"
#include "mp_pwm_LR_.h"
#include "mp_pwm_L_.h"
#include "mp_pwm_R_.h"

/* This is implementation include. */
#ifdef HOST
# include "simu.host.h"
#endif


#ifdef HOST
/** Simulation data. */
uint8_t main_simu, main_simu_cpt;
#endif /* HOST */

/** Record timer value at different stage of computing. Used for performance
 * analisys. */
uint8_t main_timer[6];

// Left channel variables
int8_t cmd_L_;
// Right channel variables
int8_t cmd_R_;

// Environnemental variables
uint8_t temperature, battery;

// current limitation values
uint8_t curLim_soft;
uint8_t curLim_temp;
uint8_t curLim_bat;

// environemental test settings
uint16_t envTest_cpt, envTest_period, envTest_autosend;

// current limit stats
uint8_t curLim_stat_cpt, curLim_stat_period;

/** Currently read SPI frame. */
uint8_t spi_frame[4];
/** Current size of the received frame. */
uint8_t spi_frame_size;


/* +AutoDec */

/** Main loop. */
void main_loop (void);

/** Just do a short flash on LEDs */
void led_flash(void);

/* -AutoDec */

/** Entry point. */
int
main (int argc, char **argv)
{
    avr_init (argc, argv);

    // Show that starts
    led_flash();

    /* Pull-ups. */
    //PORTA = 0xff;

    uart0_init ();
    spi_init (SPI_SLAVE, SPI_CPOL_FALLING | SPI_CPHA_SETUP, SPI_MSB_FIRST,
	      SPI_FOSC_DIV16);
    init_timer_LR_ ();
    init_curLim ();
    //postrack_init ();

    envTest_period = 200;
    proto_send0 ('z');
    sei ();

    // Background "task"
    while (1)
	    main_loop ();

    return 0;
}

/** Just do a short flash on LEDs */
void
led_flash(void)
{
    uint8_t ddrb_backup, portb_backup;

    // Save previous state
    ddrb_backup  = DDRB;
    portb_backup = PORTB;

    // Light up LEDs
    DDRB  = 0x0f;
    PORTB = 0x0f;

    // Delay 0.25s
    utils_delay(0.25);

    // Shut down LEDs
    DDRB  = 0x00;
    PORTB = 0x00;

    // Delay 0.25s
    utils_delay(0.25);

    // Restore previous state
    PORTB = portb_backup,
    DDRB  = ddrb_backup;

    return;
}

/** Main loop. */
void
main_loop (void)
{
    /* Uart */
    if (uart0_poll ())
      proto_accept (uart0_getc ());

    /* SPI. */
    if (SPSR & _BV (SPIF))
      {
	spi_frame[spi_frame_size++] = SPDR;
	if (spi_frame_size == 4)
	  {
	    /* Check integrity. */
	    if ((spi_frame[0] ^ spi_frame[1] ^ spi_frame[2] ^ spi_frame[3])
		== 0x42)
	      {
		uint8_t left_val = ((spi_frame[0] & 0x70) << 1) | (spi_frame[1] >> 3);
		uint8_t left_dir = (spi_frame[0] & 0x80) ? 1 : 0;
		if (left_dir)
		    left_val = -left_val;
		uint8_t right_val = ((spi_frame[0] & 0x07) << 5) | (spi_frame[2] >> 3);
		uint8_t right_dir = (spi_frame[0] & 0x08) ? 1 : 0;
		if (right_dir)
		    right_val = -right_val;
		start_motor_L_ (left_val, left_dir);
		start_motor_R_ (right_val, right_dir);
	      }
	    spi_frame_size = 0;
	  }
      }
    if (PINB & _BV (4))
      {
	spi_frame_size = 0;
      }

    /* Counter for launching environemental tests */
    if (!(envTest_cpt --)) {
      envTest_cpt = envTest_period;

      launch_envTest ();
      curLim_temp = get_curLim_temp (temperature);
      curLim_bat= get_curLim_bat (battery);
      update_curLim ();
    }
}

/** Handle incoming messages. */
void
proto_callback (uint8_t cmd, uint8_t size, uint8_t *args)
{
#define c(cmd, size) (cmd << 8 | size)
    switch (c (cmd, size))
      {
  case c ('z', 0):
	/* Reset. */
	stop_motor_L_ ();
	stop_motor_R_ ();
	utils_reset ();
	break;

  case c ('L', 0):
	/* Set high Z state for channel L */
	stop_motor_L_ ();
	break;

  case c ('l', 1):
	/* Set pwm value for _L_ side.
	 * - 0x00: 0% on duty cycle (brake state)
	 * - 0x7F: max on duty cycle (direction = 0)
	 * - 0x80: 0% on duty cycle (brake state)
	 * - 0xFF: max on duty cylcle (direction = 1) */
	cmd_L_ = (int8_t) args[0];
	if (cmd_L_ >= 0)
	  {
	    start_motor_L_ ((cmd_L_ << 1) , 0);
	  }
	else
	  {
	    start_motor_L_ ((cmd_L_ << 1), 1);
	  }
	break;

  case c ('R', 0):
	/* Set high Z state for channel R */
	stop_motor_R_ ();
	break;

  case c ('r', 1):
	/* Set pwm value for _R_ side.
	 * - 0x00: 0% on duty cycle (brake state)
	 * - 0x7F: max on duty cycle (direction = 0)
	 * - 0x80: 0% on duty cycle (brake state)
	 * - 0xFF: max on duty cylcle (direction = 1) */
	cmd_R_ = (int8_t) args[0];
	if (cmd_R_ >= 0)
	  {
	    start_motor_R_ ((cmd_R_ << 1) , 0);
	  }
	else
	  {
	    start_motor_R_ ((cmd_R_ << 1), 1);
	  }
	break;

  case c ('w', 0):
	/* Set zero pwm. */
    start_motor_L_ ( 0, 0);
    start_motor_R_ ( 0, 0);
	break;

  case c ('w', 4):
	/* Set pwm.
	 * - w: left pwm.
	 * - w: right pwm. */
    start_motor_L_ ( v8_to_v16 (args[0], args[1]) >> 7, (int8_t)args[0] < 0);
    start_motor_R_ ( v8_to_v16 (args[2], args[3]) >> 7, (int8_t)args[2] < 0);
	break;

  case c ('e', 0):
	/* Get environnemental test results */
	//TODO
	// envoyer surune seule ligne :
	// - valeur batterie
	// - valeur température
	// - curLim_bat
	// - curLim_temp
	// - curLim_soft
	// - curLim
	break;

  case c ('e', 1):
	/* Set environnemental test automatic sending 
	 * 0     : no automatic send
	 * other : automatic send */
	envTest_autosend = args[0];
	break;

  case c ('E', 1):
	/* Set environnemental test period */
	envTest_cpt = envTest_period = args[0];
	break;

  case c ('c', 0):
	/* Get current limit stat */
	//TODO 
	// envoyer le nombre d'IT overcurrent qu'il y a eu
	// depuis le dernier envoi de cette stat
	break;

  case c ('c', 1):
	/* Set current limit software value */
	setCurLim_soft (args[0]);
	break;

  case c ('C', 1):
	/* Set current limit stats period 
	 * Can be set to 0 for no automatic stat send */
	curLim_stat_cpt = curLim_stat_period = args[0];
	break;

	/*#ifdef HOST
	  case c ('Y', 1):
	// Simulation data.
	main_simu_cpt = main_simu = args[0];
	break;
#endif // HOST */
}
proto_send (cmd, size, args);
#undef c
}