LCOV - code coverage report
Current view: top level - SITL - SIM_Volz.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 8 8 100.0 %
Date: 2026-09-09 11:02:08 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :    This program is free software: you can redistribute it and/or modify
       3             :    it under the terms of the GNU General Public License as published by
       4             :    the Free Software Foundation, either version 3 of the License, or
       5             :    (at your option) any later version.
       6             : 
       7             :    This program is distributed in the hope that it will be useful,
       8             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
       9             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      10             :    GNU General Public License for more details.
      11             : 
      12             :    You should have received a copy of the GNU General Public License
      13             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      14             :  */
      15             : /*
      16             :   Simulator for Volz servos
      17             : 
      18             : ./Tools/autotest/sim_vehicle.py --gdb --debug -v Plane -A --serial5=sim:volz --speedup=1 --console -f plane-redundant
      19             : 
      20             : param set SERIAL5_PROTOCOL 14  // Volz
      21             : param set SERIAL5_BAUD 115200
      22             : param set SERVO_VOLZ_MASK 1803
      23             : param set SIM_VOLZ_ENA 1
      24             : param set SIM_VOLZ_MASK 1803
      25             : reboot
      26             : 
      27             : param fetch
      28             : 
      29             : #param set SIM_VOLZ_FMASK 1  # fail mask
      30             : 
      31             : ./Tools/autotest/autotest.py --gdb --debug build.ArduCopter fly.ArduCopter.Volz
      32             : 
      33             : */
      34             : 
      35             : #pragma once
      36             : 
      37             : #include "SIM_config.h"
      38             : 
      39             : #if AP_SIM_VOLZ_ENABLED
      40             : 
      41             : #include <AP_Param/AP_Param.h>
      42             : 
      43             : #include "SITL_Input.h"
      44             : 
      45             : #include "SIM_SerialDevice.h"
      46             : 
      47             : namespace SITL {
      48             : 
      49             : class Volz : public SerialDevice {
      50             : public:
      51             : 
      52             :     Volz();
      53             : 
      54             :     // update state
      55             :     void update(const class Aircraft &aircraft);
      56             : 
      57             :     static const AP_Param::GroupInfo var_info[];
      58             : 
      59   187275482 :     bool enabled() const { return _enabled.get(); }
      60             : 
      61             :     void update_sitl_input_pwm(struct sitl_input &input);
      62             : 
      63             : private:
      64             : 
      65             :     // parameters / configuration
      66             :     AP_Int8  _enabled;  // enable FETtec servo sim
      67             :     AP_Int32 _output_mask;  // mask of servo outputs to override
      68             :     AP_Int32 _failed_mask;  // mask of servo outputs to mark as failed
      69             : 
      70             :     // packet abstraction; "struct Command" is used for both reading
      71             :     // and writing to the autopilot.
      72             :     enum class CommandId : uint8_t {
      73             :         SET_EXTENDED_POSITION = 0xDC,
      74             :         EXTENDED_POSITION_RESPONSE = 0x2C,
      75             :         READ_CURRENT = 0xB0,
      76             :         CURRENT_RESPONSE = 0x30,
      77             :         READ_VOLTAGE = 0xB1,
      78             :         VOLTAGE_RESPONSE = 0x31,
      79             :         READ_TEMPERATURE = 0xC0,
      80             :         TEMPERATURE_RESPONSE = 0x10,
      81             :     };
      82             :     struct PACKED Command {
      83             :         CommandId command_id;
      84             :         uint8_t actuator_id; // actuator send to or receiving from
      85             :         uint8_t arg1; // CMD dependant argument 1
      86             :         uint8_t arg2; // CMD dependant argument 2
      87             :         uint16_t crc;
      88             : 
      89       91350 :         uint16_t calculate_checksum() const {
      90       91350 :             return crc_crc16_ibm(0xffff, (uint8_t*)this, 4);
      91             :         }
      92             :         void update_checksum();
      93             :     };
      94             : 
      95             :     // reading-from-autopilot support:
      96             :     union {
      97             :         Command command;
      98             :         uint8_t buffer[128];
      99             :     } u;
     100             :     uint8_t buflen;
     101             :     bool shift_command_to_front_of_buffer();
     102             :     void consume_command();
     103             :     void process_command(const Command &command);
     104             :     void update_servos(const class Aircraft &aircraft);
     105             :     void update_input();
     106             : 
     107             :     // class creating a servo abstraction:
     108             :     class Servo {
     109             :     public:
     110             :         uint8_t id;
     111             : 
     112             :         float position;  // -1 to 1
     113             :         float desired_position;
     114             :         float pcb_temperature;
     115             :         float motor_temperature;
     116             :         float primary_current;
     117             :         float secondary_current;
     118             :         float primary_voltage;
     119             :         float secondary_voltage;
     120             : 
     121     1303416 :         uint16_t pwm() {
     122     1303416 :             if (failed) {
     123       18848 :                 return last_good_pwm;
     124             :             }
     125     1284568 :             last_good_pwm = 1500 + desired_position*500;
     126     1284568 :             return last_good_pwm;
     127             :         }
     128             : 
     129             :         bool failed;
     130             :         uint16_t last_good_pwm;
     131             :     };
     132             : 
     133             :     // the simulated servos:
     134             :     Servo servos[16];
     135             : 
     136             :     // last time we updated the servo physics:
     137             :     uint32_t last_servo_update_us;
     138             : 
     139             : 
     140             :     void log_Servo(const Volz::Servo &servo, const uint32_t now_us);
     141             : };
     142             : 
     143             : }
     144             : 
     145             : #endif  // AP_SIM_VOLZ_ENABLED

Generated by: LCOV version 1.14