summaryrefslogtreecommitdiff
path: root/digital/avr/modules/motor/pos_control
diff options
context:
space:
mode:
Diffstat (limited to 'digital/avr/modules/motor/pos_control')
-rw-r--r--digital/avr/modules/motor/pos_control/Makefile5
-rw-r--r--digital/avr/modules/motor/pos_control/Makefile.module1
-rw-r--r--digital/avr/modules/motor/pos_control/pos_control.c103
-rw-r--r--digital/avr/modules/motor/pos_control/pos_control.h60
-rw-r--r--digital/avr/modules/motor/pos_control/pos_control.txt32
5 files changed, 201 insertions, 0 deletions
diff --git a/digital/avr/modules/motor/pos_control/Makefile b/digital/avr/modules/motor/pos_control/Makefile
new file mode 100644
index 00000000..439f8adc
--- /dev/null
+++ b/digital/avr/modules/motor/pos_control/Makefile
@@ -0,0 +1,5 @@
+BASE = ../../..
+DOC = pos_control.html
+EXTRACTDOC = pos_control.h
+
+include $(BASE)/make/Makefile.gen
diff --git a/digital/avr/modules/motor/pos_control/Makefile.module b/digital/avr/modules/motor/pos_control/Makefile.module
new file mode 100644
index 00000000..cc290a21
--- /dev/null
+++ b/digital/avr/modules/motor/pos_control/Makefile.module
@@ -0,0 +1 @@
+motor_pos_control_SOURCES = pos_control.c
diff --git a/digital/avr/modules/motor/pos_control/pos_control.c b/digital/avr/modules/motor/pos_control/pos_control.c
new file mode 100644
index 00000000..80caae74
--- /dev/null
+++ b/digital/avr/modules/motor/pos_control/pos_control.c
@@ -0,0 +1,103 @@
+/* pos_control.c */
+/* motor - Motor control module. {{{
+ *
+ * Copyright (C) 2011 Nicolas Schodet
+ *
+ * APBTeam:
+ * Web: http://apbteam.org/
+ * Email: team AT apbteam DOT org
+ *
+ * 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 "pos_control.h"
+
+#include "modules/utils/utils.h"
+
+/** Compute a PID.
+ * How to compute maximum numbers size:
+ * Result is 24 bits (16 bits kept after shift).
+ * If e_sat == 1023, e max is 11 bits (do not forget the sign bit), and diff
+ * max is 12 bits (can be saturated with d_sat).
+ * If i_sat == 1023, i max is 11 bits.
+ * In the final addition, let's give 23 bits to the p part, and 22 bits to the
+ * i and d part (23b + 22b + 22b => 23b + 23b => 24b).
+ * Therefore, kp can be 23 - 11 = 12 bits (f4.8).
+ * ki can be 22 - 11 = 11 bits (f3.8).
+ * kd can be 22 - 12 = 10 bits (f2.8).
+ * How to increase this number:
+ * - lower the shift.
+ * - bound the value returned.
+ * - lower e, i & d saturation. */
+static inline int16_t
+pos_control_compute_pid (struct pos_control_t *pos_control, int32_t e)
+{
+ int32_t diff, pid;
+ /* Saturate error. */
+ UTILS_BOUND (e, -pos_control->e_sat, pos_control->e_sat);
+ /* Integral update. */
+ pos_control->i += e;
+ UTILS_BOUND (pos_control->i, -pos_control->i_sat, pos_control->i_sat);
+ /* Differential value. */
+ diff = e - pos_control->last_error;
+ UTILS_BOUND (diff, -pos_control->d_sat, pos_control->d_sat);
+ /* Compute PID. */
+ pid = e * pos_control->kp + pos_control->i * pos_control->ki
+ + diff * pos_control->kd;
+ /* Save result. */
+ pos_control->last_error = e;
+ return pid >> 8;
+}
+
+/** Reset position control so that it does not go crazy when enabled. */
+static void
+pos_control_reset (pos_control_t *pos_control)
+{
+ pos_control->cons = pos_control->cur;
+ pos_control->i = 0;
+ pos_control->last_error = 0;
+}
+
+void
+pos_control_init (pos_control_t *pos_control)
+{
+ pos_control_reset (pos_control);
+}
+
+int16_t
+pos_control_update (pos_control_t *pos_control, int16_t cur_speed,
+ uint8_t enabled)
+{
+ int32_t error;
+ int16_t pid;
+ /* Update current shaft position. */
+ pos_control->cur += cur_speed;
+ /* Control position if enabled. */
+ if (enabled)
+ {
+ /* Compute error. */
+ error = pos_control->cons - pos_control->cur;
+ /* Compute PID. */
+ pid = pos_control_compute_pid (pos_control, error);
+ }
+ else
+ {
+ pos_control_reset (pos_control);
+ pid = 0;
+ }
+ return pid;
+}
+
diff --git a/digital/avr/modules/motor/pos_control/pos_control.h b/digital/avr/modules/motor/pos_control/pos_control.h
new file mode 100644
index 00000000..1378f3ad
--- /dev/null
+++ b/digital/avr/modules/motor/pos_control/pos_control.h
@@ -0,0 +1,60 @@
+#ifndef pos_control_h
+#define pos_control_h
+/* pos_control.h */
+/* motor - Motor control module. {{{
+ *
+ * Copyright (C) 2011 Nicolas Schodet
+ *
+ * APBTeam:
+ * Web: http://apbteam.org/
+ * Email: team AT apbteam DOT org
+ *
+ * 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.
+ *
+ * }}} */
+
+/** Position control state. */
+struct pos_control_t
+{
+ /** Current position. */
+ uint32_t cur;
+ /** Consign position. */
+ uint32_t cons;
+ /** PID coefficients (f8.8, maximum depends on saturation values). */
+ uint16_t kp, ki, kd;
+ /** Error saturation. */
+ int32_t e_sat;
+ /** Integral saturation. */
+ int32_t i_sat;
+ /** Differential saturation. */
+ int32_t d_sat;
+ /** Current integral value. */
+ int32_t i;
+ /** Last error value. */
+ int32_t last_error;
+};
+typedef struct pos_control_t pos_control_t;
+
+/** Initialise position control. */
+void
+pos_control_init (pos_control_t *pos_control);
+
+/** Control position of system, take current speed as input, return the new
+ * command. */
+int16_t
+pos_control_update (pos_control_t *pos_control, int16_t cur_speed,
+ uint8_t enabled);
+
+#endif /* pos_control_h */
diff --git a/digital/avr/modules/motor/pos_control/pos_control.txt b/digital/avr/modules/motor/pos_control/pos_control.txt
new file mode 100644
index 00000000..d1c1d03f
--- /dev/null
+++ b/digital/avr/modules/motor/pos_control/pos_control.txt
@@ -0,0 +1,32 @@
+==========================
+ motor/pos_control module
+==========================
+:Author: Nicolas Schodet
+
+Introduction
+============
+
+This module provides position control, usually for a DC motor. Given a
+position consign, it will adjust output power so that the measured position
+matches the consign. There is no speed control, this is handled by another
+module.
+
+Usage
+=====
+
+The `pos_control_t` structure contains both current control state and control
+parameters. The `kp`, `kd` and `ki` parameters are the PID coefficients,
+using a fixed point representation. The `e_sat`, `i_sat` and `d_sat` are the
+maximum values for error, integral and differential values. They must be
+adapted so that PID computation does not overflow. See the
+`pos_control_compute_pid` function comments for guidelines to choose those
+values.
+
+The `cur` field is updated by this module with the current position. The
+`cons` field must be written by user to set the position consign. This module
+will try to make the current position matches the consign position.
+
+API
+===
+
+.. include:: pos_control.exd