PX4 Firmware
PX4 Autopilot Software http://px4.io
esc.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2014 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file esc.hpp
36  *
37  * UAVCAN <--> ORB bridge for ESC messages:
38  * uavcan.equipment.esc.RawCommand
39  * uavcan.equipment.esc.RPMCommand
40  * uavcan.equipment.esc.Status
41  *
42  * @author Pavel Kirienko <pavel.kirienko@gmail.com>
43  */
44 
45 #pragma once
46 
47 #include <uavcan/uavcan.hpp>
48 #include <uavcan/equipment/esc/RawCommand.hpp>
49 #include <uavcan/equipment/esc/Status.hpp>
50 #include <lib/perf/perf_counter.h>
53 #include <uORB/topics/esc_status.h>
54 #include <drivers/drv_hrt.h>
56 
58 {
59 public:
61  static constexpr unsigned MAX_RATE_HZ = 200; ///< XXX make this configurable
62  static constexpr uint16_t DISARMED_OUTPUT_VALUE = UINT16_MAX;
63 
64  UavcanEscController(uavcan::INode &node);
66 
67  int init();
68 
69  void update_outputs(bool stop_motors, uint16_t outputs[MAX_ACTUATORS], unsigned num_outputs);
70 
71  /**
72  * Sets the number of rotors
73  */
74  void set_rotor_count(uint8_t count) { _rotor_count = count; }
75 
77 
78 private:
79  /**
80  * ESC status message reception will be reported via this callback.
81  */
82  void esc_status_sub_cb(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::Status> &msg);
83 
84  /**
85  * ESC status will be published to ORB from this callback (fixed rate).
86  */
87  void orb_pub_timer_cb(const uavcan::TimerEvent &event);
88 
89  /**
90  * Checks all the ESCs freshness based on timestamp, if an ESC exceeds the timeout then is flagged offline.
91  */
92  uint8_t check_escs_status();
93 
94  static constexpr unsigned ESC_STATUS_UPDATE_RATE_HZ = 10;
95  static constexpr unsigned UAVCAN_COMMAND_TRANSFER_PRIORITY = 5; ///< 0..31, inclusive, 0 - highest, 31 - lowest
96 
97  typedef uavcan::MethodBinder<UavcanEscController *,
98  void (UavcanEscController::*)(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::Status>&)> StatusCbBinder;
99 
100  typedef uavcan::MethodBinder<UavcanEscController *,
101  void (UavcanEscController::*)(const uavcan::TimerEvent &)> TimerCbBinder;
102 
104 
106 
107  uint8_t _rotor_count{0};
108 
109  /*
110  * libuavcan related things
111  */
112  uavcan::MonotonicTime _prev_cmd_pub; ///< rate limiting
113  uavcan::INode &_node;
114  uavcan::Publisher<uavcan::equipment::esc::RawCommand> _uavcan_pub_raw_cmd;
115  uavcan::Subscriber<uavcan::equipment::esc::Status, StatusCbBinder> _uavcan_sub_status;
116  uavcan::TimerEventForwarder<TimerCbBinder> _orb_timer;
117 
118  /*
119  * ESC states
120  */
122 };
uavcan::MonotonicTime _prev_cmd_pub
rate limiting
Definition: esc.hpp:112
uavcan::INode & _node
Definition: esc.hpp:113
uavcan::MethodBinder< UavcanEscController *, void(UavcanEscController::*)(const uavcan::ReceivedDataStructure< uavcan::equipment::esc::Status > &)> StatusCbBinder
Definition: esc.hpp:98
esc_status_s _esc_status
Definition: esc.hpp:103
UavcanEscController(uavcan::INode &node)
Definition: esc.cpp:48
uint8_t _rotor_count
Definition: esc.hpp:107
static constexpr int MAX_ACTUATORS
uORB::PublicationMulti< esc_status_s > _esc_status_pub
Definition: esc.hpp:105
uavcan::Publisher< uavcan::equipment::esc::RawCommand > _uavcan_pub_raw_cmd
Definition: esc.hpp:114
High-resolution timer with callouts and timekeeping.
static constexpr int MAX_ACTUATORS
Definition: esc.hpp:60
uint8_t check_escs_status()
Checks all the ESCs freshness based on timestamp, if an ESC exceeds the timeout then is flagged offli...
Definition: esc.cpp:171
static char msg[NUM_MSG][CONFIG_USART1_TXBUFSIZE]
Definition: px4io.c:89
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
void esc_status_sub_cb(const uavcan::ReceivedDataStructure< uavcan::equipment::esc::Status > &msg)
ESC status message reception will be reported via this callback.
Definition: esc.cpp:143
static constexpr unsigned MAX_RATE_HZ
XXX make this configurable.
Definition: esc.hpp:61
static int max_output_value()
Definition: esc.hpp:76
uavcan::TimerEventForwarder< TimerCbBinder > _orb_timer
Definition: esc.hpp:116
uavcan::Subscriber< uavcan::equipment::esc::Status, StatusCbBinder > _uavcan_sub_status
Definition: esc.hpp:115
constexpr _Tp max(_Tp a, _Tp b)
Definition: Limits.hpp:60
static constexpr uint16_t DISARMED_OUTPUT_VALUE
Definition: esc.hpp:62
void set_rotor_count(uint8_t count)
Sets the number of rotors.
Definition: esc.hpp:74
void update_outputs(bool stop_motors, uint16_t outputs[MAX_ACTUATORS], unsigned num_outputs)
Definition: esc.cpp:80
static constexpr unsigned UAVCAN_COMMAND_TRANSFER_PRIORITY
0..31, inclusive, 0 - highest, 31 - lowest
Definition: esc.hpp:95
void orb_pub_timer_cb(const uavcan::TimerEvent &event)
ESC status will be published to ORB from this callback (fixed rate).
Definition: esc.cpp:159
static constexpr unsigned ESC_STATUS_UPDATE_RATE_HZ
Definition: esc.hpp:94
~UavcanEscController()
Definition: esc.cpp:57
uint8_t _max_number_of_nonzero_outputs
Definition: esc.hpp:121
Performance measuring tools.
uavcan::MethodBinder< UavcanEscController *, void(UavcanEscController::*)(const uavcan::TimerEvent &)> TimerCbBinder
Definition: esc.hpp:101