2014-01-09 06:20:26 -08:00
|
|
|
/*
|
2014-01-09 09:02:38 -08:00
|
|
|
Copyright 2012-2014 Benjamin Vedder benjamin@vedder.se
|
2014-01-09 06:20:26 -08:00
|
|
|
|
|
|
|
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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mc_pwm.h
|
|
|
|
*
|
|
|
|
* Created on: 13 okt 2012
|
|
|
|
* Author: benjamin
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MCPWM_H_
|
|
|
|
#define MCPWM_H_
|
|
|
|
|
2014-04-14 14:02:45 -07:00
|
|
|
#include "conf_general.h"
|
|
|
|
|
2014-01-09 06:20:26 -08:00
|
|
|
typedef enum {
|
|
|
|
MC_STATE_OFF = 0,
|
|
|
|
MC_STATE_DETECTING,
|
|
|
|
MC_STATE_RUNNING,
|
|
|
|
MC_STATE_FULL_BRAKE,
|
|
|
|
} mc_state;
|
|
|
|
|
|
|
|
typedef enum {
|
2014-03-29 14:28:57 -07:00
|
|
|
PWM_MODE_NONSYNCHRONOUS_HISW, // This mode is not recommended
|
2014-04-12 12:59:33 -07:00
|
|
|
PWM_MODE_SYNCHRONOUS, // The recommended and most tested mode
|
|
|
|
PWM_MODE_BIPOLAR // Some glitches occasionally, can kill MOSFETs
|
2014-01-09 06:20:26 -08:00
|
|
|
} mc_pwm_mode;
|
|
|
|
|
2014-03-21 07:40:05 -07:00
|
|
|
typedef enum {
|
|
|
|
FAULT_CODE_NONE = 0,
|
|
|
|
FAULT_CODE_OVER_VOLTAGE,
|
|
|
|
FAULT_CODE_UNDER_VOLTAGE,
|
|
|
|
FAULT_CODE_DRV8302
|
|
|
|
} mc_fault_code;
|
|
|
|
|
2014-03-30 15:51:59 -07:00
|
|
|
typedef enum {
|
|
|
|
CONTROL_MODE_DUTY = 0,
|
|
|
|
CONTROL_MODE_SPEED,
|
2014-04-12 12:59:33 -07:00
|
|
|
CONTROL_MODE_CURRENT,
|
2014-07-27 10:40:38 -07:00
|
|
|
CONTROL_MODE_CURRENT_BRAKE,
|
2014-04-12 12:59:33 -07:00
|
|
|
CONTROL_MODE_NONE
|
2014-03-30 15:51:59 -07:00
|
|
|
} mc_control_mode;
|
|
|
|
|
2014-01-09 06:20:26 -08:00
|
|
|
// Functions
|
|
|
|
void mcpwm_init(void);
|
2014-06-29 05:23:39 -07:00
|
|
|
void mcpwm_init_hall_table(int dir, int fwd_add, int rev_add);
|
2014-01-09 06:20:26 -08:00
|
|
|
void mcpwm_set_duty(float dutyCycle);
|
2014-03-30 15:51:59 -07:00
|
|
|
void mcpwm_set_pid_speed(float rpm);
|
|
|
|
void mcpwm_set_current(float current);
|
2014-07-27 10:40:38 -07:00
|
|
|
void mcpwm_set_brake_current(float current);
|
2014-04-12 12:59:33 -07:00
|
|
|
void mcpwm_brake_now(void);
|
2014-04-12 13:02:22 -07:00
|
|
|
void mcpwm_release_motor(void);
|
2014-01-09 06:20:26 -08:00
|
|
|
int mcpwm_get_comm_step(void);
|
2014-04-02 02:54:21 -07:00
|
|
|
float mcpwm_get_duty_cycle_set(void);
|
2014-04-02 11:23:27 -07:00
|
|
|
float mcpwm_get_duty_cycle_now(void);
|
2014-08-05 13:24:13 -07:00
|
|
|
float mcpwm_get_switching_frequency_now(void);
|
2014-01-09 06:20:26 -08:00
|
|
|
float mcpwm_get_rpm(void);
|
2014-04-02 02:54:21 -07:00
|
|
|
mc_state mcpwm_get_state(void);
|
|
|
|
mc_fault_code mcpwm_get_fault(void);
|
2014-01-09 06:20:26 -08:00
|
|
|
float mcpwm_get_kv(void);
|
|
|
|
float mcpwm_get_kv_filtered(void);
|
|
|
|
int mcpwm_get_tachometer_value(int reset);
|
|
|
|
float mcpwm_get_tot_current(void);
|
2014-04-02 02:54:21 -07:00
|
|
|
float mcpwm_get_tot_current_filtered(void);
|
2014-01-09 06:20:26 -08:00
|
|
|
float mcpwm_get_tot_current_in(void);
|
2014-04-02 02:54:21 -07:00
|
|
|
float mcpwm_get_tot_current_in_filtered(void);
|
2014-01-09 06:20:26 -08:00
|
|
|
void mcpwm_set_detect(void);
|
2014-03-13 07:28:56 -07:00
|
|
|
float mcpwm_get_detect_pos(void);
|
2014-01-09 06:20:26 -08:00
|
|
|
signed int mcpwm_read_hall_phase(void);
|
|
|
|
float mcpwm_read_reset_avg_motor_current(void);
|
|
|
|
float mcpwm_read_reset_avg_input_current(void);
|
|
|
|
float mcpwm_get_last_adc_isr_duration(void);
|
|
|
|
float mcpwm_get_last_inj_adc_isr_duration(void);
|
|
|
|
|
|
|
|
// Interrupt handlers
|
|
|
|
void mcpwm_time_int_handler(void);
|
2014-04-01 10:26:46 -07:00
|
|
|
void mcpwm_update_int_handler(void);
|
2014-01-09 06:20:26 -08:00
|
|
|
void mcpwm_adc_inj_int_handler(void);
|
|
|
|
void mcpwm_adc_int_handler(void *p, uint32_t flags);
|
|
|
|
|
|
|
|
// External variables
|
|
|
|
extern volatile uint16_t ADC_Value[];
|
|
|
|
extern volatile int ADC_curr_norm_value[];
|
2014-03-13 07:28:56 -07:00
|
|
|
extern volatile float mcpwm_detect_currents[];
|
2014-04-21 07:17:39 -07:00
|
|
|
extern volatile float mcpwm_detect_currents_diff[];
|
2014-03-15 14:32:00 -07:00
|
|
|
extern volatile int mcpwm_vzero;
|
2014-01-09 06:20:26 -08:00
|
|
|
|
2014-04-14 14:02:45 -07:00
|
|
|
#ifdef MCCONF_OUTRUNNER1
|
|
|
|
#include "mcconf_outrunner1.h"
|
2014-05-05 11:39:25 -07:00
|
|
|
#elif defined MCCONF_OUTRUNNER2
|
|
|
|
#include "mcconf_outrunner2.h"
|
2014-04-14 14:02:45 -07:00
|
|
|
#elif defined MCCONF_RCCAR1
|
|
|
|
#include "mcconf_rccar1.h"
|
|
|
|
#elif defined MCCONF_STEN
|
|
|
|
#include "mcconf_sten.h"
|
|
|
|
#endif
|
|
|
|
|
2014-01-09 06:20:26 -08:00
|
|
|
/*
|
|
|
|
* Parameters
|
|
|
|
*/
|
2014-08-01 06:43:36 -07:00
|
|
|
#define MCPWM_SWITCH_FREQUENCY_MIN 3000 // The lowest switching frequency in Hz
|
|
|
|
#define MCPWM_SWITCH_FREQUENCY_MAX 35000 // The highest switching frequency in Hz
|
2014-01-09 06:20:26 -08:00
|
|
|
#define MCPWM_DEAD_TIME_CYCLES 80 // Dead time
|
2014-07-30 03:48:40 -07:00
|
|
|
#define MCPWM_RPM_TIMER_FREQ 1000000.0 // Frequency of the RPM measurement timer
|
2014-03-15 14:32:00 -07:00
|
|
|
#define MCPWM_PWM_MODE PWM_MODE_SYNCHRONOUS // Default PWM mode
|
2014-07-27 10:40:38 -07:00
|
|
|
#define MCPWM_MIN_DUTY_CYCLE 0.005 // Minimum duty cycle
|
2014-01-09 06:20:26 -08:00
|
|
|
#define MCPWM_MAX_DUTY_CYCLE 0.95 // Maximum duty cycle
|
2014-07-27 10:40:38 -07:00
|
|
|
#define MCPWM_RAMP_STEP 0.01 // Ramping step (1000 times/sec) at maximum duty cycle
|
|
|
|
#define MCPWM_RAMP_STEP_CURRENT_MAX 0.04 // Maximum ramping step (1000 times/sec) for the current control
|
2014-07-28 01:51:44 -07:00
|
|
|
#define MCPWM_RAMP_STEP_RPM_LIMIT 0.0005 // Ramping step when limiting the RPM
|
2014-04-04 14:19:11 -07:00
|
|
|
#define MCPWM_CURRENT_LIMIT_GAIN 0.1 // The error gain of the current limiting algorithm
|
2014-03-21 07:40:05 -07:00
|
|
|
#define MCPWM_FAULT_STOP_TIME 3000 // Ignore commands for this duration in msec when faults occur
|
2014-07-30 03:48:40 -07:00
|
|
|
#define MCPWM_CMD_STOP_TIME 0 // Ignore commands for this duration in msec after a stop has been sent
|
2014-05-05 11:39:25 -07:00
|
|
|
#define MCPWM_DETECT_STOP_TIME 500 // Ignore commands for this duration in msec after a detect command
|
2014-01-09 06:20:26 -08:00
|
|
|
|
2014-03-30 15:51:59 -07:00
|
|
|
// Speed PID parameters
|
2014-01-09 06:20:26 -08:00
|
|
|
#define MCPWM_PID_TIME_K 0.001 // Pid controller sample time in seconds
|
2014-03-30 15:51:59 -07:00
|
|
|
|
2014-04-14 14:02:45 -07:00
|
|
|
// Parameters that can be overridden
|
2014-06-29 05:23:39 -07:00
|
|
|
#ifndef MCPWM_HALL_DIR
|
|
|
|
#define MCPWM_HALL_DIR 0 // Hall sensor direction [0 or 1]
|
|
|
|
#endif
|
|
|
|
#ifndef MCPWM_HALL_FWD_ADD
|
|
|
|
#define MCPWM_HALL_FWD_ADD 0 // Hall sensor offset fwd [0 to 5]
|
|
|
|
#endif
|
|
|
|
#ifndef MCPWM_HALL_REV_ADD
|
2014-06-30 00:50:05 -07:00
|
|
|
#define MCPWM_HALL_REV_ADD 0 // Hall sensor offset rev [0 to 5]
|
2014-04-14 14:02:45 -07:00
|
|
|
#endif
|
|
|
|
#ifndef MCPWM_MIN_VOLTAGE
|
|
|
|
#define MCPWM_MIN_VOLTAGE 8.0 // Minimum input voltage
|
|
|
|
#endif
|
|
|
|
#ifndef MCPWM_MAX_VOLTAGE
|
|
|
|
#define MCPWM_MAX_VOLTAGE 50.0 // Maximum input voltage
|
|
|
|
#endif
|
|
|
|
#ifndef MCPWM_RPM_MAX
|
|
|
|
#define MCPWM_RPM_MAX 100000.0 // The motor speed limit (Upper)
|
|
|
|
#endif
|
|
|
|
#ifndef MCPWM_RPM_MIN
|
|
|
|
#define MCPWM_RPM_MIN -100000.0 // The motor speed limit (Lower)
|
|
|
|
#endif
|
2014-05-08 14:44:27 -07:00
|
|
|
#ifndef MCPWM_CURRENT_STARTUP_BOOST
|
2014-07-27 10:40:38 -07:00
|
|
|
#define MCPWM_CURRENT_STARTUP_BOOST 0.01 // The lowest duty cycle to use in current control mode @ 20V.
|
2014-05-08 14:44:27 -07:00
|
|
|
#endif
|
2014-06-09 03:35:50 -07:00
|
|
|
#ifndef MCPWM_RPM_LIMIT_NEG_TORQUE
|
|
|
|
#define MCPWM_RPM_LIMIT_NEG_TORQUE 1 // Use negative torque to limit the RPM
|
|
|
|
#endif
|
2014-07-27 10:40:38 -07:00
|
|
|
#ifndef MCPWM_CURR_MIN_RPM_FBRAKE
|
|
|
|
#define MCPWM_CURR_MIN_RPM_FBRAKE 1500 // Minimum electrical RPM to use full brake at
|
|
|
|
#endif
|
2014-03-21 12:15:44 -07:00
|
|
|
|
2014-01-09 06:20:26 -08:00
|
|
|
#endif /* MC_PWM_H_ */
|