41 #include <px4_platform_common/px4_config.h> 42 #include <px4_platform_common/defines.h> 43 #include <px4_platform_common/module.h> 44 #include <px4_platform_common/tasks.h> 45 #include <px4_platform_common/posix.h> 46 #include <px4_platform_common/getopt.h> 55 #include <arch/board/board.h> 99 int set_min_pwm(
int fd,
unsigned long max_channels,
int pwm_value);
101 int set_out(
int fd,
unsigned long max_channels,
float output);
103 int prepare(
int fd,
unsigned long *max_channels);
108 static void usage(
const char *reason);
114 PX4_ERR(
"%s", reason);
117 PRINT_MODULE_DESCRIPTION(
120 Application to test motor ramp up. 122 Before starting, make sure to stop any running attitude controller: 123 $ mc_rate_control stop 124 $ fw_att_control stop 126 When starting, a background task is started, runs for several seconds (as specified), then exits. 129 $ motor_ramp sine -a 1100 -r 0.5 133 PRINT_MODULE_USAGE_NAME_SIMPLE("motor_ramp",
"command");
134 PRINT_MODULE_USAGE_ARG(
"ramp|sine|square",
"mode",
false);
135 PRINT_MODULE_USAGE_PARAM_STRING(
'd',
"/dev/pwm_output0",
nullptr,
"Pwm output device",
true);
136 PRINT_MODULE_USAGE_PARAM_INT(
'a', 0, 900, 1500,
"Select minimum pwm duty cycle in usec",
false);
137 PRINT_MODULE_USAGE_PARAM_INT(
'b', 2000, 901, 2100,
"Select maximum pwm duty cycle in usec",
true);
138 PRINT_MODULE_USAGE_PARAM_FLOAT(
'r', 1.0
f, 0.0
f, 65536.0
f,
"Select motor ramp duration in sec",
true);
140 PRINT_MODULE_USAGE_PARAM_COMMENT(
"WARNING: motors will ramp up to full speed!");
156 const char *myoptarg =
nullptr;
157 bool error_flag =
false;
158 bool set_pwm_min =
false;
163 PX4_WARN(
"motor_ramp already running\n");
169 usage(
"missing parameters");
173 while ((ch = px4_getopt(argc, argv,
"d:a:b:r:", &myoptind, &myoptarg)) != EOF) {
177 if(!strcmp(myoptarg,
"/dev/pwm_output0") || !strcmp(myoptarg,
"/dev/pwm_output1")){
180 usage(
"pwm output device not found");
189 usage(
"min PWM not in range");
201 usage(
"max PWM not in range");
211 usage(
"ramp time must be greater than 0");
218 PX4_WARN(
"unrecognized flag");
227 PX4_WARN(
"pwm_min not set. use -a flag");
232 if (!strcmp(argv[myoptind],
"ramp")) {
235 }
else if (!strcmp(argv[myoptind],
"sine")) {
238 }
else if (!strcmp(argv[myoptind],
"square")) {
242 usage(
"selected mode not valid");
254 SCHED_PRIORITY_DEFAULT + 40,
257 (argv) ? (
char *
const *)&argv[2] : (
char *
const *)
nullptr);
264 return pwm_value >= 900 && pwm_value <= 1500;
269 return pwm_value <= 2100 && pwm_value >
_min_pwm;
280 for (
unsigned i = 0; i < max_channels; i++) {
281 pwm_values.
values[i] = pwm_value;
287 PX4_ERR(
"failed setting min values");
294 int set_out(
int fd,
unsigned long max_channels,
float output)
299 for (
unsigned i = 0; i < max_channels; i++) {
304 PX4_ERR(
"PWM_SERVO_SET(%d), value: %d", i, pwm);
329 PX4_ERR(
"ABORTING! Attitude control still active. Please ensure to shut down all controllers:\n" 330 "\tmc_rate_control stop\n" 331 "\tfw_att_control stop\n");
337 PX4_ERR(
"PWM_SERVO_GET_COUNT");
343 PX4_ERR(
"PWM_SERVO_SET_ARM_OK");
349 PX4_ERR(
"PWM_SERVO_ARM");
355 PX4_ERR(
"PWM_SERVO_SET_FORCE_SAFETY_OFF");
366 unsigned long max_channels = 0;
369 static unsigned servo_count;
381 PX4_ERR(
"PWM_SERVO_GET_COUNT");
389 for (
unsigned i = 0; i < servo_count; i++) {
392 PX4_ERR(
"PWM_SERVO_GET(%d)", i);
401 PX4_ERR(
"failed getting pwm min values");
408 PX4_ERR(
"Failed to Enter pwm test mode");
440 switch (ramp_state) {
442 PX4_INFO(
"setting pwm min: %d",
_min_pwm);
455 set_out(fd, max_channels, output);
466 output = 0.5f * (1.0f + sinf(M_TWOPI_F * timer /
_ramp_time - M_PI_2_F));
477 PX4_INFO(
"%s finished, waiting",
_mode_c);
480 set_out(fd, max_channels, output);
487 PX4_INFO(
"stopping");
491 set_out(fd, max_channels, output);
503 PX4_ERR(
"failed setting pwm min values");
510 for (
unsigned i = 0; i < servo_count; i++) {
513 PX4_ERR(
"PWM_SERVO_SET(%d)", i);
521 PX4_ERR(
"Failed to Exit pwm test mode");
int orb_copy(const struct orb_metadata *meta, int handle, void *buffer)
#define PWM_SERVO_SET_ARM_OK
set the 'ARM ok' bit, which activates the safety switch
static void usage(const char *reason)
Print the correct usage.
__EXPORT int motor_ramp_main(int argc, char *argv[])
motor_ramp management function.
#define PWM_SERVO_GET_COUNT
get the number of servos in *(unsigned *)arg
#define PWM_SERVO_ARM
arm all servo outputs handle by this driver
bool min_pwm_valid(int pwm_value)
static const char * _mode_c
int motor_ramp_thread_main(int argc, char *argv[])
Mainloop of motor_ramp.
static bool _thread_running
motor_ramp status flag
High-resolution timer with callouts and timekeeping.
static const char * _pwm_output_dev
#define PWM_SERVO_SET(_servo)
set a single servo to a specific value
static int _motor_ramp_task
Handle of motor_ramp task / thread.
int orb_subscribe(const struct orb_metadata *meta)
uint16_t values[PWM_OUTPUT_MAX_CHANNELS]
#define ORB_ID_VEHICLE_ATTITUDE_CONTROLS
#define PWM_SERVO_ENTER_TEST_MODE
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
int set_min_pwm(int fd, unsigned long max_channels, int pwm_value)
static hrt_abstime hrt_elapsed_time(const hrt_abstime *then)
Compute the delta between a timestamp taken in the past and now.
static bool _thread_should_exit
motor_ramp exit flag
Simple error/warning functions, heavily inspired by the BSD functions of the same names...
#define PWM_SERVO_SET_FORCE_SAFETY_OFF
force safety switch off (to disable use of safety switch)
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
#define PWM_SERVO_GET(_servo)
get a single specific servo value
#define PWM_SERVO_SET_MIN_PWM
set the minimum PWM value the output will send
#define PWM_SERVO_SET_MODE
int prepare(int fd, unsigned long *max_channels)
#define PWM_SERVO_GET_MIN_PWM
get the minimum PWM value the output will send
int orb_check(int handle, bool *updated)
bool max_pwm_valid(int pwm_value)
int set_out(int fd, unsigned long max_channels, float output)
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
#define PWM_SERVO_EXIT_TEST_MODE