PX4 Firmware
PX4 Autopilot Software http://px4.io
heater.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2018-19 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 heater.cpp
36  *
37  * @author Mark Sauder <mcsauder@gmail.com>
38  * @author Alex Klimaj <alexklimaj@gmail.com>
39  * @author Jake Dahl <dahl.jakejacob@gmail.com>
40  * @author Mohammed Kabir <mhkabir@mit.edu>
41  */
42 
43 #include "heater.h"
44 
45 #include <px4_platform_common/getopt.h>
46 #include <px4_platform_common/log.h>
47 #include <drivers/drv_hrt.h>
48 
49 #ifndef GPIO_HEATER_OUTPUT
50 #error "To use the heater driver, the board_config.h must define and initialize GPIO_HEATER_OUTPUT"
51 #endif
52 
54  ModuleParams(nullptr),
55  ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::lp_default)
56 {
57  // Initialize heater to off state
58  px4_arch_configgpio(GPIO_HEATER_OUTPUT);
59 }
60 
62 {
63  // Reset heater to off state
64  px4_arch_configgpio(GPIO_HEATER_OUTPUT);
65 }
66 
67 int Heater::custom_command(int argc, char *argv[])
68 {
69  // Check if the driver is running.
70  if (!is_running()) {
71  PX4_INFO("not running");
72  return PX4_ERROR;
73  }
74 
75  return print_usage("Unrecognized command.");
76 }
77 
79 {
80  if (should_exit()) {
81  exit_and_cleanup();
82  return;
83  }
84 
85  if (_heater_on) {
86  // Turn the heater off.
87  px4_arch_gpiowrite(GPIO_HEATER_OUTPUT, 0);
88  _heater_on = false;
89 
90  } else {
91  update_params(false);
92 
94 
95  // Obtain the current IMU sensor temperature.
97 
98  // Calculate the temperature delta between the setpoint and reported temperature.
99  float temperature_delta = _param_sens_imu_temp.get() - _sensor_temperature;
100 
101  // Modulate the heater time on with a feedforward/PI controller.
102  _proportional_value = temperature_delta * _param_sens_imu_temp_p.get();
103  _integrator_value += temperature_delta * _param_sens_imu_temp_i.get();
104 
105  // Constrain the integrator value to no more than 25% of the duty cycle.
107 
108  // Calculate the duty cycle. This is a value between 0 and 1.
109  float duty = _proportional_value + _integrator_value;
110 
111  _controller_time_on_usec = (int)(duty * (float)_controller_period_usec);
112 
113  // Constrain the heater time within the allowable duty cycle.
115 
116  // Turn the heater on.
117  _heater_on = true;
118  px4_arch_gpiowrite(GPIO_HEATER_OUTPUT, 1);
119  }
120 
121  // Schedule the next cycle.
122  if (_heater_on) {
123  ScheduleDelayed(_controller_time_on_usec);
124 
125  } else {
127  }
128 }
129 
131 {
132  // Get the total number of accelerometer instances.
133  uint8_t number_of_imus = orb_group_count(ORB_ID(sensor_accel));
134 
135  // Check each instance for the correct ID.
136  for (uint8_t x = 0; x < number_of_imus; x++) {
137  _sensor_accel_sub = uORB::Subscription{ORB_ID(sensor_accel), x};
138 
139  if (!_sensor_accel_sub.advertised()) {
140  continue;
141  }
142 
144 
145  // If the correct ID is found, exit the for-loop with _sensor_accel_sub pointing to the correct instance.
146  if (_sensor_accel.device_id == (uint32_t)_param_sens_temp_id.get()) {
147  break;
148  }
149  }
150 
151  // Exit the driver if the sensor ID does not match the desired sensor.
152  if (_sensor_accel.device_id != (uint32_t)_param_sens_temp_id.get()) {
153  request_stop();
154  PX4_ERR("Could not identify IMU sensor.");
155  }
156 }
157 
159 {
160  PX4_INFO("Sensor ID: %d - Temperature: %3.3fC, Setpoint: %3.2fC, Heater State: %s",
162  (double)_sensor_temperature,
163  (double)_param_sens_imu_temp.get(),
164  _heater_on ? "On" : "Off");
165 
166  return PX4_OK;
167 }
168 
170 {
171  update_params(true);
173 
174  ScheduleNow();
175 
176  return PX4_OK;
177 }
178 
179 int Heater::task_spawn(int argc, char *argv[])
180 {
181  Heater *heater = new Heater();
182 
183  if (!heater) {
184  PX4_ERR("driver allocation failed");
185  return PX4_ERROR;
186  }
187 
188  _object.store(heater);
189  _task_id = task_id_is_work_queue;
190 
191  heater->start();
192 
193  return 0;
194 }
195 
196 void Heater::update_params(const bool force)
197 {
198  // check for parameter updates
199  if (_parameter_update_sub.updated() || force) {
200  // clear update
201  parameter_update_s pupdate;
202  _parameter_update_sub.copy(&pupdate);
203 
204  // update parameters from storage
205  ModuleParams::updateParams();
206  }
207 }
208 
209 int Heater::print_usage(const char *reason)
210 {
211  if (reason) {
212  printf("%s\n\n", reason);
213  }
214 
215  PRINT_MODULE_DESCRIPTION(
216  R"DESCR_STR(
217 ### Description
218 Background process running periodically on the LP work queue to regulate IMU temperature at a setpoint.
219 
220 This task can be started at boot from the startup scripts by setting SENS_EN_THERMAL or via CLI.
221 )DESCR_STR");
222 
223  PRINT_MODULE_USAGE_NAME("heater", "system");
224  PRINT_MODULE_USAGE_COMMAND("start");
225  PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
226 
227  return 0;
228 }
229 
230 /**
231  * Main entry point for the heater driver module
232  */
233 int heater_main(int argc, char *argv[])
234 {
235  return Heater::main(argc, argv);
236 }
constexpr _Tp constrain(_Tp val, _Tp min_val, _Tp max_val)
Definition: Limits.hpp:66
static int custom_command(int argc, char *argv[])
main Main entry point to the module that should be called directly from the module&#39;s main method...
Definition: heater.cpp:67
int main(int argc, char **argv)
Definition: main.cpp:3
uORB::Subscription _sensor_accel_sub
Definition: heater.h:181
uORB::Subscription _parameter_update_sub
Definition: heater.h:177
static bool is_running()
Definition: dataman.cpp:415
bool _heater_on
Definition: heater.h:173
High-resolution timer with callouts and timekeeping.
int _controller_time_on_usec
Definition: heater.h:171
void initialize_topics()
Called once to initialize uORB topics.
Definition: heater.cpp:130
int _controller_period_usec
Definition: heater.h:169
void Run() override
Calculates the heater element on/off time, carries out closed loop feedback and feedforward temperatu...
Definition: heater.cpp:78
virtual ~Heater()
Definition: heater.cpp:61
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
Heater()
Definition: heater.cpp:53
float _integrator_value
Definition: heater.h:175
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
bool updated()
Check if there is a new update.
static int print_usage(const char *reason=nullptr)
Prints the module usage to the nuttshell console.
Definition: heater.cpp:209
int orb_group_count(const struct orb_metadata *meta)
Get the number of published instances of a topic group.
Definition: uORB.cpp:110
int start()
Initiates the heater driver work queue, starts a new background task, and fails if it is already runn...
Definition: heater.cpp:169
float _proportional_value
Definition: heater.h:179
uint32_t device_id
Definition: sensor_accel.h:55
Definition: bst.cpp:62
bool update(void *dst)
Update the struct.
float _sensor_temperature
Definition: heater.h:184
static int task_spawn(int argc, char *argv[])
Initializes the class in the same context as the work queue and starts the background listener...
Definition: heater.cpp:179
int print_status()
Reports curent status and diagnostic information about the heater driver.
Definition: heater.cpp:158
sensor_accel_s _sensor_accel
Definition: heater.h:182
bool copy(void *dst)
Copy the struct.
Definition: heater.h:64
void update_params(const bool force=false)
Updates and checks for updated uORB parameters.
Definition: heater.cpp:196
int heater_main(int argc, char *argv[])
Main entry point for the heater driver module.
Definition: heater.cpp:233