Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Tools/AP_Periph/hardpoint.cpp
Views: 1862
#include "AP_Periph.h"12#ifdef HAL_PERIPH_ENABLE_PWM_HARDPOINT34#include <AP_HAL/AP_HAL.h>56extern const AP_HAL::HAL &hal;78/*9hardpoint support10*/1112#include <dronecan_msgs.h>1314void AP_Periph_FW::pwm_hardpoint_init()15{16hal.gpio->attach_interrupt(17PWM_HARDPOINT_PIN,18FUNCTOR_BIND_MEMBER(&AP_Periph_FW::pwm_irq_handler, void, uint8_t, bool, uint32_t), AP_HAL::GPIO::INTERRUPT_BOTH);19}2021/*22called on PWM pin transition23*/24void AP_Periph_FW::pwm_irq_handler(uint8_t pin, bool pin_state, uint32_t timestamp)25{26if (pin_state == 0 && pwm_hardpoint.last_state == 1 && pwm_hardpoint.last_ts_us != 0) {27uint32_t width = timestamp - pwm_hardpoint.last_ts_us;28if (width > 500 && width < 2500) {29pwm_hardpoint.pwm_value = width;30if (width > pwm_hardpoint.highest_pwm) {31pwm_hardpoint.highest_pwm = width;32}33}34}35pwm_hardpoint.last_state = pin_state;36pwm_hardpoint.last_ts_us = timestamp;37}3839void AP_Periph_FW::pwm_hardpoint_update()40{41uint32_t now = AP_HAL::millis();42// send at 10Hz43void *save = hal.scheduler->disable_interrupts_save();44uint16_t value = pwm_hardpoint.highest_pwm;45pwm_hardpoint.highest_pwm = 0;46hal.scheduler->restore_interrupts(save);47float rate = g.hardpoint_rate;48rate = constrain_float(rate, 10, 100);49if (value > 0 && now - pwm_hardpoint.last_send_ms >= 1000U/rate) {50pwm_hardpoint.last_send_ms = now;51uavcan_equipment_hardpoint_Command cmd {};52cmd.hardpoint_id = g.hardpoint_id;53cmd.command = value;5455uint8_t buffer[UAVCAN_EQUIPMENT_HARDPOINT_COMMAND_MAX_SIZE];56uint16_t total_size = uavcan_equipment_hardpoint_Command_encode(&cmd, buffer, !canfdout());57canard_broadcast(UAVCAN_EQUIPMENT_HARDPOINT_COMMAND_SIGNATURE,58UAVCAN_EQUIPMENT_HARDPOINT_COMMAND_ID,59CANARD_TRANSFER_PRIORITY_LOW,60&buffer[0],61total_size);62}63}6465#endif // HAL_PERIPH_ENABLE_PWM_HARDPOINT666768