summaryrefslogtreecommitdiff
path: root/n/asserv/src/pwm.c
diff options
context:
space:
mode:
authorschodet2004-10-13 00:12:18 +0000
committerschodet2004-10-13 00:12:18 +0000
commit7868feb67d7c2d1414cbcd84b6f02a7305a65117 (patch)
tree35826ef1077ff133862b295f93242f89e4b7dc68 /n/asserv/src/pwm.c
parentfdd6216054017f4aded7501d4a6e725710438a35 (diff)
Version toute nouvelle qu'elle est bien et bien organisée.
Gestion du mode pwm simple. Gestion de la virgule fixe dans les coefs. Préparation pour les améliorations futures. Séparation dans des fichiers différents. Netoyage.
Diffstat (limited to 'n/asserv/src/pwm.c')
-rw-r--r--n/asserv/src/pwm.c77
1 files changed, 46 insertions, 31 deletions
diff --git a/n/asserv/src/pwm.c b/n/asserv/src/pwm.c
index 84791d4..7979842 100644
--- a/n/asserv/src/pwm.c
+++ b/n/asserv/src/pwm.c
@@ -1,8 +1,12 @@
/* pwm.c */
-/* {{{
+/* asserv - Position & 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
@@ -17,17 +21,29 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
- * Contact :
- * Web: http://perso.efrei.fr/~schodet/
- * Email: <contact@ni.fr.eu.org>
* }}} */
-#include "pwm.h"
-#include <avr/io.h>
+
+/* PWM values. */
+int16_t pwm_left, pwm_right;
/* +AutoDec */
+
+/** Initialise PWM generator. */
+static inline void
+pwm_init (void);
+
+/** Preprocess a PWM value before sending it to hardware. */
+static inline uint8_t
+pwm_preproc (uint16_t v);
+
+/** Update the hardware PWM values. */
+static inline void
+pwm_update (void);
+
/* -AutoDec */
-inline void
+/** Initialise PWM generator. */
+static inline void
pwm_init (void)
{
/* No timer/counter interrupt. */
@@ -42,51 +58,50 @@ pwm_init (void)
DDRB |= _BV (7) | _BV (6) | _BV (3) | _BV (2);
}
-static inline int16_t
-pwm_preproc (int16_t v)
+/** Preprocess a PWM value before sending it to hardware. */
+static inline uint8_t
+pwm_preproc (uint16_t v)
{
- v = (v >> 2) + ((v >> 1) & 1);
- /*if (v > 0)
- v += 0x0f;
- else if (v < 0)
- v -= 0x0f;*/
+ v += 0x10;
if (v > 255)
return 255;
- else if (v < -255)
- return -255;
else
return v;
}
-inline void
-pwm_set_left (int16_t v)
+/** Update the hardware PWM values. */
+static inline void
+pwm_update (void)
{
- v = pwm_preproc (v);
- if (v < 0)
+ /* Set left PWM. */
+ if (pwm_left == 0)
+ {
+ OCR1B = 0;
+ }
+ else if (pwm_left < 0)
{
PORTB &= ~_BV (2);
- OCR1B = -v;
+ OCR1B = pwm_preproc (-pwm_left);
}
else
{
PORTB |= _BV (2);
- OCR1B = v;
+ OCR1B = pwm_preproc (pwm_left);
}
-}
-
-inline void
-pwm_set_right (int16_t v)
-{
- v = pwm_preproc (v);
- if (v < 0)
+ /* Set right PWM. */
+ if (pwm_right == 0)
+ {
+ OCR1C = 0;
+ }
+ else if (pwm_right < 0)
{
PORTB |= _BV (3);
- OCR1C = -v;
+ OCR1C = pwm_preproc (-pwm_right);
}
else
{
PORTB &= ~_BV (3);
- OCR1C = v;
+ OCR1C = pwm_preproc (pwm_right);
}
}