Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HAL_Linux: Implement force_safety_on/off in RCOutput_PCA9685, do force_safety_on before shutting down #18635

Merged
merged 2 commits into from Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions libraries/AP_HAL_Linux/HAL_Linux_Class.cpp
Expand Up @@ -413,6 +413,8 @@ void HAL_Linux::run(int argc, char* const argv[], Callbacks* callbacks) const
callbacks->loop();
}

// At least try to stop all PWM before shutting down
rcout->force_safety_on();
rcin->teardown();
I2CDeviceManager::from(i2c_mgr)->teardown();
SPIDeviceManager::from(spi)->teardown();
Expand Down
23 changes: 23 additions & 0 deletions libraries/AP_HAL_Linux/RCOutput_PCA9685.cpp
Expand Up @@ -167,6 +167,29 @@ void RCOutput_PCA9685::disable_ch(uint8_t ch)
write(ch, 0);
}

bool RCOutput_PCA9685::force_safety_on() {
if (!_dev || !_dev->get_semaphore()->take(10)) {
return false;
}
/* Shutdown before sleeping. */
_dev->write_register(PCA9685_RA_ALL_LED_OFF_H, PCA9685_ALL_LED_OFF_H_SHUT);
/* Put PCA9685 to sleep */
_dev->write_register(PCA9685_RA_MODE1, PCA9685_MODE1_SLEEP_BIT);

_dev->get_semaphore()->give();
return true;
}

void RCOutput_PCA9685::force_safety_off() {
if (!_dev || !_dev->get_semaphore()->take(10)) {
return;
}
/* Restart the device and enable auto-incremented write */
_dev->write_register(PCA9685_RA_MODE1,
PCA9685_MODE1_RESTART_BIT | PCA9685_MODE1_AI_BIT);
_dev->get_semaphore()->give();
}

void RCOutput_PCA9685::write(uint8_t ch, uint16_t period_us)
{
if (ch >= (PWM_CHAN_COUNT - _channel_offset)) {
Expand Down
2 changes: 2 additions & 0 deletions libraries/AP_HAL_Linux/RCOutput_PCA9685.h
Expand Up @@ -26,6 +26,8 @@ class RCOutput_PCA9685 : public AP_HAL::RCOutput {
uint16_t get_freq(uint8_t ch) override;
void enable_ch(uint8_t ch) override;
void disable_ch(uint8_t ch) override;
bool force_safety_on() override;
void force_safety_off() override;
void write(uint8_t ch, uint16_t period_us) override;
void cork() override;
void push() override;
Expand Down