Skip to content

Commit

Permalink
Conditional RAMFUNC directives (#230)
Browse files Browse the repository at this point in the history
* add conditional ramfunc directives

* add ndebug flag

* bump debug level
  • Loading branch information
yconst authored Nov 26, 2022
1 parent fe0abba commit 2954b2b
Show file tree
Hide file tree
Showing 30 changed files with 203 additions and 194 deletions.
8 changes: 4 additions & 4 deletions firmware/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ OBJECTS := $(addprefix $(BUILDDIR)/,$(CSOURCES:%.c=%.o)) $(addprefix $(BUILDDIR)
all: release

# Debug target
debug: CFLAGS += -DDEBUG -g1 -O1
debug: CPPFLAGS += -DDEBUG -g1 -O1
debug: CFLAGS += -DDEBUG -g2 -O1
debug: CPPFLAGS += -DDEBUG -g2 -O1
debug: LDFLAGS += -O1
debug: OBJECTS += $(BUILDDIR)/bootloader.o
debug: binary

# Upgrade target
upgrade: CFLAGS += -g0 -O3
upgrade: CPPFLAGS += -g0 -O3
upgrade: CFLAGS += -DNDEBUG -g0 -O3
upgrade: CPPFLAGS += -DNDEBUG -g0 -O3
upgrade: LDFLAGS += -O3
upgrade: binary

Expand Down
6 changes: 3 additions & 3 deletions firmware/src/adc/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,12 @@ void ADC_DTSE_Init(void)
pac5xxx_dtse_seq_config(18, ADC0, 0, ADC_IRQ0_EN, SEQ_END); // Get result at DTSERES18, Interrupt
}

PAC5XXX_RAMFUNC int16_t adc_get_mcu_temp(void)
TM_RAMFUNC int16_t adc_get_mcu_temp(void)
{
return adc_state.temp;
}

PAC5XXX_RAMFUNC void ADC_GetPhaseCurrents(struct FloatTriplet *phc)
TM_RAMFUNC void ADC_GetPhaseCurrents(struct FloatTriplet *phc)
{
phc->A = adc_state.I_phase_meas.A;
if (motor_phases_swapped())
Expand All @@ -236,7 +236,7 @@ PAC5XXX_RAMFUNC void ADC_GetPhaseCurrents(struct FloatTriplet *phc)
}
}

