summaryrefslogtreecommitdiff
path: root/n/asserv/src/asserv/pos.c
blob: b6bee63edfcfaeb0ab9938b66ec22fcb7129af25 (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
/* pos.c - Position motor control. */
/* asserv - 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.
 *
 * }}} */

/**
 * This file is responsible for position motor control.  The consign is a
 * position of the motor shafts, as theta/alpha.  Theta is the sum of right
 * and left position, alpha is the difference between the right and the left
 * position.
 * 16 bits are enough as long as there is no long blocking (see 2005 cup!).
 */

/** Current theta/alpha. */
uint32_t pos_theta_cur, pos_alpha_cur;
/** Consign theta/alpha. */
uint32_t pos_theta_cons, pos_alpha_cons;

/** Error saturation. */
int32_t pos_e_sat = 1023;
/** Integral saturation. */
int32_t pos_int_sat = 1023;
/** P coefficients. */
uint16_t pos_theta_kp = 1200, pos_alpha_kp = 1500;
/** I coefficients. */
uint16_t pos_theta_ki = 120, pos_alpha_ki = 150;
/** D coefficients. */
uint16_t pos_theta_kd = 2000, pos_alpha_kd = 2000;

/** Current integral values. */
int32_t pos_theta_int, pos_alpha_int;
/** Last error values. */
int32_t pos_theta_e_old, pos_alpha_e_old;

/* +AutoDec */
/* -AutoDec */

/** Compute a PID. */
static inline int16_t
pos_compute_pid (int32_t e, int32_t *i, int32_t *e_old,
		 uint16_t kp, uint16_t ki, uint16_t kd)
{
    int32_t diff, pid;
    /* Saturate error. */
    UTILS_BOUND (e, -pos_e_sat, pos_e_sat);
    /* Integral update. */
    *i += e;
    UTILS_BOUND (*i, -pos_int_sat, pos_int_sat);
    /* Differential value. */
    diff = e - *e_old;
    /* Compute PID. */
    pid = e * kp + *i * ki + diff * kd;
    /* Save result. */
    *e_old = e;
    return pid >> 12;
}

/** Update PWM according to consign. */
static void
pos_update (void)
{
    int16_t pid_theta, pid_alpha;
    /* Update current shaft positions. */
    pos_theta_cur += counter_left_diff + counter_right_diff;
    pos_alpha_cur += counter_right_diff - counter_left_diff;
    /* Compute PID. */
    pid_theta = pos_compute_pid (pos_theta_cons - pos_theta_cur,
				 &pos_theta_int, &pos_theta_e_old,
				 pos_theta_kp, pos_theta_ki, pos_theta_kd);
    pid_alpha = pos_compute_pid (pos_alpha_cons - pos_alpha_cur,
				 &pos_alpha_int, &pos_alpha_e_old,
				 pos_alpha_kp, pos_alpha_ki, pos_alpha_kd);
    /* Update PWM. */
    pwm_left = pid_theta - pid_alpha;
    UTILS_BOUND (pwm_left, -PWM_MAX, PWM_MAX);
    pwm_right = pid_theta + pid_alpha;
    UTILS_BOUND (pwm_right, -PWM_MAX, PWM_MAX);
}

/** Reset position control internal state. */
static void
pos_reset (void)
{
    pos_theta_int = pos_alpha_int = 0;
    pos_theta_cur = pos_alpha_cur = 0;
    pos_theta_cons = pos_alpha_cons = 0;
    pos_theta_e_old = pos_alpha_e_old = 0;
}