Skip to content

Commit

Permalink
[chibios] make log file closing faster and safer (paparazzi#2470)
Browse files Browse the repository at this point in the history
This features are needed for some board to close properly the log files.
This is now board specific, Tawaki, Apogee and Chimera are the only one
concerned so far.

- increase ADC predivider to relax DMA throughtput pressure on memory
- don't flush individually every open file, but close filsesystem to speed filesystem closing when power drop is detected
- relaxing all unnecessary PINs to input floating to save energy
  • Loading branch information
gautierhattenberger committed Nov 19, 2019
1 parent 5cf27ee commit 8fb4629
Show file tree
Hide file tree
Showing 11 changed files with 333 additions and 18 deletions.
11 changes: 11 additions & 0 deletions conf/airframes/ENAC/conf_enac.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@
settings_modules="[modules/gps_ubx_ucenter.xml] modules/gps.xml modules/nav_basic_fw.xml modules/guidance_full_pid_fw.xml modules/stabilization_adaptive_fw.xml modules/airspeed_adc.xml modules/imu_common.xml modules/ahrs_float_dcm.xml"
gui_color="#ffff7d7d0000"
/>
<aircraft
name="TAWAKI"
ac_id="1"
airframe="airframes/ENAC/fixed-wing/tawaki.xml"
radio="radios/cockpitSX.xml"
telemetry="telemetry/fixedwing_flight_recorder.xml"
flight_plan="flight_plans/basic.xml"
settings="settings/fixedwing_basic.xml"
settings_modules="modules/air_data.xml modules/nav_basic_fw.xml modules/guidance_full_pid_fw.xml modules/stabilization_adaptive_fw.xml modules/gps.xml modules/imu_common.xml modules/ahrs_float_dcm.xml modules/ahrs_float_cmpl_quat.xml modules/ins_float_invariant.xml"
gui_color="blue"
/>
<aircraft
name="WEASEL"
ac_id="19"
Expand Down
223 changes: 223 additions & 0 deletions conf/airframes/ENAC/fixed-wing/tawaki.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
<!DOCTYPE airframe SYSTEM "../../airframe.dtd">

<airframe name="Tawaki Test File">

<firmware name="fixedwing">
<!--configure name="RTOS_DEBUG" value="1"/-->
<!--configure name="CH_OPT" value="0 -g -ggdb3"/-->
<configure name="PERIODIC_FREQUENCY" value="100"/>
<define name="SERVO_HZ" type="60"/>

<target name="ap" board="tawaki_1.0">
<module name="radio_control" type="sbus"/>
<!--module name="ahrs" type="float_dcm"/>
<module name="ins" type="alt_float"/-->
<module name="ins" type="float_invariant">
<configure name="AHRS_PROPAGATE_FREQUENCY" value="100"/>
<configure name="AHRS_CORRECT_FREQUENCY" value="100"/>
<define name="SEND_INVARIANT_FILTER"/>
</module>
</target>

<target name="nps" board="pc">
<configure name="PERIODIC_FREQUENCY" value="100"/>
<module name="radio_control" type="ppm"/>
<module name="fdm" type="jsbsim"/>

<module name="ahrs" type="float_cmpl_quat">
<configure name="USE_MAGNETOMETER" value="1"/>
</module>
<module name="ins" type="alt_float"/>
</target>

<target name="sim" board="pc">
<module name="radio_control" type="ppm"/>
<module name="ahrs" type="float_dcm"/>
<module name="ins" type="alt_float"/>
</target>

<module name="telemetry" type="xbee_api"/>

<module name="board" type="tawaki">
<configure name="BOARD_TAWAKI_ROTATED" value="TRUE"/>
</module>

<module name="gps" type="ublox">
<configure name="GPS_BAUD" value="B115200"/>
</module>

<module name="control" type="new"/>
<module name="navigation"/>

<module name="air_data">
<define name="AIR_DATA_BARO_ABS_ID" value="BARO_BMP3_SENDER_ID"/>
<define name="AIR_DATA_CALC_AMSL_BARO" value="TRUE"/>
</module>

<!--configure name="SDLOG_USE_RTC" value="FALSE"/-->
<module name="tlsf"/>
<module name="pprzlog"/>
<module name="logger" type="sd_chibios"/>
<module name="flight_recorder"/>
</firmware>

<!-- commands section -->
<servos>
<servo name="MOTOR" no="1" min="1040" neutral="1040" max="2000"/>
<servo name="AILEVON_RIGHT" no="2" max="1900" neutral="1416" min="1100"/>
<servo name="AILEVON_LEFT" no="3" max="1100" neutral="1586" min="1900"/>
</servos>

<commands>
<axis name="THROTTLE" failsafe_value="0"/>
<axis name="ROLL" failsafe_value="0"/>
<axis name="PITCH" failsafe_value="0"/>
</commands>

<rc_commands>
<set command="THROTTLE" value="@THROTTLE"/>
<set command="ROLL" value="@ROLL"/>
<set command="PITCH" value="@PITCH"/>
</rc_commands>

<section name="MIXER">
<define name="AILEVON_AILERON_RATE" value="0.75"/>
<define name="AILEVON_ELEVATOR_RATE" value="0.75"/>
<define name="AILERON_DIFF" value="0.5"/>
<define name="COMMAND_ROLL_TRIM" value="0"/>
<define name="COMMAND_PITCH_TRIM" value="0"/>
</section>

<command_laws>
<let var="aileron" value="@ROLL * AILEVON_AILERON_RATE"/>
<let var="elevator" value="@PITCH * AILEVON_ELEVATOR_RATE"/>
<set servo="MOTOR" value="@THROTTLE"/>
<set servo="AILEVON_LEFT" value="$elevator - ($aileron > 0 ? AILERON_DIFF : 1) * $aileron"/>
<set servo="AILEVON_RIGHT" value="$elevator + ($aileron > 0 ? 1 : AILERON_DIFF) * $aileron"/>
</command_laws>


<section name="AUTO1" prefix="AUTO1_">
<define name="MAX_ROLL" value="45." unit="deg"/>
<define name="MAX_PITCH" value="30." unit="deg"/>
</section>

<section name="IMU" prefix="IMU_">
<define name="GYRO_P_SIGN" value="1"/>
<define name="GYRO_Q_SIGN" value="1"/>
<define name="GYRO_R_SIGN" value="1"/>

<define name="ACCEL_X_SIGN" value="1"/>
<define name="ACCEL_Y_SIGN" value="1"/>
<define name="ACCEL_Z_SIGN" value="1"/>
<define name="ACCEL_X_NEUTRAL" value="-41"/>
<define name="ACCEL_Y_NEUTRAL" value="1"/>
<define name="ACCEL_Z_NEUTRAL" value="24"/>
<define name="ACCEL_X_SENS" value="2.45166329295" integer="16"/>
<define name="ACCEL_Y_SENS" value="2.45072320868" integer="16"/>
<define name="ACCEL_Z_SENS" value="2.44974997157" integer="16"/>

<define name="MAG_X_SIGN" value="1"/>
<define name="MAG_Y_SIGN" value="1"/>
<define name="MAG_Z_SIGN" value="1"/>
<define name="MAG_X_NEUTRAL" value="1984"/>
<define name="MAG_Y_NEUTRAL" value="-528"/>
<define name="MAG_Z_NEUTRAL" value="1055"/>
<define name="MAG_X_SENS" value="0.676644619376" integer="16"/>
<define name="MAG_Y_SENS" value="0.667566939093" integer="16"/>
<define name="MAG_Z_SENS" value="0.663687314941" integer="16"/>

<define name="BODY_TO_IMU_PHI" value="0" unit="deg"/>
<define name="BODY_TO_IMU_THETA" value="0." unit="deg"/>
<define name="BODY_TO_IMU_PSI" value="0" unit="deg"/>
</section>

<section name="INS" prefix="INS_">
<!--muret-->
<define name="H_X" value="0.5180"/>
<define name="H_Y" value="-0.0071"/>
<define name="H_Z" value="0.8554"/>
</section>


<section name="BAT">
<define name="CALIB_AMP_A" value="0.0045360"/>
<define name="CALIB_AMP_B" value="2.8136"/>
<define name="CATASTROPHIC_BAT_LEVEL" value="9.3" unit="V"/>
<!--define name="MilliAmpereOfAdc(_adc)" value="(_adc-620)*4.536"/-->
</section>

<section name="MISC">
<define name="NOMINAL_AIRSPEED" value="12." unit="m/s"/>
<define name="CARROT" value="5." unit="s"/>
<define name="KILL_MODE_DISTANCE" value="(1.5*MAX_DIST_FROM_HOME)"/>
<define name="DEFAULT_CIRCLE_RADIUS" value="80."/>
<define name="UNLOCKED_HOME_MODE" value="TRUE"/>
</section>

<section name="BUNGEE" prefix="BUNGEE_TAKEOFF_">
<define name="HEIGHT" value="15."/>
<define name="DISTANCE" value="-10."/>
<define name="MIN_SPEED" value="2"/>
<define name="PITCH" value="12" unit="deg"/>
<define name="THROTTLE" value="1."/>
</section>


<section name="VERTICAL CONTROL" prefix="V_CTL_">
<define name="POWER_CTL_BAT_NOMINAL" value="11.1" unit="volt"/>
<!-- outer loop proportional gain -->
<define name="ALTITUDE_PGAIN" value="0.06"/>
<!-- outer loop saturation -->
<define name="ALTITUDE_MAX_CLIMB" value="3."/>
<!-- auto throttle inner loop -->
<define name="AUTO_THROTTLE_NOMINAL_CRUISE_THROTTLE" value="0.5"/>
<define name="AUTO_THROTTLE_MIN_CRUISE_THROTTLE" value="0.25"/>
<define name="AUTO_THROTTLE_MAX_CRUISE_THROTTLE" value="0.85"/>
<define name="AUTO_THROTTLE_LOITER_TRIM" value="1000"/>
<define name="AUTO_THROTTLE_DASH_TRIM" value="-1200"/>
<define name="AUTO_THROTTLE_CLIMB_THROTTLE_INCREMENT" value="0.08" unit="%/(m/s)"/>
<define name="AUTO_THROTTLE_PGAIN" value="0.011"/>
<define name="AUTO_THROTTLE_IGAIN" value="0.006"/>
<define name="AUTO_THROTTLE_PITCH_OF_VZ_PGAIN" value="0.13"/>
<define name="THROTTLE_SLEW" value="0.1"/>

<!-- TODO : CTRL_NEW "Climb loop (pitch)" -->
<define name="AUTO_PITCH_PGAIN" value="0.028"/>
<define name="AUTO_PITCH_DGAIN" value="0.013"/>
<define name="AUTO_PITCH_IGAIN" value="0.006"/>
<define name="AUTO_PITCH_MAX_PITCH" value="20" unit="deg"/>
<define name="AUTO_PITCH_MIN_PITCH" value="-20" unit="deg"/>

<define name="PITCH_TRIM" value="0." unit="deg"/>
</section>

<section name="HORIZONTAL CONTROL" prefix="H_CTL_">
<define name="COURSE_PGAIN" value="0.58"/>
<define name="ROLL_MAX_SETPOINT" value="45" unit="deg"/>
<define name="PITCH_MAX_SETPOINT" value="30." unit="deg"/>
<define name="PITCH_MIN_SETPOINT" value="-30." unit="deg"/>
<define name="ROLL_ATTITUDE_GAIN" value="10041"/>
<define name="ROLL_RATE_GAIN" value="1500"/>
<define name="PITCH_PGAIN" value="10672"/>
<define name="PITCH_DGAIN" value="1343"/>
<define name="AILERON_OF_THROTTLE" value="0.0"/>
<define name="PITCH_OF_ROLL" value="0.024"/>
</section>

<section name="FAILSAFE" prefix="FAILSAFE_">
<define name="DELAY_WITHOUT_GPS" value="2" unit="s"/>
<define name="DEFAULT_THROTTLE" value="0.3" unit="%"/>
<define name="DEFAULT_ROLL" value="0.17" unit="rad"/>
<define name="DEFAULT_PITCH" value="0.08" unit="rad"/>
<define name="HOME_RADIUS" value="100" unit="m"/>
</section>

<section name="SIMULATOR" prefix="NPS_">
<define name="JSBSIM_LAUNCHSPEED" value="15"/>
<define name="JSBSIM_MODEL" value="easystar" type="string"/>
<define name="SENSORS_PARAMS" value="nps_sensors_params_wind_estimator.h" type="string"/>
<define name="JS_AXIS_MODE" value="4"/>
</section>

</airframe>
6 changes: 6 additions & 0 deletions sw/airborne/arch/chibios/mcu_arch.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,9 @@ void mcu_arch_init(void)

}

