From 11dcf9a380ac98b8cb9f25c61494b9ffba5780c6 Mon Sep 17 00:00:00 2001 From: Guillaume Chevillot Date: Thu, 6 Mar 2008 09:32:45 +0100 Subject: Add support of right side --- analog/motor-power-avr/src/Makefile | 6 +- analog/motor-power-avr/src/main.c | 14 +-- analog/motor-power-avr/src/mp_pwm_LR_.c | 3 +- analog/motor-power-avr/src/mp_pwm_LR_.h | 8 +- analog/motor-power-avr/src/mp_pwm_L_.c | 4 +- analog/motor-power-avr/src/mp_pwm_R_.c | 206 ++++++++++++++++++++++++++++++++ analog/motor-power-avr/src/mp_pwm_R_.h | 37 ++++++ 7 files changed, 257 insertions(+), 21 deletions(-) create mode 100644 analog/motor-power-avr/src/mp_pwm_R_.c create mode 100644 analog/motor-power-avr/src/mp_pwm_R_.h (limited to 'analog/motor-power-avr/src') diff --git a/analog/motor-power-avr/src/Makefile b/analog/motor-power-avr/src/Makefile index 543bf020..37e67d18 100644 --- a/analog/motor-power-avr/src/Makefile +++ b/analog/motor-power-avr/src/Makefile @@ -1,7 +1,7 @@ BASE = ../../../digital/avr PROGS = mp HOST_PROGS = test_mp -mp_SOURCES = main.c mp_pwm_L_.c mp_pwm_LR_.c adc.c +mp_SOURCES = main.c mp_pwm_L_.c mp_pwm_R_.c mp_pwm_LR_.c adc.c MODULES = adc proto uart utils math/fixed CONFIGFILE = avrconfig.h # atmega8, atmega8535, atmega128... @@ -12,3 +12,7 @@ OPTIMIZE = -O2 HOST_LIBS = -lm include $(BASE)/make/Makefile.gen + +src: + sed -e 's/_L_/_R_/g' mp_pwm_L_.c > mp_pwm_R_.c + sed -e 's/_L_/_R_/g' mp_pwm_L_.h > mp_pwm_R_.h diff --git a/analog/motor-power-avr/src/main.c b/analog/motor-power-avr/src/main.c index b5e24028..10e26830 100644 --- a/analog/motor-power-avr/src/main.c +++ b/analog/motor-power-avr/src/main.c @@ -33,7 +33,7 @@ #include "io.h" #include "mp_pwm_LR_.h" #include "mp_pwm_L_.h" -//#include "mp_pwm_R_.h" +#include "mp_pwm_R_.h" /* This is implementation include. */ #ifdef HOST @@ -88,7 +88,6 @@ main (int argc, char **argv) // Show that starts led_flash(); - DDRB |= 0x0c; //-- /* Pull-ups. */ //PORTA = 0xff; @@ -253,14 +252,3 @@ proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) proto_send (cmd, size, args); #undef c } - -/** Defines empty vector for timer 2 until R pwm file exists */ - -//-- -// To remove when R side file will be generated -EMPTY_INTERRUPT(R_OVF_vect) - -//-- -// To remove when R side file will be generated -EMPTY_INTERRUPT(R_COMP_vect) - diff --git a/analog/motor-power-avr/src/mp_pwm_LR_.c b/analog/motor-power-avr/src/mp_pwm_LR_.c index 545aea38..e3ec252c 100644 --- a/analog/motor-power-avr/src/mp_pwm_LR_.c +++ b/analog/motor-power-avr/src/mp_pwm_LR_.c @@ -1,6 +1,7 @@ #include "common.h" #include "mp_pwm_LR_.h" #include "mp_pwm_L_.h" +#include "mp_pwm_R_.h" // Variables for current limitation static uint8_t curLim_temp; @@ -14,7 +15,7 @@ static uint8_t curLim_soft; void init_timer_LR_(void) { init_pwm_L_(); - //init_pwm_R_(); + init_pwm_R_(); TCNT_L_ = 0x00; TCNT_R_ = 0x80; // 180° phase shifted to TCNT_L diff --git a/analog/motor-power-avr/src/mp_pwm_LR_.h b/analog/motor-power-avr/src/mp_pwm_LR_.h index 85da1fec..7156dc8f 100644 --- a/analog/motor-power-avr/src/mp_pwm_LR_.h +++ b/analog/motor-power-avr/src/mp_pwm_LR_.h @@ -121,10 +121,10 @@ // Vectors #define ILIM_R_vect INT0_vect #define ILIM_L_vect INT1_vect -#define L_OVF_vect TIMER0_OVF_vect -#define R_OVF_vect TIMER2_OVF_vect -#define L_COMP_vect TIMER0_COMP_vect -#define R_COMP_vect TIMER2_COMP_vect +#define OVF_L_vect TIMER0_OVF_vect +#define OVF_R_vect TIMER2_OVF_vect +#define COMP_L_vect TIMER0_COMP_vect +#define COMP_R_vect TIMER2_COMP_vect // Command states #define CMD_STATE_DIR_0 0x00 diff --git a/analog/motor-power-avr/src/mp_pwm_L_.c b/analog/motor-power-avr/src/mp_pwm_L_.c index a9e49943..13f49729 100644 --- a/analog/motor-power-avr/src/mp_pwm_L_.c +++ b/analog/motor-power-avr/src/mp_pwm_L_.c @@ -35,7 +35,7 @@ void init_pwm_L_ (void) { } // PWM rising edge on timer overflow IT -ISR(L_OVF_vect) { +ISR(OVF_L_vect) { // programs the state which is ordered by the core code state_L_ = state_L_cmd; @@ -111,7 +111,7 @@ ISR(L_OVF_vect) { } // PWM falling edge on timer compare IT -ISR(L_COMP_vect) { +ISR(COMP_L_vect) { // PC = PC + state_L_; TODO :saut calculé ? switch (state_L_) diff --git a/analog/motor-power-avr/src/mp_pwm_R_.c b/analog/motor-power-avr/src/mp_pwm_R_.c new file mode 100644 index 00000000..5e2a4ea2 --- /dev/null +++ b/analog/motor-power-avr/src/mp_pwm_R_.c @@ -0,0 +1,206 @@ +/* "mp_pwm_R_.c" + * this file contains routines for managing the _R_ channel of mp board + * the command sed -e 's/_R_/_R_/g' can be used for generating the _R_ file + */ + +#include "mp_pwm_LR_.h" +#include "mp_pwm_R_.h" +#include "io.h" + +// static variables +static uint8_t state_R_; +static uint8_t state_R_cmd; +static uint8_t pwm_R_; + +// Le PC, afin de faire le saut calculé +//#define PC PC_REG + +// init +void init_pwm_R_ (void) { + state_R_cmd = CMD_STATE_HIGH_Z; + pwm_R_ = 0x00; + + // Set outputs to 0 (ie HIGH_Z) + _R_AL_0; + _R_AH_0; + _R_BL_0; + _R_BH_0; + + // status LEDs + _R_LED0_0; + _R_LED1_0; + + // Set IOs as outputs + _R_ACTIVATE_OUTPUTS; +} + +// PWM rising edge on timer overflow IT +ISR(OVF_R_vect) { + // programs the state which is ordered by the core code + state_R_ = state_R_cmd; + + // the falling of the other side may have delayed a few our IT + OCR_R_ = pwm_R_; + + //PC = PC + state_R_; // j'aurais bien aimé faire un calculated jump + + switch (state_R_) + { + case CMD_STATE_DIR_0: + // dir 0 + //rise_R_label0: + _R_BH_0; + _R_BL_1; + _R_AL_0; + _R_AH_1; + sei(); // set back interrupts + + // Display CMD_STATE on LEDs + _R_LED0_1; + _R_LED1_0; + + break; + + case CMD_STATE_DIR_1: + // dir 1 + //org rise_R_label0 + 0x10 + _R_AH_0; + _R_AL_1; + _R_BL_0; + _R_BH_1; + sei(); // set back interrupts + + // Display CMD_STATE on LEDs + _R_LED0_0; + _R_LED1_1; + + break; + + case CMD_STATE_BRAKE: + // switch to forced low steady state + //org rise_R_label0 + 0x20 + _R_AH_0; + _R_AL_1; + _R_BH_0; + _R_BL_1; + sei(); // set back interrupts + + // Display CMD_STATE on LEDs + _R_LED0_0; + _R_LED1_0; + + break; + + case CMD_STATE_HIGH_Z: + default: + // switch to high impedance steady state + //org rise_R_label0 + 0x30 + _R_AL_0; + _R_AH_0; + _R_BL_0; + _R_BH_0; + sei(); // set back interrupts + + // Display CMD_STATE on LEDs + _R_LED0_1; + _R_LED1_1; + + break; + + } +} + +// PWM falling edge on timer compare IT +ISR(COMP_R_vect) { + // PC = PC + state_R_; TODO :saut calculé ? + + switch (state_R_) + { + case CMD_STATE_DIR_0: + // in the case we are in 0x00 direction + //fall_R_label0: + _R_AH_0; + _R_AL_1; + sei(); // set back interrupts + + // Display CMD_STATE on LEDs + _R_LED0_0; + _R_LED1_0; + + break; + + case CMD_STATE_DIR_1: + // in the case we are in 0x10 direction + //org fall_R_label0 + 0x10 + _R_BH_0; + _R_BL_1; + sei(); // set back interrupts + + // Display CMD_STATE on LEDs + _R_LED0_0; + _R_LED1_0; + + break; + + case CMD_STATE_BRAKE: + // forced low + //org fall_R_label0 + 0x20 + sei(); // set back interrupts + break; + + case CMD_STATE_HIGH_Z: + default: + // left high Z + //org fall_R_label0 + 0x30 + sei(); // set back interrupts + break; + } +} + +// overcurrent detected by comparators +ISR(ILIM_R_vect) { + _R_AL_0; + _R_AH_0; + _R_BL_0; + _R_BH_0; + sei(); // set back interrupts + // following line orders to keep high Z state when faling edge will arrive + state_R_ = CMD_STATE_HIGH_Z; + return; +} + +// starts the motor +void start_motor_R_ (uint8_t pwmspeed, uint8_t direction) { + // checking direction + if (direction) + { + state_R_cmd = CMD_STATE_DIR_1; + } + else + { + state_R_cmd = CMD_STATE_DIR_0; + } + + // setting pwm value + if (pwmspeed == 0) + {// brake + state_R_cmd = CMD_STATE_BRAKE; + pwm_R_ = 0; + } + else + { + // adding offset + pwmspeed = pwmspeed + PWM_OFFSET_LR_; + + // clamp the value + UTILS_BOUND(pwmspeed, PWM_MIN_LR_, PWM_MAX_LR_); + + // Apply the value + pwm_R_ = pwmspeed; + } +} + +// puts motor in high Z +void stop_motor_R_ (void) { + state_R_ = CMD_STATE_HIGH_Z; +} diff --git a/analog/motor-power-avr/src/mp_pwm_R_.h b/analog/motor-power-avr/src/mp_pwm_R_.h new file mode 100644 index 00000000..6819e4ab --- /dev/null +++ b/analog/motor-power-avr/src/mp_pwm_R_.h @@ -0,0 +1,37 @@ +#ifndef mp_pwm_R__h +#define mp_pwm_R__h +/* mp_pwm_R_.h */ +/* {{{ + * + * Copyright (C) 2008 Pierre Prot + * + * 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. + * + * }}} */ + + +void init_pwm_R_ (void); +void rise_R_ (void); +void fall_R_ (void); +void ovc_R_ (void); +void start_motor_R_ (uint8_t pwmspeed, uint8_t direction); +void stop_motor_R_ (void); + + +#endif /* mp_pwm_R__h */ -- cgit v1.2.3