summaryrefslogtreecommitdiff
path: root/n/asserv/src/asserv/pos.c
diff options
context:
space:
mode:
authorschodet2005-11-06 18:01:08 +0000
committerschodet2005-11-06 18:01:08 +0000
commit0cc8897a51d07baaf42db53f70c9c9aac5272b86 (patch)
treecfe1442247d4af71cb71e8299ec3f4f6cd48cc5c /n/asserv/src/asserv/pos.c
parent170c552ebdc180e3a231231d5851909fd29f6f39 (diff)
Première version du programme asserv.
Diffstat (limited to 'n/asserv/src/asserv/pos.c')
-rw-r--r--n/asserv/src/asserv/pos.c104
1 files changed, 104 insertions, 0 deletions
diff --git a/n/asserv/src/asserv/pos.c b/n/asserv/src/asserv/pos.c
new file mode 100644
index 0000000..0a1af23
--- /dev/null
+++ b/n/asserv/src/asserv/pos.c
@@ -0,0 +1,104 @@
+/* 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. */
+uint16_t pos_theta_cur, pos_alpha_cur;
+/** Consign theta/alpha. */
+uint16_t pos_theta_cons, pos_alpha_cons;
+
+/** Error saturation. */
+int16_t pos_e_sat = 1023;
+/** Integral saturation. */
+int16_t pos_int_sat = 8191;
+/** P coefficients. */
+uint16_t pos_theta_kp = 0, pos_alpha_kp = 0;
+/** I coefficients. */
+uint16_t pos_theta_ki = 0, pos_alpha_ki = 0;
+/** D coefficients. */
+uint16_t pos_theta_kd = 0, pos_alpha_kd = 0;
+/** Current integral values. */
+int16_t pos_theta_int, pos_alpha_int;
+/** Last error values. */
+int16_t pos_theta_e_old, pos_alpha_e_old;
+
+/* +AutoDec */
+/* -AutoDec */
+
+/** Compute a PID. */
+static inline int16_t
+pos_compute_pid (int16_t e, int16_t *i, int16_t *e_old,
+ uint16_t kp, uint16_t ki, uint16_t kd)
+{
+ int16_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;
+}
+
+/** 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;
+}