2013-08-04 09:25:41 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2015-08-11 03:28:41 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include "AC_Sprayer.h"
|
2013-08-04 09:25:41 -03:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
// ------------------------------
|
|
|
|
|
2015-10-25 14:03:46 -03:00
|
|
|
const AP_Param::GroupInfo AC_Sprayer::var_info[] = {
|
2013-08-04 09:25:41 -03:00
|
|
|
// @Param: ENABLE
|
|
|
|
// @DisplayName: Sprayer enable/disable
|
|
|
|
// @Description: Allows you to enable (1) or disable (0) the sprayer
|
|
|
|
// @Values: 0:Disabled,1:Enabled
|
|
|
|
// @User: Standard
|
2016-08-04 15:30:03 -03:00
|
|
|
AP_GROUPINFO_FLAGS("ENABLE", 0, AC_Sprayer, _enabled, 0, AP_PARAM_FLAG_ENABLE),
|
2013-08-04 09:25:41 -03:00
|
|
|
|
|
|
|
// @Param: PUMP_RATE
|
|
|
|
// @DisplayName: Pump speed
|
|
|
|
// @Description: Desired pump speed when travelling 1m/s expressed as a percentage
|
|
|
|
// @Units: percentage
|
|
|
|
// @Range: 0 100
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("PUMP_RATE", 1, AC_Sprayer, _pump_pct_1ms, AC_SPRAYER_DEFAULT_PUMP_RATE),
|
|
|
|
|
|
|
|
// @Param: SPINNER
|
|
|
|
// @DisplayName: Spinner rotation speed
|
|
|
|
// @Description: Spinner's rotation speed in PWM (a higher rate will disperse the spray over a wider area horizontally)
|
|
|
|
// @Units: ms
|
|
|
|
// @Range: 1000 2000
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("SPINNER", 2, AC_Sprayer, _spinner_pwm, AC_SPRAYER_DEFAULT_SPINNER_PWM),
|
|
|
|
|
|
|
|
// @Param: SPEED_MIN
|
|
|
|
// @DisplayName: Speed minimum
|
|
|
|
// @Description: Speed minimum at which we will begin spraying
|
|
|
|
// @Units: cm/s
|
|
|
|
// @Range: 0 1000
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("SPEED_MIN", 3, AC_Sprayer, _speed_min, AC_SPRAYER_DEFAULT_SPEED_MIN),
|
|
|
|
|
2013-09-11 05:04:58 -03:00
|
|
|
// @Param: PUMP_MIN
|
|
|
|
// @DisplayName: Pump speed minimum
|
|
|
|
// @Description: Minimum pump speed expressed as a percentage
|
|
|
|
// @Units: percentage
|
|
|
|
// @Range: 0 100
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("PUMP_MIN", 4, AC_Sprayer, _pump_min_pct, AC_SPRAYER_DEFAULT_PUMP_MIN),
|
|
|
|
|
2013-08-04 09:25:41 -03:00
|
|
|
AP_GROUPEND
|
|
|
|
};
|
|
|
|
|
2013-08-12 10:25:34 -03:00
|
|
|
AC_Sprayer::AC_Sprayer(const AP_InertialNav* inav) :
|
2013-08-04 09:25:41 -03:00
|
|
|
_inav(inav),
|
|
|
|
_speed_over_min_time(0),
|
|
|
|
_speed_under_min_time(0)
|
|
|
|
{
|
|
|
|
AP_Param::setup_object_defaults(this, var_info);
|
|
|
|
|
|
|
|
// check for silly parameter values
|
2014-07-15 00:02:08 -03:00
|
|
|
if (_pump_pct_1ms < 0.0f || _pump_pct_1ms > 100.0f) {
|
2013-08-04 09:25:41 -03:00
|
|
|
_pump_pct_1ms.set_and_save(AC_SPRAYER_DEFAULT_PUMP_RATE);
|
|
|
|
}
|
|
|
|
if (_spinner_pwm < 0) {
|
|
|
|
_spinner_pwm.set_and_save(AC_SPRAYER_DEFAULT_SPINNER_PWM);
|
|
|
|
}
|
|
|
|
|
2015-06-08 01:27:32 -03:00
|
|
|
// initialise flags
|
|
|
|
_flags.spraying = false;
|
|
|
|
_flags.testing = false;
|
|
|
|
|
2013-08-04 09:25:41 -03:00
|
|
|
// To-Do: ensure that the pump and spinner servo channels are enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
void AC_Sprayer::enable(bool true_false)
|
|
|
|
{
|
|
|
|
// return immediately if no change
|
|
|
|
if (true_false == _enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set enabled/disabled parameter (in memory only)
|
|
|
|
_enabled = true_false;
|
|
|
|
|
|
|
|
// turn off the pump and spinner servos if necessary
|
|
|
|
if (!_enabled) {
|
|
|
|
// send output to pump channel
|
|
|
|
// To-Do: change 0 below to radio_min of pump servo
|
|
|
|
RC_Channel_aux::set_radio_to_min(RC_Channel_aux::k_sprayer_pump);
|
|
|
|
|
|
|
|
// send output to spinner channel
|
|
|
|
// To-Do: change 0 below to radio_min of spinner servo
|
|
|
|
RC_Channel_aux::set_radio_to_min(RC_Channel_aux::k_sprayer_spinner);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// update - adjust pwm of servo controlling pump speed according to the desired quantity and our horizontal speed
|
|
|
|
void
|
|
|
|
AC_Sprayer::update()
|
|
|
|
{
|
|
|
|
uint32_t now;
|
|
|
|
float ground_speed;
|
|
|
|
|
|
|
|
// exit immediately if we are disabled (perhaps set pwm values back to defaults)
|
|
|
|
if (!_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// exit immediately if the pump function has not been set-up for any servo
|
|
|
|
if (!RC_Channel_aux::function_assigned(RC_Channel_aux::k_sprayer_pump)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get horizontal velocity
|
2013-08-12 10:57:34 -03:00
|
|
|
const Vector3f &velocity = _inav->get_velocity();
|
2016-04-16 06:58:46 -03:00
|
|
|
ground_speed = norm(velocity.x,velocity.y);
|
2013-08-04 09:25:41 -03:00
|
|
|
|
|
|
|
// get the current time
|
2015-11-19 23:05:39 -04:00
|
|
|
now = AP_HAL::millis();
|
2013-08-04 09:25:41 -03:00
|
|
|
|
|
|
|
// check our speed vs the minimum
|
|
|
|
if (ground_speed >= _speed_min) {
|
|
|
|
// if we are not already spraying
|
2013-09-11 05:04:58 -03:00
|
|
|
if (!_flags.spraying) {
|
2013-08-04 09:25:41 -03:00
|
|
|
// set the timer if this is the first time we've surpassed the min speed
|
|
|
|
if (_speed_over_min_time == 0) {
|
|
|
|
_speed_over_min_time = now;
|
|
|
|
}else{
|
|
|
|
// check if we've been over the speed long enough to engage the sprayer
|
|
|
|
if((now - _speed_over_min_time) > AC_SPRAYER_DEFAULT_TURN_ON_DELAY) {
|
2013-09-11 05:04:58 -03:00
|
|
|
_flags.spraying = true;
|
2013-08-04 09:25:41 -03:00
|
|
|
_speed_over_min_time = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// reset the speed under timer
|
|
|
|
_speed_under_min_time = 0;
|
|
|
|
}else{
|
|
|
|
// we are under the min speed. If we are spraying
|
2013-09-11 05:04:58 -03:00
|
|
|
if (_flags.spraying) {
|
2013-08-04 09:25:41 -03:00
|
|
|
// set the timer if this is the first time we've dropped below the min speed
|
|
|
|
if (_speed_under_min_time == 0) {
|
|
|
|
_speed_under_min_time = now;
|
|
|
|
}else{
|
|
|
|
// check if we've been over the speed long enough to engage the sprayer
|
|
|
|
if((now - _speed_under_min_time) > AC_SPRAYER_DEFAULT_SHUT_OFF_DELAY) {
|
2013-09-11 05:04:58 -03:00
|
|
|
_flags.spraying = false;
|
2013-08-04 09:25:41 -03:00
|
|
|
_speed_under_min_time = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// reset the speed over timer
|
|
|
|
_speed_over_min_time = 0;
|
|
|
|
}
|
|
|
|
|
2013-09-11 05:04:58 -03:00
|
|
|
// if testing pump output speed as if travelling at 1m/s
|
|
|
|
if (_flags.testing) {
|
2014-07-15 00:02:08 -03:00
|
|
|
ground_speed = 100.0f;
|
2013-09-11 05:04:58 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// if spraying or testing update the pump servo position
|
|
|
|
if (_flags.spraying || _flags.testing) {
|
2015-11-27 13:11:58 -04:00
|
|
|
RC_Channel_aux::move_servo(RC_Channel_aux::k_sprayer_pump, MIN(MAX(ground_speed * _pump_pct_1ms, 100 *_pump_min_pct),10000),0,10000);
|
2013-08-04 11:24:40 -03:00
|
|
|
RC_Channel_aux::set_radio(RC_Channel_aux::k_sprayer_spinner, _spinner_pwm);
|
2013-08-04 09:25:41 -03:00
|
|
|
}else{
|
2013-08-04 11:24:40 -03:00
|
|
|
// ensure sprayer and spinner are off
|
2013-08-04 09:25:41 -03:00
|
|
|
RC_Channel_aux::set_radio_to_min(RC_Channel_aux::k_sprayer_pump);
|
2013-08-04 11:24:40 -03:00
|
|
|
RC_Channel_aux::set_radio_to_min(RC_Channel_aux::k_sprayer_spinner);
|
2013-08-04 09:25:41 -03:00
|
|
|
}
|
|
|
|
}
|