summaryrefslogtreecommitdiff
path: root/n/line-follower/src/speed.c
blob: cafd446bc31c280f7bd5fe9bbd230e8b9aacac03 (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
/* speed.c - PI speed control. */
/* asserv - Pos ition & speed motor control on a ATmega128. {{{
 *
 * Copyright (C) 2004 Nicolas Schodet
 *
 * Robot APB Team/Efrei 2005.
 *        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 "linesensor.c"

/** Statistics about pid components. */
extern int16_t pid_pid,pid_p,pid_i,pid_d

/** Maximum value the pwm can reach (defined by the avr configuration). */
#define PWM_MAX 255
/** Maximum value for the pwm in linefol mode (controls avg speed). */
int16_t linefol_max_pwm = 80;
/** Attenuation for filtering the derivate term (f88). */
uint16_t K_ATT_LINEPOS_DER = 0x00FE; // ~= 95%
/** Table and index for calculating the derivate term. */
#define N_LINEPOS_DER_TAB 8 // max : 255 (8bit-addressing)
int16_t linepos_der_tab[N_LINEPOS_DER_TAB];
uint8_t linepos_der_index;

/** Actual speed. */
int8_t speed_left, speed_right;
/** Wanted speed. */
int8_t speed_left_aim, speed_right_aim;
/** Acceleration value. */
uint8_t speed_acc = 8;
/** Acceleration counter, speed gets updated when it reachs 0. */
uint8_t speed_acc_cpt;
/** Integral term. */
int16_t speed_left_int, speed_right_int, linepos_int;
/** Integral max value. */
int16_t speed_int_max = 1024;
int16_t linepos_int_max = 1024;
/** Last error value. */
int16_t speed_left_e_old, speed_right_e_old, e_old;
/** P coeficients. 5.8 fixed point format. */
uint16_t speed_kp = 2 * 255;
/** I coeficients. 4.8 fixed point format. */
uint16_t speed_ki = 1 * 255;
/** D coeficient. 4.8 fixed point format. */
uint16_t speed_kd = 1 * 255;


/* +AutoDec */

/** Update speeds according to the wanted speeds and the acceleration. */
static inline void
speed_update (void);

/** Initialise speed parameters. */
static inline void
speed_init (void);

/** Initialize the line sensor parameters. */
static inline void
linesensor_reset (void);

/** Update speeds according to the wanted speeds and the acceleration. */
static inline void
speed_update (void);

/** Compute new pwm value for motors in linefol mode. */
static inline void
speed_compute_linefol_pwm (void);

/** Compute new pwm value for left motor. */
static inline void
speed_compute_left_pwm (void);

/** Compute new pwm value for right motor. */
static inline void
speed_compute_right_pwm (void);

/** Forget past event, usefull when the speed control is disabled for some
 * time. */
static inline void
speed_restart (void);

/* -AutoDec */


/** Initialise speed parameters. */
static inline void
speed_init (void)
{
    speed_acc = 8;
    speed_int_max = 1024;
    speed_kp = 2 * 255;
    speed_ki = 1 * 255;
}

/** Initialize the line sensor parameters. */
static inline void
linesensor_reset (void)
{
    uint8_t i;
    
    for(i=0;i<N_LINEPOS_DER_TAB;i++)
	linepos_der_tab[i]=0;
    
    linepos_der_index=0;
    e_old=0;
    linepos_int=0;
}


/** Update speeds according to the wanted speeds and the acceleration. */
static inline void
speed_update (void)
{
    if (speed_acc)
      {
	speed_acc_cpt--;
	if (speed_acc_cpt == 0)
	  {
	    speed_acc_cpt = speed_acc;
	    /* Update speeds. */
	    if (speed_left > speed_left_aim)
		speed_left--;
	    else if (speed_left < speed_left_aim)
		speed_left++;
	    if (speed_right > speed_right_aim)
		speed_right--;
	    else if (speed_right < speed_right_aim)
		speed_right++;
	  }
      }
    else
      {
	speed_left = speed_left_aim;
	speed_right = speed_right_aim;
      }
}
/** Compute new pwm value for motors in linefol mode. */
static inline void
speed_compute_linefol_pwm (void)
{
    int16_t e, linepos_der;
    uint8_t i;
    int16_t pwm;
    // reusing left channel variables

    e = linepos;					/* 10b = 8b + 9b */
    
    /* Derivative update. */
    i=linepos_der_index;				// old index
    linepos_der_index=					// updating new index 
	(linepos_der_index+N_LINEPOS_DER_TAB-1)		// modulo adressing
	& (N_LINEPOS_DER_TAB-1);
    linepos_der_tab[linepos_der_index] = 		// new der value
	dsp_mul_i16f88 ((linepos_der_tab[i] + e - e_old),
			K_ATT_LINEPOS_DER);

    linepos_der = 0;
    for(i=0 ; i < N_LINEPOS_DER_TAB ; i++)		// calculating sum
	linepos_der += linepos_der_tab[i];
    
     /* Integral update (reusing left channel variables). */
    linepos_int += e;				/* 12b = 11b + 10b */
    if (linepos_int > linepos_int_max)			/* 11b */
	linepos_int = linepos_int_max;
    else if (linepos_int < -linepos_int_max)
	linepos_int = -linepos_int_max;

    /* Compute PI. */					/* 16b = 15b + 15b */
    pwm = dsp_mul_i16f88 (e, speed_kp)			/* 15b = 10b * 5.8b */
	+ dsp_mul_i16f88 (linepos_int, speed_ki)	/* 15b = 11b * 4.8b */
	+ dsp_mul_i16f88 (linepos_der, speed_kd);	/* 15b = 11b * 4.8b */
    
    pid_p = e;
    pid_i = linepos_int;
    pid_d = linepos_der;
    pid_pid = pwm;
    
    /* Save result. */
    e_old = e;
    if(pwm > 0)
      {
	pwm_left = linefol_max_pwm * (PWM_MAX - pwm);
	pwm_right = linefol_max_pwm * (PWM_MAX);
      }
    else
      {
	pwm_left = linefol_max_pwm * PWM_MAX;
	pwm_right = linefol_max_pwm * (PWM_MAX - pwm);
      }
}


/** Compute new pwm value for left motor. */
static inline void
speed_compute_left_pwm (void)
{
    int16_t e;
    int16_t pwm;
    e = speed_left - counter_left_diff;			/* 10b = 8b + 9b */
    /* Integral update. */
    speed_left_int += e;				/* 12b = 11b + 10b */
    if (speed_left_int > speed_int_max)			/* 11b */
	speed_left_int = speed_int_max;
    else if (speed_left_int < -speed_int_max)
	speed_left_int = -speed_int_max;
    /* Compute PI. */					/* 16b = 15b + 15b */
    pwm = dsp_mul_i16f88 (e, speed_kp)			/* 15b = 10b * 5.8b */
	+ dsp_mul_i16f88 (speed_left_int, speed_ki);	/* 15b = 11b * 4.8b */
    /* Save result. */
    speed_left_e_old = e;
    pwm_left = pwm;
}

/** Compute new pwm value for right motor. */
static inline void
speed_compute_right_pwm (void)
{
    int16_t e;
    int16_t pwm;
    e = speed_right - counter_right_diff;
    /* Integral update. */
    speed_right_int += e;
    if (speed_right_int > speed_int_max)
	speed_right_int = speed_int_max;
    else if (speed_right_int < -speed_int_max)
	speed_right_int = -speed_int_max;
    /* Compute PI. */
    pwm = dsp_mul_i16f88 (e, speed_kp)
	+ dsp_mul_i16f88 (speed_right_int, speed_ki);
    /* Save result. */
    speed_right_e_old = e;
    pwm_right = pwm;
}

/** Forget past event, usefull when the speed control is disabled for some
 * time. */
static inline void
speed_restart (void)
{
    speed_left_int = 0;
    speed_right_int = 0;
    speed_left = 0;
    speed_right = 0;
}