void WEAK mcu_periph_energy_save(void)
{
// Default empty implementation
// see board.c file
}

5 changes: 5 additions & 0 deletions sw/airborne/arch/chibios/mcu_arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,9 @@ static inline void mcu_reset(void)
NVIC_SystemReset();
}

/** Call board specific energy saving
* Can be necessary for closing on power off
*/
extern void mcu_periph_energy_save(void);

#endif /* CHIBIOS_MCU_ARCH_H */
16 changes: 16 additions & 0 deletions sw/airborne/boards/apogee/chibios/v1.0/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,19 @@ bool mmc_lld_is_write_protected(MMCDriver *mmcp) {
*/
void boardInit(void) {
}

/** Energy saving procedure for SD log closing
*/
void mcu_periph_energy_save(void)
{
palSetLineMode(LINE_LED1, PAL_MODE_INPUT);
palSetLineMode(LINE_LED2, PAL_MODE_INPUT);
palSetLineMode(LINE_LED3, PAL_MODE_INPUT);
palSetLineMode(LINE_LED4, PAL_MODE_INPUT);
palSetLineMode(LINE_SPI1_CS, PAL_MODE_INPUT);
palSetLineMode(LINE_AUX1, PAL_MODE_INPUT);
palSetLineMode(LINE_AUX2, PAL_MODE_INPUT);
palSetLineMode(LINE_AUX3, PAL_MODE_INPUT);
palSetLineMode(LINE_AUX4, PAL_MODE_INPUT);
}

20 changes: 20 additions & 0 deletions sw/airborne/boards/chimera/chibios/v1.0/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,23 @@ bool mmc_lld_is_write_protected(MMCDriver *mmcp) {
void boardInit(void) {

}

/** Energy saving procedure for SD log closing
*/
void mcu_periph_energy_save(void)
{
palSetLineMode(LINE_LED1, PAL_MODE_INPUT);
palSetLineMode(LINE_LED2, PAL_MODE_INPUT);
palSetLineMode(LINE_LED3, PAL_MODE_INPUT);
palSetLineMode(LINE_LED4, PAL_MODE_INPUT);
palSetLineMode(LINE_SPI1_CS, PAL_MODE_INPUT);
palSetLineMode(LINE_AUX0, PAL_MODE_INPUT);
palSetLineMode(LINE_AUX1, PAL_MODE_INPUT);
palSetLineMode(LINE_AUX2, PAL_MODE_INPUT);
palSetLineMode(LINE_AUX3, PAL_MODE_INPUT);
palSetLineMode(LINE_AUX4, PAL_MODE_INPUT);
palSetLineMode(LINE_AUX5, PAL_MODE_INPUT);
palSetLineMode(LINE_AUX6, PAL_MODE_INPUT);
palSetLineMode(LINE_AUX7, PAL_MODE_INPUT);
}

21 changes: 21 additions & 0 deletions sw/airborne/boards/tawaki/chibios/v1.0/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,24 @@ bool mmc_lld_is_write_protected(MMCDriver *mmcp) {
void boardInit(void) {

}

/** Energy saving procedure for SD log closing
*/
void mcu_periph_energy_save(void)
{
palSetLineMode(LINE_D15_LED1, PAL_MODE_INPUT);
palSetLineMode(LINE_A10_LED2, PAL_MODE_INPUT);
palSetLineMode(LINE_C07_LED3, PAL_MODE_INPUT);
palSetLineMode(LINE_D10_LED4, PAL_MODE_INPUT);
palSetLineMode(LINE_E04_SPI4_CS_INTERNAL, PAL_MODE_INPUT);
palSetLineMode(LINE_B12_SPI2_CS_EXTERNAL, PAL_MODE_INPUT);
palSetLineMode(LINE_A00_AUX_A1, PAL_MODE_INPUT);
palSetLineMode(LINE_A01_AUX_A2, PAL_MODE_INPUT);
palSetLineMode(LINE_A02_AUX_A3, PAL_MODE_INPUT);
palSetLineMode(LINE_A06_AUX_A4, PAL_MODE_INPUT);
palSetLineMode(LINE_A03_AUX_B1, PAL_MODE_INPUT);
palSetLineMode(LINE_A07_AUX_B2, PAL_MODE_INPUT);
palSetLineMode(LINE_B00_AUX_B3, PAL_MODE_INPUT);
palSetLineMode(LINE_B01_AUX_B4, PAL_MODE_INPUT);
}

2 changes: 1 addition & 1 deletion sw/airborne/boards/tawaki/chibios/v1.0/mcuconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
/*
* ADC driver system settings.
*/
#define STM32_ADC_ADCPRE ADC_CCR_ADCPRE_DIV4
#define STM32_ADC_ADCPRE ADC_CCR_ADCPRE_DIV8
#define STM32_ADC_USE_ADC1 TRUE
#define STM32_ADC_USE_ADC2 FALSE
#define STM32_ADC_USE_ADC3 FALSE
Expand Down
25 changes: 19 additions & 6 deletions sw/airborne/modules/loggers/sdlog_chibios.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,23 @@ void sdlog_chibios_init(void)
void sdlog_chibios_finish(const bool flush)
{
if (pprzLogFile != -1) {
// disable all the LEDs to save energy and maximize chance to flush files
// disable all required periph to save energy and maximize chance to flush files
// to mass storage and avoid infamous dirty bit on filesystem
led_disable();
mcu_periph_energy_save();

// if a FF_FS_REENTRANT is true, we can umount fs without closing
// file, fatfs lock will assure that umount is done after a write,
// and umount will close all open files cleanly. Thats the fatest
// way to umount cleanly filesystem.
//
// if FF_FS_REENTRANT is false,
// we have to flush and close files before unmounting filesystem
#if FF_FS_REENTRANT == 0
sdLogCloseAllLogs(flush);
#else
(void) flush;
#endif

sdLogFinish();
pprzLogFile = 0;
#if FLIGHTRECORDER_SDLOG
Expand Down Expand Up @@ -218,15 +231,15 @@ static void thd_startlog(void *arg)
} else {
removeEmptyLogs(PPRZ_LOG_DIR, PPRZ_LOG_NAME, 50);
if (sdLogOpenLog(&pprzLogFile, PPRZ_LOG_DIR,
PPRZ_LOG_NAME, SDLOG_AUTO_FLUSH_PERIOD, true,
SDLOG_CONTIGUOUS_STORAGE_MEM, false) != SDLOG_OK) {
PPRZ_LOG_NAME, SDLOG_AUTO_FLUSH_PERIOD, LOG_APPEND_TAG_AT_CLOSE_DISABLED,
SDLOG_CONTIGUOUS_STORAGE_MEM, LOG_PREALLOCATION_DISABLED) != SDLOG_OK) {
sdOk = false;
}
#if FLIGHTRECORDER_SDLOG
removeEmptyLogs(FR_LOG_DIR, FLIGHTRECORDER_LOG_NAME, 50);
if (sdLogOpenLog(&flightRecorderLogFile, FR_LOG_DIR, FLIGHTRECORDER_LOG_NAME,
SDLOG_AUTO_FLUSH_PERIOD, false,
SDLOG_CONTIGUOUS_STORAGE_MEM, false) != SDLOG_OK) {
SDLOG_AUTO_FLUSH_PERIOD, LOG_APPEND_TAG_AT_CLOSE_DISABLED,
SDLOG_CONTIGUOUS_STORAGE_MEM, LOG_PREALLOCATION_DISABLED) != SDLOG_OK) {
sdOk = false;
}
#endif
Expand Down
4 changes: 4 additions & 0 deletions sw/airborne/modules/loggers/sdlog_chibios/sdLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ extern "C" {
#define SDLOG_NEED_QUEUE
#endif

#define LOG_PREALLOCATION_ENABLED true
#define LOG_PREALLOCATION_DISABLED false
#define LOG_APPEND_TAG_AT_CLOSE_ENABLED true
#define LOG_APPEND_TAG_AT_CLOSE_DISABLED false

#ifdef SDLOG_NEED_QUEUE
typedef struct LogMessage LogMessage;
Expand Down
Loading

0 comments on commit 8fb4629

Please sign in to comment.