2014-09-19 18:27:06 -07:00
|
|
|
/*
|
|
|
|
Copyright 2012-2014 Benjamin Vedder benjamin@vedder.se
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2014-09-14 14:08:22 -07:00
|
|
|
/*
|
|
|
|
* datatypes.h
|
|
|
|
*
|
|
|
|
* Created on: 14 sep 2014
|
|
|
|
* Author: benjamin
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DATATYPES_H_
|
|
|
|
#define DATATYPES_H_
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2015-02-19 12:20:07 -08:00
|
|
|
#include "ch.h"
|
2014-09-14 14:08:22 -07:00
|
|
|
|
|
|
|
// Data types
|
|
|
|
typedef enum {
|
|
|
|
MC_STATE_OFF = 0,
|
|
|
|
MC_STATE_DETECTING,
|
|
|
|
MC_STATE_RUNNING,
|
|
|
|
MC_STATE_FULL_BRAKE,
|
|
|
|
} mc_state;
|
|
|
|
|
|
|
|
typedef enum {
|
2014-09-14 17:39:58 -07:00
|
|
|
PWM_MODE_NONSYNCHRONOUS_HISW = 0, // This mode is not recommended
|
2014-09-14 14:08:22 -07:00
|
|
|
PWM_MODE_SYNCHRONOUS, // The recommended and most tested mode
|
|
|
|
PWM_MODE_BIPOLAR // Some glitches occasionally, can kill MOSFETs
|
|
|
|
} mc_pwm_mode;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
COMM_MODE_INTEGRATE = 0,
|
|
|
|
COMM_MODE_DELAY
|
|
|
|
} mc_comm_mode;
|
|
|
|
|
2015-06-19 14:38:46 -07:00
|
|
|
typedef enum {
|
|
|
|
SENSOR_MODE_SENSORLESS = 0,
|
|
|
|
SENSOR_MODE_SENSORED,
|
|
|
|
SENSOR_MODE_HYBRID
|
|
|
|
} mc_sensor_mode;
|
|
|
|
|
2015-04-12 09:23:17 -07:00
|
|
|
typedef enum {
|
|
|
|
MOTOR_TYPE_BLDC = 0,
|
|
|
|
MOTOR_TYPE_DC,
|
|
|
|
} mc_motor_type;
|
|
|
|
|
2014-09-14 14:08:22 -07:00
|
|
|
typedef enum {
|
|
|
|
FAULT_CODE_NONE = 0,
|
|
|
|
FAULT_CODE_OVER_VOLTAGE,
|
|
|
|
FAULT_CODE_UNDER_VOLTAGE,
|
|
|
|
FAULT_CODE_DRV8302,
|
2015-01-22 15:40:09 -08:00
|
|
|
FAULT_CODE_ABS_OVER_CURRENT,
|
|
|
|
FAULT_CODE_OVER_TEMP_FET,
|
|
|
|
FAULT_CODE_OVER_TEMP_MOTOR
|
2014-09-14 14:08:22 -07:00
|
|
|
} mc_fault_code;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
CONTROL_MODE_DUTY = 0,
|
|
|
|
CONTROL_MODE_SPEED,
|
|
|
|
CONTROL_MODE_CURRENT,
|
|
|
|
CONTROL_MODE_CURRENT_BRAKE,
|
2015-04-26 15:02:32 -07:00
|
|
|
CONTROL_MODE_POS,
|
2014-09-14 14:08:22 -07:00
|
|
|
CONTROL_MODE_NONE
|
|
|
|
} mc_control_mode;
|
|
|
|
|
|
|
|
typedef struct {
|
2014-09-17 12:05:57 -07:00
|
|
|
float cycle_int_limit;
|
|
|
|
float cycle_int_limit_running;
|
|
|
|
float cycle_int_limit_max;
|
|
|
|
float comm_time_sum;
|
|
|
|
float comm_time_sum_min_rpm;
|
|
|
|
int32_t comms;
|
|
|
|
uint32_t time_at_comm;
|
2014-09-14 14:08:22 -07:00
|
|
|
} mc_rpm_dep_struct;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
// Switching and drive
|
|
|
|
mc_pwm_mode pwm_mode;
|
|
|
|
mc_comm_mode comm_mode;
|
2015-04-12 09:23:17 -07:00
|
|
|
mc_motor_type motor_type;
|
2015-06-19 14:38:46 -07:00
|
|
|
mc_sensor_mode sensor_mode;
|
2014-09-14 14:08:22 -07:00
|
|
|
// Limits
|
|
|
|
float l_current_max;
|
|
|
|
float l_current_min;
|
|
|
|
float l_in_current_max;
|
|
|
|
float l_in_current_min;
|
|
|
|
float l_abs_current_max;
|
|
|
|
float l_min_erpm;
|
|
|
|
float l_max_erpm;
|
2014-09-14 17:39:58 -07:00
|
|
|
float l_max_erpm_fbrake;
|
2014-12-13 02:46:26 -08:00
|
|
|
float l_max_erpm_fbrake_cc;
|
2014-09-14 14:08:22 -07:00
|
|
|
float l_min_vin;
|
|
|
|
float l_max_vin;
|
2015-08-26 14:12:39 -07:00
|
|
|
float l_battery_cut_start;
|
|
|
|
float l_battery_cut_end;
|
2014-09-14 14:08:22 -07:00
|
|
|
bool l_slow_abs_current;
|
|
|
|
bool l_rpm_lim_neg_torque;
|
2014-11-25 12:05:46 -08:00
|
|
|
float l_temp_fet_start;
|
|
|
|
float l_temp_fet_end;
|
|
|
|
float l_temp_motor_start;
|
|
|
|
float l_temp_motor_end;
|
2015-05-20 07:13:48 -07:00
|
|
|
float l_min_duty;
|
|
|
|
float l_max_duty;
|
2014-10-19 10:31:06 -07:00
|
|
|
// Overridden limits (Computed during runtime)
|
|
|
|
float lo_current_max;
|
|
|
|
float lo_current_min;
|
|
|
|
float lo_in_current_max;
|
|
|
|
float lo_in_current_min;
|
2014-09-14 14:08:22 -07:00
|
|
|
// Sensorless
|
|
|
|
float sl_min_erpm;
|
|
|
|
float sl_min_erpm_cycle_int_limit;
|
2014-12-12 16:31:54 -08:00
|
|
|
float sl_max_fullbreak_current_dir_change;
|
2014-09-14 14:08:22 -07:00
|
|
|
float sl_cycle_int_limit;
|
2015-02-19 12:20:07 -08:00
|
|
|
float sl_phase_advance_at_br;
|
2014-09-14 14:08:22 -07:00
|
|
|
float sl_cycle_int_rpm_br;
|
|
|
|
float sl_bemf_coupling_k;
|
|
|
|
// Hall sensor
|
2015-06-19 14:38:46 -07:00
|
|
|
int8_t hall_table[8];
|
|
|
|
float hall_sl_erpm;
|
2014-09-14 14:08:22 -07:00
|
|
|
// Speed PID
|
|
|
|
float s_pid_kp;
|
|
|
|
float s_pid_ki;
|
|
|
|
float s_pid_kd;
|
2015-09-17 14:24:55 -07:00
|
|
|
float s_pid_min_erpm;
|
2015-04-26 15:02:32 -07:00
|
|
|
// Pos PID
|
|
|
|
float p_pid_kp;
|
|
|
|
float p_pid_ki;
|
|
|
|
float p_pid_kd;
|
2014-09-14 14:08:22 -07:00
|
|
|
// Current controller
|
|
|
|
float cc_startup_boost_duty;
|
|
|
|
float cc_min_current;
|
|
|
|
float cc_gain;
|
2015-04-26 15:02:32 -07:00
|
|
|
float cc_ramp_step_max;
|
2014-09-18 14:00:14 -07:00
|
|
|
// Misc
|
|
|
|
int32_t m_fault_stop_time_ms;
|
2015-08-26 14:12:39 -07:00
|
|
|
float m_duty_ramp_step;
|
|
|
|
float m_duty_ramp_step_rpm_lim;
|
|
|
|
float m_current_backoff_gain;
|
2014-09-14 14:08:22 -07:00
|
|
|
} mc_configuration;
|
|
|
|
|
2014-09-17 12:05:57 -07:00
|
|
|
// Applications to use
|
|
|
|
typedef enum {
|
|
|
|
APP_NONE = 0,
|
|
|
|
APP_PPM,
|
2015-05-04 14:25:43 -07:00
|
|
|
APP_ADC,
|
2014-09-19 18:22:38 -07:00
|
|
|
APP_UART,
|
|
|
|
APP_PPM_UART,
|
2015-05-04 14:25:43 -07:00
|
|
|
APP_ADC_UART,
|
2014-10-18 16:00:53 -07:00
|
|
|
APP_NUNCHUK,
|
2015-04-11 01:07:36 -07:00
|
|
|
APP_NRF,
|
2014-09-17 12:05:57 -07:00
|
|
|
APP_CUSTOM
|
|
|
|
} app_use;
|
|
|
|
|
|
|
|
// PPM control types
|
|
|
|
typedef enum {
|
2014-09-20 04:41:18 -07:00
|
|
|
PPM_CTRL_TYPE_NONE = 0,
|
|
|
|
PPM_CTRL_TYPE_CURRENT,
|
2014-09-18 14:00:14 -07:00
|
|
|
PPM_CTRL_TYPE_CURRENT_NOREV,
|
|
|
|
PPM_CTRL_TYPE_CURRENT_NOREV_BRAKE,
|
2014-09-17 12:05:57 -07:00
|
|
|
PPM_CTRL_TYPE_DUTY,
|
2014-09-18 14:00:14 -07:00
|
|
|
PPM_CTRL_TYPE_DUTY_NOREV,
|
|
|
|
PPM_CTRL_TYPE_PID,
|
|
|
|
PPM_CTRL_TYPE_PID_NOREV
|
2014-09-17 12:05:57 -07:00
|
|
|
} ppm_control_type;
|
|
|
|
|
2015-02-19 12:20:07 -08:00
|
|
|
typedef struct {
|
|
|
|
ppm_control_type ctrl_type;
|
|
|
|
float pid_max_erpm;
|
|
|
|
float hyst;
|
|
|
|
float pulse_start;
|
2015-04-30 16:57:55 -07:00
|
|
|
float pulse_end;
|
2015-04-20 13:09:53 -07:00
|
|
|
bool median_filter;
|
2015-04-30 16:57:55 -07:00
|
|
|
bool safe_start;
|
2015-02-19 12:20:07 -08:00
|
|
|
float rpm_lim_start;
|
|
|
|
float rpm_lim_end;
|
|
|
|
bool multi_esc;
|
|
|
|
bool tc;
|
|
|
|
float tc_max_diff;
|
|
|
|
} ppm_config;
|
|
|
|
|
2015-05-04 14:25:43 -07:00
|
|
|
// ADC control types
|
|
|
|
typedef enum {
|
|
|
|
ADC_CTRL_TYPE_NONE = 0,
|
|
|
|
ADC_CTRL_TYPE_CURRENT,
|
|
|
|
ADC_CTRL_TYPE_CURRENT_REV_CENTER,
|
|
|
|
ADC_CTRL_TYPE_CURRENT_REV_BUTTON,
|
|
|
|
ADC_CTRL_TYPE_CURRENT_NOREV_BRAKE_CENTER,
|
|
|
|
ADC_CTRL_TYPE_CURRENT_NOREV_BRAKE_BUTTON,
|
|
|
|
ADC_CTRL_TYPE_DUTY,
|
|
|
|
ADC_CTRL_TYPE_DUTY_REV_CENTER,
|
|
|
|
ADC_CTRL_TYPE_DUTY_REV_BUTTON
|
|
|
|
} adc_control_type;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
adc_control_type ctrl_type;
|
|
|
|
float hyst;
|
|
|
|
float voltage_start;
|
|
|
|
float voltage_end;
|
|
|
|
bool use_filter;
|
|
|
|
bool safe_start;
|
2015-08-23 09:26:05 -07:00
|
|
|
bool cc_button_inverted;
|
|
|
|
bool rev_button_inverted;
|
2015-05-04 14:25:43 -07:00
|
|
|
bool voltage_inverted;
|
|
|
|
float rpm_lim_start;
|
|
|
|
float rpm_lim_end;
|
|
|
|
bool multi_esc;
|
|
|
|
bool tc;
|
|
|
|
float tc_max_diff;
|
|
|
|
uint32_t update_rate_hz;
|
|
|
|
} adc_config;
|
|
|
|
|
2014-10-18 16:00:53 -07:00
|
|
|
// Nunchuk control types
|
|
|
|
typedef enum {
|
|
|
|
CHUK_CTRL_TYPE_NONE = 0,
|
|
|
|
CHUK_CTRL_TYPE_CURRENT,
|
|
|
|
CHUK_CTRL_TYPE_CURRENT_NOREV
|
|
|
|
} chuk_control_type;
|
|
|
|
|
2015-02-19 12:20:07 -08:00
|
|
|
typedef struct {
|
|
|
|
chuk_control_type ctrl_type;
|
|
|
|
float hyst;
|
|
|
|
float rpm_lim_start;
|
|
|
|
float rpm_lim_end;
|
|
|
|
float ramp_time_pos;
|
|
|
|
float ramp_time_neg;
|
2015-09-17 14:24:55 -07:00
|
|
|
float stick_erpm_per_s_in_cc;
|
2015-02-19 12:20:07 -08:00
|
|
|
bool multi_esc;
|
|
|
|
bool tc;
|
|
|
|
float tc_max_diff;
|
|
|
|
} chuk_config;
|
|
|
|
|
2014-09-17 12:05:57 -07:00
|
|
|
typedef struct {
|
2014-09-20 04:41:18 -07:00
|
|
|
// Settings
|
2015-02-19 12:20:07 -08:00
|
|
|
uint8_t controller_id;
|
2014-09-20 04:41:18 -07:00
|
|
|
uint32_t timeout_msec;
|
|
|
|
float timeout_brake_current;
|
2015-02-19 12:20:07 -08:00
|
|
|
bool send_can_status;
|
2015-04-26 15:02:32 -07:00
|
|
|
uint32_t send_can_status_rate_hz;
|
2014-09-20 04:41:18 -07:00
|
|
|
|
2014-09-17 12:05:57 -07:00
|
|
|
// Application to use
|
|
|
|
app_use app_to_use;
|
|
|
|
|
|
|
|
// PPM application settings
|
2015-02-19 12:20:07 -08:00
|
|
|
ppm_config app_ppm_conf;
|
2014-09-18 14:00:14 -07:00
|
|
|
|
2015-05-04 14:25:43 -07:00
|
|
|
// ADC application settings
|
|
|
|
adc_config app_adc_conf;
|
|
|
|
|
2014-09-18 14:00:14 -07:00
|
|
|
// UART application settings
|
|
|
|
uint32_t app_uart_baudrate;
|
2014-10-18 16:00:53 -07:00
|
|
|
|
2015-02-19 12:20:07 -08:00
|
|
|
// Nunchuk application settings
|
|
|
|
chuk_config app_chuk_conf;
|
2014-09-17 12:05:57 -07:00
|
|
|
} app_configuration;
|
|
|
|
|
2014-09-19 18:22:38 -07:00
|
|
|
// Communication commands
|
|
|
|
typedef enum {
|
2015-05-04 14:25:43 -07:00
|
|
|
COMM_FW_VERSION = 0,
|
2015-05-08 13:53:59 -07:00
|
|
|
COMM_JUMP_TO_BOOTLOADER,
|
|
|
|
COMM_ERASE_NEW_APP,
|
|
|
|
COMM_WRITE_NEW_APP_DATA,
|
2015-05-04 14:25:43 -07:00
|
|
|
COMM_GET_VALUES,
|
2014-09-19 18:22:38 -07:00
|
|
|
COMM_SET_DUTY,
|
|
|
|
COMM_SET_CURRENT,
|
|
|
|
COMM_SET_CURRENT_BRAKE,
|
|
|
|
COMM_SET_RPM,
|
2015-04-26 15:02:32 -07:00
|
|
|
COMM_SET_POS,
|
2014-09-19 18:22:38 -07:00
|
|
|
COMM_SET_DETECT,
|
2015-07-31 14:26:50 -07:00
|
|
|
COMM_SET_SERVO_POS,
|
2014-09-19 18:22:38 -07:00
|
|
|
COMM_SET_MCCONF,
|
|
|
|
COMM_GET_MCCONF,
|
|
|
|
COMM_SET_APPCONF,
|
|
|
|
COMM_GET_APPCONF,
|
|
|
|
COMM_SAMPLE_PRINT,
|
|
|
|
COMM_TERMINAL_CMD,
|
|
|
|
COMM_PRINT,
|
|
|
|
COMM_ROTOR_POSITION,
|
|
|
|
COMM_EXPERIMENT_SAMPLE,
|
|
|
|
COMM_DETECT_MOTOR_PARAM,
|
2014-09-20 04:41:18 -07:00
|
|
|
COMM_REBOOT,
|
2014-09-20 06:23:36 -07:00
|
|
|
COMM_ALIVE,
|
2014-10-18 16:00:53 -07:00
|
|
|
COMM_GET_DECODED_PPM,
|
2015-05-04 14:25:43 -07:00
|
|
|
COMM_GET_DECODED_ADC,
|
2015-04-26 15:02:32 -07:00
|
|
|
COMM_GET_DECODED_CHUK,
|
|
|
|
COMM_FORWARD_CAN
|
2014-09-19 18:22:38 -07:00
|
|
|
} COMM_PACKET_ID;
|
|
|
|
|
2014-12-06 19:30:25 -08:00
|
|
|
// CAN commands
|
|
|
|
typedef enum {
|
|
|
|
CAN_PACKET_SET_DUTY = 0,
|
|
|
|
CAN_PACKET_SET_CURRENT,
|
|
|
|
CAN_PACKET_SET_CURRENT_BRAKE,
|
2015-02-19 12:20:07 -08:00
|
|
|
CAN_PACKET_SET_RPM,
|
2015-04-26 15:02:32 -07:00
|
|
|
CAN_PACKET_SET_POS,
|
|
|
|
CAN_PACKET_FILL_RX_BUFFER,
|
2015-05-20 07:13:48 -07:00
|
|
|
CAN_PACKET_FILL_RX_BUFFER_LONG,
|
2015-04-26 15:02:32 -07:00
|
|
|
CAN_PACKET_PROCESS_RX_BUFFER,
|
2015-04-27 11:43:02 -07:00
|
|
|
CAN_PACKET_PROCESS_SHORT_BUFFER,
|
2015-02-19 12:20:07 -08:00
|
|
|
CAN_PACKET_STATUS
|
2014-12-06 19:30:25 -08:00
|
|
|
} CAN_PACKET_ID;
|
|
|
|
|
2014-11-27 11:56:17 -08:00
|
|
|
// Logged fault data
|
|
|
|
typedef struct {
|
|
|
|
mc_fault_code fault;
|
|
|
|
float current;
|
|
|
|
float current_filtered;
|
|
|
|
float voltage;
|
|
|
|
float duty;
|
|
|
|
float rpm;
|
|
|
|
int tacho;
|
2015-08-23 09:26:05 -07:00
|
|
|
int cycles_running;
|
2015-08-26 14:12:39 -07:00
|
|
|
int pwm_cycles;
|
2015-08-23 09:26:05 -07:00
|
|
|
int tim_val_samp;
|
|
|
|
int tim_current_samp;
|
|
|
|
int tim_top;
|
2014-11-27 11:56:17 -08:00
|
|
|
int comm_step;
|
|
|
|
float temperature;
|
|
|
|
} fault_data;
|
|
|
|
|
2014-12-08 15:11:29 -08:00
|
|
|
// External LED state
|
|
|
|
typedef enum {
|
|
|
|
LED_EXT_OFF = 0,
|
|
|
|
LED_EXT_NORMAL,
|
|
|
|
LED_EXT_BRAKE,
|
|
|
|
LED_EXT_TURN_LEFT,
|
|
|
|
LED_EXT_TURN_RIGHT,
|
|
|
|
LED_EXT_BRAKE_TURN_LEFT,
|
2015-03-07 05:56:25 -08:00
|
|
|
LED_EXT_BRAKE_TURN_RIGHT,
|
|
|
|
LED_EXT_BATT
|
2014-12-08 15:11:29 -08:00
|
|
|
} LED_EXT_STATE;
|
|
|
|
|
2014-12-20 03:52:38 -08:00
|
|
|
typedef struct {
|
|
|
|
int js_x;
|
|
|
|
int js_y;
|
|
|
|
int acc_x;
|
|
|
|
int acc_y;
|
|
|
|
int acc_z;
|
|
|
|
bool bt_c;
|
|
|
|
bool bt_z;
|
2015-02-19 12:20:07 -08:00
|
|
|
} chuck_data;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int id;
|
|
|
|
systime_t rx_time;
|
|
|
|
float rpm;
|
|
|
|
float current;
|
|
|
|
float duty;
|
|
|
|
} can_status_msg;
|
2014-12-20 03:52:38 -08:00
|
|
|
|
2015-04-11 01:07:36 -07:00
|
|
|
typedef struct {
|
|
|
|
uint8_t js_x;
|
|
|
|
uint8_t js_y;
|
|
|
|
bool bt_c;
|
|
|
|
bool bt_z;
|
|
|
|
bool bt_push;
|
|
|
|
float vbat;
|
|
|
|
} mote_state;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
MOTE_PACKET_BATT_LEVEL = 0,
|
2015-07-31 14:26:50 -07:00
|
|
|
MOTE_PACKET_BUTTONS,
|
|
|
|
MOTE_PACKET_ALIVE
|
2015-04-11 01:07:36 -07:00
|
|
|
} MOTE_PACKET;
|
|
|
|
|
2015-10-08 14:09:39 -07:00
|
|
|
typedef struct {
|
|
|
|
float v_in;
|
|
|
|
float temp_mos1;
|
|
|
|
float temp_mos2;
|
|
|
|
float temp_mos3;
|
|
|
|
float temp_mos4;
|
|
|
|
float temp_mos5;
|
|
|
|
float temp_mos6;
|
|
|
|
float temp_pcb;
|
|
|
|
float current_motor;
|
|
|
|
float current_in;
|
|
|
|
float rpm;
|
|
|
|
float duty_now;
|
|
|
|
float amp_hours;
|
|
|
|
float amp_hours_charged;
|
|
|
|
float watt_hours;
|
|
|
|
float watt_hours_charged;
|
|
|
|
int tachometer;
|
|
|
|
int tachometer_abs;
|
|
|
|
mc_fault_code fault_code;
|
|
|
|
} mc_values;
|
|
|
|
|
2014-09-14 14:08:22 -07:00
|
|
|
#endif /* DATATYPES_H_ */
|