Firmware
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 
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 <perf/perf_counter.h>
51 #include <uORB/topics/esc_status.h>
52 #include <uORB/topics/actuator_outputs.h>
53 
54 
56 {
57 public:
58  UavcanEscController(uavcan::INode &node);
60 
61  int init();
62 
63  void update_outputs(float *outputs, unsigned num_outputs);
64 
65  void arm_all_escs(bool arm);
66  void arm_single_esc(int num, bool arm);
67 
68  void enable_idle_throttle_when_armed(bool value) { _run_at_idle_throttle_when_armed = value; }
69 
70 private:
74  void esc_status_sub_cb(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::Status> &msg);
75 
79  void orb_pub_timer_cb(const uavcan::TimerEvent &event);
80 
81 
82  static constexpr unsigned MAX_RATE_HZ = 200;
83  static constexpr unsigned ESC_STATUS_UPDATE_RATE_HZ = 10;
84  static constexpr unsigned UAVCAN_COMMAND_TRANSFER_PRIORITY = 5;
85 
86  typedef uavcan::MethodBinder<UavcanEscController *,
87  void (UavcanEscController::*)(const uavcan::ReceivedDataStructure<uavcan::equipment::esc::Status>&)>
88  StatusCbBinder;
89 
90  typedef uavcan::MethodBinder<UavcanEscController *, void (UavcanEscController::*)(const uavcan::TimerEvent &)>
91  TimerCbBinder;
92 
93  bool _armed = false;
94  bool _run_at_idle_throttle_when_armed = false;
95  esc_status_s _esc_status = {};
96  orb_advert_t _esc_status_pub = nullptr;
97  orb_advert_t _actuator_outputs_pub = nullptr;
98 
99  /*
100  * libuavcan related things
101  */
102  uavcan::MonotonicTime _prev_cmd_pub;
103  uavcan::INode &_node;
104  uavcan::Publisher<uavcan::equipment::esc::RawCommand> _uavcan_pub_raw_cmd;
105  uavcan::Subscriber<uavcan::equipment::esc::Status, StatusCbBinder> _uavcan_sub_status;
106  uavcan::TimerEventForwarder<TimerCbBinder> _orb_timer;
107 
108  /*
109  * ESC states
110  */
111  uint32_t _armed_mask = 0;
112  uint8_t _max_number_of_nonzero_outputs = 0;
113 
114  /*
115  * Perf counters
116  */
117  perf_counter_t _perfcnt_invalid_input = perf_alloc(PC_COUNT, "uavcan_esc_invalid_input");
118  perf_counter_t _perfcnt_scaling_error = perf_alloc(PC_COUNT, "uavcan_esc_scaling_error");
119 };
Definition: esc.hpp:55
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
count the number of times an event occurs
Definition: perf_counter.h:55
Header common to all counters.
Definition: perf_counter.cpp:65
__BEGIN_DECLS __EXPORT perf_counter_t perf_alloc(enum perf_counter_type type, const char *name)
Create a new local counter.
Definition: perf_counter.cpp:123
Performance measuring tools.