diff --git a/libraries/AP_Gripper/AP_Gripper.cpp b/libraries/AP_Gripper/AP_Gripper.cpp new file mode 100644 index 0000000000..52f6e8074a --- /dev/null +++ b/libraries/AP_Gripper/AP_Gripper.cpp @@ -0,0 +1,81 @@ +#include "AP_Gripper.h" + +#include "AP_Gripper_Servo.h" + +extern const AP_HAL::HAL& hal; + +#define GRIPPER_GRAB_PWM_DEFAULT 1900 +#define GRIPPER_RELEASE_PWM_DEFAULT 1100 + +const AP_Param::GroupInfo AP_Gripper::var_info[] = { + // @Param: ENABLE + // @DisplayName: Gripper Enable/Disable + // @Description: Gripper enable/disable + // @User: Standard + // @Values: 0:Disabled, 1:Enabled + AP_GROUPINFO_FLAGS("ENABLE", 0, AP_Gripper, _enabled, 0, AP_PARAM_FLAG_ENABLE), + + // @Param: TYPE + // @DisplayName: Gripper Type + // @Description: Gripper enable/disable + // @User: Standard + // @Values: 0:None,1:Servo,2:EPM + AP_GROUPINFO("TYPE", 1, AP_Gripper, config.type, 0), + + // @Param: GRAB + // @DisplayName: Gripper Grab PWM + // @Description: PWM value sent to Gripper to initiate grabbing the cargo + // @User: Advanced + // @Range: 1000 2000 + AP_GROUPINFO("GRAB", 2, AP_Gripper, config.grab_pwm, GRIPPER_GRAB_PWM_DEFAULT), + + // @Param: RELEASE + // @DisplayName: Gripper Release PWM + // @Description: PWM value sent to Gripper to release the cargo + // @User: Advanced + // @Range: 1000 2000 + AP_GROUPINFO("RELEASE", 3, AP_Gripper, config.release_pwm, GRIPPER_RELEASE_PWM_DEFAULT), + + AP_GROUPEND +}; + +AP_Gripper::AP_Gripper() +{ + AP_Param::setup_object_defaults(this, var_info); +} + +void AP_Gripper::init() +{ + // return immediately if not enabled + if (!_enabled.get()) { + return; + } + + switch(config.type.get()) { + case 0: + break; + case 1: + backend = new AP_Gripper_Servo(config); + break; + default: + break; + } + if (backend != nullptr) { + backend->init(); + } +} + +// update - should be called at at least 10hz +#define PASS_TO_BACKEND(function_name) \ + void AP_Gripper::function_name() \ + { \ + if (backend != nullptr) { \ + backend->function_name(); \ + } \ + } + +PASS_TO_BACKEND(grab) +PASS_TO_BACKEND(release) +PASS_TO_BACKEND(update) + +#undef PASS_TO_BACKEND diff --git a/libraries/AP_Gripper/AP_Gripper.h b/libraries/AP_Gripper/AP_Gripper.h new file mode 100644 index 0000000000..af95b6ef96 --- /dev/null +++ b/libraries/AP_Gripper/AP_Gripper.h @@ -0,0 +1,52 @@ +/* + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +#pragma once + +#include + +class AP_Gripper_Backend; + +class AP_Gripper { +public: + AP_Gripper(); + + // initialise the gripper + void init(); + + // grab - move the servo to the grab position + void grab(); + + // release - move the servo output to the release position + void release(); + + // update - should be called at at least 10hz + void update(); + + static const struct AP_Param::GroupInfo var_info[]; + + // parameters + AP_Int8 _enabled; // grabber enable/disable + + struct Backend_Config { + AP_Int8 type; // grabber type (e.g. EPM or servo) + AP_Int16 grab_pwm; // PWM value sent to Gripper to initiate grabbing the cargo + AP_Int16 release_pwm; // PWM value sent to Gripper to release the cargo + } config; + +private: + + AP_Gripper_Backend *backend; +}; diff --git a/libraries/AP_Gripper/AP_Gripper_Backend.cpp b/libraries/AP_Gripper/AP_Gripper_Backend.cpp new file mode 100644 index 0000000000..5e82144491 --- /dev/null +++ b/libraries/AP_Gripper/AP_Gripper_Backend.cpp @@ -0,0 +1,14 @@ +#include "AP_Gripper_Backend.h" + +extern const AP_HAL::HAL& hal; + +void AP_Gripper_Backend::init() +{ + init_gripper(); +} + +// update - should be called at at least 10hz +void AP_Gripper_Backend::update() +{ + update_gripper(); +} diff --git a/libraries/AP_Gripper/AP_Gripper_Backend.h b/libraries/AP_Gripper/AP_Gripper_Backend.h new file mode 100644 index 0000000000..f60c580ed6 --- /dev/null +++ b/libraries/AP_Gripper/AP_Gripper_Backend.h @@ -0,0 +1,46 @@ +/* + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +#pragma once + +#include + +class AP_Gripper_Backend { +public: + AP_Gripper_Backend(struct AP_Gripper::Backend_Config &_config) : + config(_config) { } + + // initialise the gripper backend + void init(); + + // update - should be called at at least 10hz + void update(); + + // grab - move the servo to the grab position + virtual void grab() = 0; + + // release - move the servo output to the release position + virtual void release() = 0; + + // type-specific intiailisations: + virtual void init_gripper() = 0; + + // type-specific periodic updates: + virtual void update_gripper() { }; + +protected: + + struct AP_Gripper::Backend_Config &config; +}; diff --git a/libraries/AP_Gripper/AP_Gripper_Servo.cpp b/libraries/AP_Gripper/AP_Gripper_Servo.cpp new file mode 100644 index 0000000000..b542f8f50c --- /dev/null +++ b/libraries/AP_Gripper/AP_Gripper_Servo.cpp @@ -0,0 +1,24 @@ +#include + +extern const AP_HAL::HAL& hal; + +void AP_Gripper_Servo::init_gripper() +{ + // move the servo to the release position + RC_Channel_aux::set_radio(RC_Channel_aux::k_gripper, config.release_pwm); +} + +void AP_Gripper_Servo::grab() +{ + // move the servo to the release position + RC_Channel_aux::set_radio(RC_Channel_aux::k_gripper, config.grab_pwm); +} + +void AP_Gripper_Servo::release() +{ + // move the servo to the release position + RC_Channel_aux::set_radio(RC_Channel_aux::k_gripper, config.release_pwm); +} + + // type-specific periodic updates: +void AP_Gripper_Servo::update_gripper() { }; diff --git a/libraries/AP_Gripper/AP_Gripper_Servo.h b/libraries/AP_Gripper/AP_Gripper_Servo.h new file mode 100644 index 0000000000..606c61fc1c --- /dev/null +++ b/libraries/AP_Gripper/AP_Gripper_Servo.h @@ -0,0 +1,40 @@ +/* + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +#pragma once + +#include +#include + +class AP_Gripper_Servo : public AP_Gripper_Backend { +public: + + AP_Gripper_Servo(struct AP_Gripper::Backend_Config &_config) : + AP_Gripper_Backend(_config) { } + + // grab - move the servo to the grab position + void grab() override; + + // release - move the servo output to the release position + void release() override; + +protected: + + // type-specific intiailisations: + void init_gripper() override; + + // type-specific periodic updates: + void update_gripper() override; +};