summaryrefslogtreecommitdiffhomepage
path: root/analog/motor-power-avr/src/main.c
blob: 466bff91ea1c0b328ad76954554067d869c3f460 (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
/* 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/byte.h"
#include "modules/math/fixed/fixed.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;

/* +AutoDec */

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

/* -AutoDec */

/** Entry point. */
int
main (int argc, char **argv)
{
    avr_init (argc, argv);
    DDRD = 0x60;
    /* Pull-ups. */
    PORTA = 0xff;
    PORTC = 0xff;
    init_timer_LR_ ();
    init_curLim ();
    //uart0_init ();
    //postrack_init ();
    envTest_period = 200;
    proto_send0 ('z');
    sei ();
    while (1)
	main_loop ();
    return 0;
}

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

    /* 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;
	/* Commands. */
	/*
	   case c ('R', 0):
	// Set high Z state for channel R 
	stop_motor_R_ ();
	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.
	 * - 0x80: -max on duty cycle (direction = 0)
	 * - 0x00: 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_ , 0);
	  }
	else
	  {
	    start_motor_L_ (-cmd_L_ , 1);
	  }
	break;

	//case c ('r', 1):
	/* Set pwm value for _R_ side.
	 * - 0x80: -max on duty cycle (direction = 0)
	 * - 0x00: 0% on duty cycle (brake state)
	 * TODO : recopier ce qui est fait pour le cote _L_
	 }
	 */
	//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 */
	//TODO 
	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
}