PAC5XXX_RAMFUNC void ADC_update(void)
TM_RAMFUNC void ADC_update(void)
{
if (gate_driver_is_enabled() == true)
{
Expand Down
2 changes: 1 addition & 1 deletion firmware/src/can/can.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void CAN_restore_config(CANConfig *config_)
config = *config_;
}

PAC5XXX_RAMFUNC void CAN_task(void) {
TM_RAMFUNC void CAN_task(void) {
// Transmit heartbeat
const uint32_t msg_diff = msTicks - state.last_msg_ms;
if (msg_diff >= config.heartbeat_period && PAC55XX_CAN->SR.TBS != 0)
Expand Down
8 changes: 4 additions & 4 deletions firmware/src/can/can_func.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void can_baud(CAN_BAUD_TYPE baud)
}
}

PAC5XXX_RAMFUNC void can_process_standard(void)
TM_RAMFUNC void can_process_standard(void)
{
uint32_t buffer = PAC55XX_CAN->RXBUF; // read RX buffer, RX buffer bit order same as TX buffer

Expand Down Expand Up @@ -212,7 +212,7 @@ PAC5XXX_RAMFUNC void can_process_standard(void)
// * You should have received a copy of the GNU General Public License
// * along with this program. If not, see <http://www.gnu.org/licenses/>.

PAC5XXX_RAMFUNC void can_process_extended(void)
TM_RAMFUNC void can_process_extended(void)
{
uint32_t buffer = PAC55XX_CAN->RXBUF; // read RX buffer, RX buffer bit order same as TX buffer

Expand Down Expand Up @@ -244,7 +244,7 @@ PAC5XXX_RAMFUNC void can_process_extended(void)
}
}

PAC5XXX_RAMFUNC void can_transmit_standard(uint8_t dataLen, uint16_t id, const uint8_t * data)
TM_RAMFUNC void can_transmit_standard(uint8_t dataLen, uint16_t id, const uint8_t * data)
{
while (PAC55XX_CAN->SR.TBS == 0) {}; // wait for TX buffer free
PAC55XX_CAN->TXBUF = (dataLen << 0) | // DLC - Data Length Code
Expand Down Expand Up @@ -288,7 +288,7 @@ PAC5XXX_RAMFUNC void can_transmit_standard(uint8_t dataLen, uint16_t id, const u
// * You should have received a copy of the GNU General Public License
// * along with this program. If not, see <http://www.gnu.org/licenses/>.

PAC5XXX_RAMFUNC void can_transmit_extended(uint8_t dataLen, uint32_t id, const uint8_t * data)
TM_RAMFUNC void can_transmit_extended(uint8_t dataLen, uint32_t id, const uint8_t * data)
{
while (PAC55XX_CAN->SR.TBS == 0) {}; // wait for TX buffer free
PAC55XX_CAN->TXBUF = (dataLen << 0) | // DLC - Data Length Code
Expand Down
8 changes: 8 additions & 0 deletions firmware/src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@
#define PAC5XXX_ERROR 1
#endif

#if defined(DEBUG)
#define TM_RAMFUNC
#elif defined (NDEBUG)
#define TM_RAMFUNC PAC5XXX_RAMFUNC
#else
#error "Unknown debug configuration"
#endif

#define PI (3.141592f)
#define TWOPI (6.283185f)
#define INVTWOPI (0.159155f)
Expand Down
42 changes: 21 additions & 21 deletions firmware/src/controller/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ void Controller_ControlLoop(void)
}
}

PAC5XXX_RAMFUNC void CLControlStep(void)
TM_RAMFUNC void CLControlStep(void)
{
if (state.mode >= CTRL_TRAJECTORY)
{
Expand Down Expand Up @@ -302,12 +302,12 @@ PAC5XXX_RAMFUNC void CLControlStep(void)
}


PAC5XXX_RAMFUNC ControlState controller_get_state(void)
TM_RAMFUNC ControlState controller_get_state(void)
{
return state.state;
}

PAC5XXX_RAMFUNC void controller_set_state(ControlState new_state)
TM_RAMFUNC void controller_set_state(ControlState new_state)
{
if ((new_state != state.state) && (false == state.is_calibrating))
{
Expand All @@ -331,12 +331,12 @@ PAC5XXX_RAMFUNC void controller_set_state(ControlState new_state)
}
}

PAC5XXX_RAMFUNC ControlMode controller_get_mode(void)
TM_RAMFUNC ControlMode controller_get_mode(void)
{
return state.mode;
}

PAC5XXX_RAMFUNC void controller_set_mode(ControlMode new_mode)
TM_RAMFUNC void controller_set_mode(ControlMode new_mode)
{
if (new_mode != state.mode)
{
Expand All @@ -359,69 +359,69 @@ PAC5XXX_RAMFUNC void controller_set_mode(ControlMode new_mode)
}
}

PAC5XXX_RAMFUNC float controller_get_pos_setpoint_user_frame(void)
TM_RAMFUNC float controller_get_pos_setpoint_user_frame(void)
{
return (state.pos_setpoint - motor_get_user_offset()) * motor_get_user_direction();
}

PAC5XXX_RAMFUNC void controller_set_pos_setpoint_user_frame(float value)
TM_RAMFUNC void controller_set_pos_setpoint_user_frame(float value)
{
// direction is either 1 or -1 so we can multiply instead of divide
state.pos_setpoint = value * motor_get_user_direction() + motor_get_user_offset();
}

PAC5XXX_RAMFUNC float controller_get_vel_setpoint_user_frame(void)
TM_RAMFUNC float controller_get_vel_setpoint_user_frame(void)
{
return state.vel_setpoint * motor_get_user_direction();
}

PAC5XXX_RAMFUNC void controller_set_vel_setpoint_user_frame(float value)
TM_RAMFUNC void controller_set_vel_setpoint_user_frame(float value)
{
// direction is either 1 or -1 so we can multiply instead of divide
state.vel_setpoint = value * motor_get_user_direction();
}

PAC5XXX_RAMFUNC float controller_get_Iq_estimate(void)
TM_RAMFUNC float controller_get_Iq_estimate(void)
{
return state.Iq_est;
}

PAC5XXX_RAMFUNC float controller_get_Iq_setpoint(void)
TM_RAMFUNC float controller_get_Iq_setpoint(void)
{
return state.Iq_setpoint;
}

PAC5XXX_RAMFUNC void controller_set_Iq_setpoint(float value)
TM_RAMFUNC void controller_set_Iq_setpoint(float value)
{
state.Iq_setpoint = value;
}

PAC5XXX_RAMFUNC float controller_get_Iq_estimate_user_frame(void)
TM_RAMFUNC float controller_get_Iq_estimate_user_frame(void)
{
return state.Iq_est * motor_get_user_direction();
}

PAC5XXX_RAMFUNC float controller_get_Iq_setpoint_user_frame(void)
TM_RAMFUNC float controller_get_Iq_setpoint_user_frame(void)
{
return state.Iq_setpoint * motor_get_user_direction();
}

PAC5XXX_RAMFUNC void controller_set_Iq_setpoint_user_frame(float value)
TM_RAMFUNC void controller_set_Iq_setpoint_user_frame(float value)
{
state.Iq_setpoint = value * motor_get_user_direction();
}

PAC5XXX_RAMFUNC float controller_get_Id_setpoint_user_frame(void)
TM_RAMFUNC float controller_get_Id_setpoint_user_frame(void)
{
return state.Id_setpoint;
}

PAC5XXX_RAMFUNC float controller_get_Vq_setpoint_user_frame(void)
TM_RAMFUNC float controller_get_Vq_setpoint_user_frame(void)
{
return state.Vq_setpoint * motor_get_user_direction();
}

PAC5XXX_RAMFUNC float controller_set_pos_vel_setpoints(float pos_setpoint, float vel_setpoint)
TM_RAMFUNC float controller_set_pos_vel_setpoints(float pos_setpoint, float vel_setpoint)
{
controller_set_pos_setpoint_user_frame(pos_setpoint);
controller_set_vel_setpoint_user_frame(vel_setpoint);
Expand Down Expand Up @@ -605,20 +605,20 @@ static inline bool Controller_LimitVelocity(float min_limit, float max_limit, fl
return our_clampc(I, Imin, Imax);
}

PAC5XXX_RAMFUNC void controller_update_I_gains(void)
TM_RAMFUNC void controller_update_I_gains(void)
{
config.I_gain = config.I_bw * motor_get_phase_inductance();
float plant_pole = motor_get_phase_resistance() / motor_get_phase_inductance();
config.Iq_integrator_gain = plant_pole * config.I_gain;
config.Id_integrator_gain = config.Iq_integrator_gain;
}

PAC5XXX_RAMFUNC uint8_t controller_get_warnings(void)
TM_RAMFUNC uint8_t controller_get_warnings(void)
{
return state.warnings;
}

PAC5XXX_RAMFUNC uint8_t controller_get_errors(void)
TM_RAMFUNC uint8_t controller_get_errors(void)
{
return state.errors;
}
40 changes: 20 additions & 20 deletions firmware/src/controller/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,35 +83,35 @@ typedef struct

void Controller_ControlLoop(void);

PAC5XXX_RAMFUNC ControlState controller_get_state(void);
PAC5XXX_RAMFUNC void controller_set_state(ControlState new_state);
ControlState controller_get_state(void);
void controller_set_state(ControlState new_state);

PAC5XXX_RAMFUNC ControlMode controller_get_mode(void);
PAC5XXX_RAMFUNC void controller_set_mode(ControlMode mode);
ControlMode controller_get_mode(void);
void controller_set_mode(ControlMode mode);

inline void controller_calibrate(void) {controller_set_state(STATE_CALIBRATE);}
inline void controller_idle(void) {controller_set_state(STATE_IDLE);}
inline void controller_position_mode(void) {controller_set_mode(CTRL_POSITION);controller_set_state(STATE_CL_CONTROL);}
inline void controller_velocity_mode(void) {controller_set_mode(CTRL_VELOCITY);controller_set_state(STATE_CL_CONTROL);}
inline void controller_current_mode(void) {controller_set_mode(CTRL_CURRENT);controller_set_state(STATE_CL_CONTROL);}

PAC5XXX_RAMFUNC float controller_get_pos_setpoint_user_frame(void);
PAC5XXX_RAMFUNC void controller_set_pos_setpoint_user_frame(float value);
PAC5XXX_RAMFUNC float controller_get_vel_setpoint_user_frame(void);
PAC5XXX_RAMFUNC void controller_set_vel_setpoint_user_frame(float value);
float controller_get_pos_setpoint_user_frame(void);
void controller_set_pos_setpoint_user_frame(float value);
float controller_get_vel_setpoint_user_frame(void);
void controller_set_vel_setpoint_user_frame(float value);

PAC5XXX_RAMFUNC float controller_get_Iq_estimate(void);
PAC5XXX_RAMFUNC float controller_get_Iq_setpoint(void);
PAC5XXX_RAMFUNC void controller_set_Iq_setpoint(float value);
float controller_get_Iq_estimate(void);
float controller_get_Iq_setpoint(void);
void controller_set_Iq_setpoint(float value);

PAC5XXX_RAMFUNC float controller_get_Iq_estimate_user_frame(void);
PAC5XXX_RAMFUNC float controller_get_Iq_setpoint_user_frame(void);
PAC5XXX_RAMFUNC void controller_set_Iq_setpoint_user_frame(float value);
PAC5XXX_RAMFUNC float controller_get_Id_setpoint_user_frame(void);
float controller_get_Iq_estimate_user_frame(void);
float controller_get_Iq_setpoint_user_frame(void);
void controller_set_Iq_setpoint_user_frame(float value);
float controller_get_Id_setpoint_user_frame(void);

PAC5XXX_RAMFUNC float controller_get_Vq_setpoint_user_frame(void);
float controller_get_Vq_setpoint_user_frame(void);

PAC5XXX_RAMFUNC float controller_set_pos_vel_setpoints(float pos_setpoint, float vel_setpoint);
float controller_set_pos_vel_setpoints(float pos_setpoint, float vel_setpoint);

void controller_get_modulation_values(struct FloatTriplet *dc);

Expand Down Expand Up @@ -144,10 +144,10 @@ void controller_set_max_Ibrake(float value);

void controller_set_motion_plan(MotionPlan mp);

PAC5XXX_RAMFUNC void controller_update_I_gains(void);
void controller_update_I_gains(void);

PAC5XXX_RAMFUNC uint8_t controller_get_warnings(void);
PAC5XXX_RAMFUNC uint8_t controller_get_errors(void);
uint8_t controller_get_warnings(void);
uint8_t controller_get_errors(void);

ControllerConfig *Controller_GetConfig(void);
void Controller_RestoreConfig(ControllerConfig *config_);
Expand Down
5 changes: 3 additions & 2 deletions firmware/src/controller/trajectory_planner.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

#include <src/common.h>
#include <src/system/system.h>
#include <src/controller/controller.h>
#include <src/can/can_endpoints.h>
Expand Down Expand Up @@ -281,12 +282,12 @@ bool planner_set_deltat_dec(float deltat_dec)
return false;
}

PAC5XXX_RAMFUNC uint8_t planner_get_errors(void)
TM_RAMFUNC uint8_t planner_get_errors(void)
{
return state.errors;
}

PAC5XXX_RAMFUNC bool planner_evaluate(float t, MotionPlan *plan)
TM_RAMFUNC bool planner_evaluate(float t, MotionPlan *plan)
{
// We assume that t is zero at the start of trajectory
bool response = true;
Expand Down
4 changes: 2 additions & 2 deletions firmware/src/controller/trajectory_planner.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ bool planner_set_deltat_tot(float deltat_tot);
float planner_get_deltat_dec(void);
bool planner_set_deltat_dec(float deltat_dec);

PAC5XXX_RAMFUNC uint8_t planner_get_errors(void);
uint8_t planner_get_errors(void);

PAC5XXX_RAMFUNC bool planner_evaluate(float t, MotionPlan *plan);
bool planner_evaluate(float t, MotionPlan *plan);

#endif /* CONTROLLER_TRAJECTORY_PLANNER_H_ */
Loading

0 comments on commit 2954b2b

Please sign in to comment.