2017-07-11 23:01:48 -03:00
|
|
|
/*
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "AP_WheelEncoder.h"
|
|
|
|
#include "WheelEncoder_Quadrature.h"
|
2019-09-04 23:59:48 -03:00
|
|
|
#include "WheelEncoder_SITL_Quadrature.h"
|
2019-02-05 01:00:40 -04:00
|
|
|
#include <AP_Logger/AP_Logger.h>
|
2017-07-11 23:01:48 -03:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
// table of user settable parameters
|
|
|
|
const AP_Param::GroupInfo AP_WheelEncoder::var_info[] = {
|
|
|
|
// @Param: _TYPE
|
|
|
|
// @DisplayName: WheelEncoder type
|
|
|
|
// @Description: What type of WheelEncoder is connected
|
2019-09-04 23:59:48 -03:00
|
|
|
// @Values: 0:None,1:Quadrature,10:SITL Quadrature
|
2017-07-11 23:01:48 -03:00
|
|
|
// @User: Standard
|
2018-02-09 21:28:30 -04:00
|
|
|
AP_GROUPINFO_FLAGS("_TYPE", 0, AP_WheelEncoder, _type[0], 0, AP_PARAM_FLAG_ENABLE),
|
2017-07-11 23:01:48 -03:00
|
|
|
|
2017-07-19 09:28:45 -03:00
|
|
|
// @Param: _CPR
|
|
|
|
// @DisplayName: WheelEncoder counts per revolution
|
|
|
|
// @Description: WheelEncoder counts per full revolution of the wheel
|
|
|
|
// @Increment: 1
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("_CPR", 1, AP_WheelEncoder, _counts_per_revolution[0], WHEELENCODER_CPR_DEFAULT),
|
|
|
|
|
|
|
|
// @Param: _RADIUS
|
2018-08-08 01:24:36 -03:00
|
|
|
// @DisplayName: Wheel radius
|
|
|
|
// @Description: Wheel radius
|
|
|
|
// @Units: m
|
2017-07-11 23:01:48 -03:00
|
|
|
// @Increment: 0.001
|
|
|
|
// @User: Standard
|
2017-07-19 09:28:45 -03:00
|
|
|
AP_GROUPINFO("_RADIUS", 2, AP_WheelEncoder, _wheel_radius[0], WHEELENCODER_RADIUS_DEFAULT),
|
2017-07-11 23:01:48 -03:00
|
|
|
|
|
|
|
// @Param: _POS_X
|
2017-07-19 09:27:35 -03:00
|
|
|
// @DisplayName: Wheel's X position offset
|
|
|
|
// @Description: X position of the center of the wheel in body frame. Positive X is forward of the origin.
|
2017-07-11 23:01:48 -03:00
|
|
|
// @Units: m
|
2020-01-30 00:33:33 -04:00
|
|
|
// @Range: -5 5
|
2017-07-19 09:27:35 -03:00
|
|
|
// @Increment: 0.01
|
2018-08-08 01:24:36 -03:00
|
|
|
// @User: Standard
|
2017-07-11 23:01:48 -03:00
|
|
|
|
|
|
|
// @Param: _POS_Y
|
2017-07-19 09:27:35 -03:00
|
|
|
// @DisplayName: Wheel's Y position offset
|
|
|
|
// @Description: Y position of the center of the wheel in body frame. Positive Y is to the right of the origin.
|
2017-07-11 23:01:48 -03:00
|
|
|
// @Units: m
|
2020-01-30 00:33:33 -04:00
|
|
|
// @Range: -5 5
|
2017-07-19 09:27:35 -03:00
|
|
|
// @Increment: 0.01
|
2018-08-08 01:24:36 -03:00
|
|
|
// @User: Standard
|
2017-07-19 09:27:35 -03:00
|
|
|
|
|
|
|
// @Param: _POS_Z
|
|
|
|
// @DisplayName: Wheel's Z position offset
|
|
|
|
// @Description: Z position of the center of the wheel in body frame. Positive Z is down from the origin.
|
|
|
|
// @Units: m
|
2020-01-30 00:33:33 -04:00
|
|
|
// @Range: -5 5
|
2017-07-19 09:27:35 -03:00
|
|
|
// @Increment: 0.01
|
2018-08-08 01:24:36 -03:00
|
|
|
// @User: Standard
|
2017-07-19 09:27:35 -03:00
|
|
|
AP_GROUPINFO("_POS", 3, AP_WheelEncoder, _pos_offset[0], 0.0f),
|
2017-07-11 23:01:48 -03:00
|
|
|
|
|
|
|
// @Param: _PINA
|
|
|
|
// @DisplayName: Input Pin A
|
|
|
|
// @Description: Input Pin A
|
2021-07-14 22:46:31 -03:00
|
|
|
// @Values: -1:Disabled,50:AUX1,51:AUX2,52:AUX3,53:AUX4,54:AUX5,55:AUX6
|
2017-07-11 23:01:48 -03:00
|
|
|
// @User: Standard
|
2021-07-14 22:43:50 -03:00
|
|
|
AP_GROUPINFO("_PINA", 4, AP_WheelEncoder, _pina[0], -1),
|
2017-07-11 23:01:48 -03:00
|
|
|
|
|
|
|
// @Param: _PINB
|
|
|
|
// @DisplayName: Input Pin B
|
|
|
|
// @Description: Input Pin B
|
2021-07-14 22:46:31 -03:00
|
|
|
// @Values: -1:Disabled,50:AUX1,51:AUX2,52:AUX3,53:AUX4,54:AUX5,55:AUX6
|
2017-07-11 23:01:48 -03:00
|
|
|
// @User: Standard
|
2021-07-14 22:43:50 -03:00
|
|
|
AP_GROUPINFO("_PINB", 5, AP_WheelEncoder, _pinb[0], -1),
|
2017-07-11 23:01:48 -03:00
|
|
|
|
|
|
|
#if WHEELENCODER_MAX_INSTANCES > 1
|
|
|
|
// @Param: 2_TYPE
|
|
|
|
// @DisplayName: Second WheelEncoder type
|
|
|
|
// @Description: What type of WheelEncoder sensor is connected
|
2019-09-04 23:59:48 -03:00
|
|
|
// @Values: 0:None,1:Quadrature,10:SITL Quadrature
|
2017-07-11 23:01:48 -03:00
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("2_TYPE", 6, AP_WheelEncoder, _type[1], 0),
|
|
|
|
|
2017-07-19 09:28:45 -03:00
|
|
|
// @Param: 2_CPR
|
|
|
|
// @DisplayName: WheelEncoder 2 counts per revolution
|
|
|
|
// @Description: WheelEncoder 2 counts per full revolution of the wheel
|
|
|
|
// @Increment: 1
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("2_CPR", 7, AP_WheelEncoder, _counts_per_revolution[1], WHEELENCODER_CPR_DEFAULT),
|
|
|
|
|
|
|
|
// @Param: 2_RADIUS
|
2018-08-08 01:24:36 -03:00
|
|
|
// @DisplayName: Wheel2's radius
|
|
|
|
// @Description: Wheel2's radius
|
|
|
|
// @Units: m
|
2017-07-11 23:01:48 -03:00
|
|
|
// @Increment: 0.001
|
|
|
|
// @User: Standard
|
2017-07-19 09:28:45 -03:00
|
|
|
AP_GROUPINFO("2_RADIUS", 8, AP_WheelEncoder, _wheel_radius[1], WHEELENCODER_RADIUS_DEFAULT),
|
2017-07-11 23:01:48 -03:00
|
|
|
|
|
|
|
// @Param: 2_POS_X
|
2017-07-19 09:27:35 -03:00
|
|
|
// @DisplayName: Wheel2's X position offset
|
|
|
|
// @Description: X position of the center of the second wheel in body frame. Positive X is forward of the origin.
|
2017-07-11 23:01:48 -03:00
|
|
|
// @Units: m
|
2020-01-30 00:33:33 -04:00
|
|
|
// @Range: -5 5
|
2017-07-19 09:27:35 -03:00
|
|
|
// @Increment: 0.01
|
2018-08-08 01:24:36 -03:00
|
|
|
// @User: Standard
|
2017-07-11 23:01:48 -03:00
|
|
|
|
2017-07-19 09:27:35 -03:00
|
|
|
// @Param: 2_POS_Y
|
|
|
|
// @DisplayName: Wheel2's Y position offset
|
|
|
|
// @Description: Y position of the center of the second wheel in body frame. Positive Y is to the right of the origin.
|
2017-07-11 23:01:48 -03:00
|
|
|
// @Units: m
|
2020-01-30 00:33:33 -04:00
|
|
|
// @Range: -5 5
|
2017-07-19 09:27:35 -03:00
|
|
|
// @Increment: 0.01
|
2018-08-08 01:24:36 -03:00
|
|
|
// @User: Standard
|
2017-07-19 09:27:35 -03:00
|
|
|
|
|
|
|
// @Param: 2_POS_Z
|
|
|
|
// @DisplayName: Wheel2's Z position offset
|
|
|
|
// @Description: Z position of the center of the second wheel in body frame. Positive Z is down from the origin.
|
|
|
|
// @Units: m
|
2020-01-30 00:33:33 -04:00
|
|
|
// @Range: -5 5
|
2017-07-19 09:27:35 -03:00
|
|
|
// @Increment: 0.01
|
2018-08-08 01:24:36 -03:00
|
|
|
// @User: Standard
|
2017-07-19 09:27:35 -03:00
|
|
|
AP_GROUPINFO("2_POS", 9, AP_WheelEncoder, _pos_offset[1], 0.0f),
|
2017-07-11 23:01:48 -03:00
|
|
|
|
|
|
|
// @Param: 2_PINA
|
|
|
|
// @DisplayName: Second Encoder Input Pin A
|
|
|
|
// @Description: Second Encoder Input Pin A
|
2021-07-14 22:46:31 -03:00
|
|
|
// @Values: -1:Disabled,50:AUX1,51:AUX2,52:AUX3,53:AUX4,54:AUX5,55:AUX6
|
2017-07-11 23:01:48 -03:00
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("2_PINA", 10, AP_WheelEncoder, _pina[1], 53),
|
|
|
|
|
|
|
|
// @Param: 2_PINB
|
|
|
|
// @DisplayName: Second Encoder Input Pin B
|
|
|
|
// @Description: Second Encoder Input Pin B
|
2021-07-14 22:46:31 -03:00
|
|
|
// @Values: -1:Disabled,50:AUX1,51:AUX2,52:AUX3,53:AUX4,54:AUX5,55:AUX6
|
2017-07-11 23:01:48 -03:00
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("2_PINB", 11, AP_WheelEncoder, _pinb[1], 52),
|
|
|
|
#endif
|
|
|
|
|
|
|
|
AP_GROUPEND
|
|
|
|
};
|
|
|
|
|
2018-07-23 00:54:48 -03:00
|
|
|
AP_WheelEncoder::AP_WheelEncoder(void)
|
2017-07-11 23:01:48 -03:00
|
|
|
{
|
2020-11-27 04:17:01 -04:00
|
|
|
_singleton = this;
|
|
|
|
|
2017-07-11 23:01:48 -03:00
|
|
|
AP_Param::setup_object_defaults(this, var_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialise the AP_WheelEncoder class.
|
|
|
|
void AP_WheelEncoder::init(void)
|
|
|
|
{
|
|
|
|
if (num_instances != 0) {
|
|
|
|
// init called a 2nd time?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (uint8_t i=0; i<WHEELENCODER_MAX_INSTANCES; i++) {
|
2018-07-04 22:57:21 -03:00
|
|
|
switch ((WheelEncoder_Type)_type[i].get()) {
|
2019-09-04 23:59:48 -03:00
|
|
|
|
2018-07-04 22:57:21 -03:00
|
|
|
case WheelEncoder_TYPE_QUADRATURE:
|
2019-09-04 23:59:48 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
|
2018-07-04 22:57:21 -03:00
|
|
|
drivers[i] = new AP_WheelEncoder_Quadrature(*this, i, state[i]);
|
2019-09-04 23:59:48 -03:00
|
|
|
#endif
|
2018-07-04 22:57:21 -03:00
|
|
|
break;
|
2019-09-04 23:59:48 -03:00
|
|
|
|
|
|
|
case WheelEncoder_TYPE_SITL_QUADRATURE:
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2021-10-26 10:01:13 -03:00
|
|
|
drivers[i] = new AP_WheelEncoder_SITL_Quadrature(*this, i, state[i]);
|
2019-09-04 23:59:48 -03:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
2018-07-04 22:57:21 -03:00
|
|
|
case WheelEncoder_TYPE_NONE:
|
|
|
|
break;
|
2017-07-11 23:01:48 -03:00
|
|
|
}
|
2019-09-04 23:59:48 -03:00
|
|
|
|
2017-07-11 23:01:48 -03:00
|
|
|
|
|
|
|
if (drivers[i] != nullptr) {
|
|
|
|
// we loaded a driver for this instance, so it must be
|
|
|
|
// present (although it may not be healthy)
|
2018-07-04 22:57:21 -03:00
|
|
|
num_instances = i+1; // num_instances is a high-water-mark
|
2017-07-11 23:01:48 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update WheelEncoder state for all instances. This should be called by main loop
|
|
|
|
void AP_WheelEncoder::update(void)
|
|
|
|
{
|
|
|
|
for (uint8_t i=0; i<num_instances; i++) {
|
|
|
|
if (drivers[i] != nullptr && _type[i] != WheelEncoder_TYPE_NONE) {
|
|
|
|
drivers[i]->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-19 09:28:45 -03:00
|
|
|
|
2023-07-13 21:58:09 -03:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2019-02-05 01:00:40 -04:00
|
|
|
// log wheel encoder information
|
2021-02-01 12:26:35 -04:00
|
|
|
void AP_WheelEncoder::Log_Write() const
|
2019-02-05 01:00:40 -04:00
|
|
|
{
|
|
|
|
// return immediately if no wheel encoders are enabled
|
|
|
|
if (!enabled(0) && !enabled(1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct log_WheelEncoder pkt = {
|
|
|
|
LOG_PACKET_HEADER_INIT(LOG_WHEELENCODER_MSG),
|
|
|
|
time_us : AP_HAL::micros64(),
|
|
|
|
distance_0 : get_distance(0),
|
|
|
|
quality_0 : (uint8_t)get_signal_quality(0),
|
|
|
|
distance_1 : get_distance(1),
|
|
|
|
quality_1 : (uint8_t)get_signal_quality(1),
|
|
|
|
};
|
|
|
|
AP::logger().WriteBlock(&pkt, sizeof(pkt));
|
|
|
|
}
|
2023-07-13 21:58:09 -03:00
|
|
|
#endif
|
2019-02-05 01:00:40 -04:00
|
|
|
|
2017-07-11 23:01:48 -03:00
|
|
|
// check if an instance is healthy
|
|
|
|
bool AP_WheelEncoder::healthy(uint8_t instance) const
|
|
|
|
{
|
|
|
|
if (instance >= num_instances) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if an instance is activated
|
|
|
|
bool AP_WheelEncoder::enabled(uint8_t instance) const
|
|
|
|
{
|
|
|
|
if (instance >= num_instances) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// if no sensor type is selected, the sensor is not activated.
|
|
|
|
if (_type[instance] == WheelEncoder_TYPE_NONE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-19 09:28:45 -03:00
|
|
|
// get the counts per revolution of the encoder
|
|
|
|
uint16_t AP_WheelEncoder::get_counts_per_revolution(uint8_t instance) const
|
2017-07-11 23:01:48 -03:00
|
|
|
{
|
|
|
|
// for invalid instances return zero vector
|
|
|
|
if (instance >= WHEELENCODER_MAX_INSTANCES) {
|
2017-07-19 09:28:45 -03:00
|
|
|
return 0;
|
2017-07-11 23:01:48 -03:00
|
|
|
}
|
2017-07-19 09:28:45 -03:00
|
|
|
return (uint16_t)_counts_per_revolution[instance];
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the wheel radius in meters
|
|
|
|
float AP_WheelEncoder::get_wheel_radius(uint8_t instance) const
|
|
|
|
{
|
|
|
|
// for invalid instances return zero vector
|
|
|
|
if (instance >= WHEELENCODER_MAX_INSTANCES) {
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
return _wheel_radius[instance];
|
2017-07-11 23:01:48 -03:00
|
|
|
}
|
|
|
|
|
2017-11-06 12:24:10 -04:00
|
|
|
// return a 3D vector defining the position offset of the center of the wheel in meters relative to the body frame origin
|
|
|
|
const Vector3f &AP_WheelEncoder::get_pos_offset(uint8_t instance) const
|
2017-07-19 09:27:35 -03:00
|
|
|
{
|
|
|
|
// for invalid instances return zero vector
|
|
|
|
if (instance >= WHEELENCODER_MAX_INSTANCES) {
|
2017-11-06 12:24:10 -04:00
|
|
|
return pos_offset_zero;
|
2017-07-19 09:27:35 -03:00
|
|
|
}
|
|
|
|
return _pos_offset[instance];
|
|
|
|
}
|
2017-07-11 23:01:48 -03:00
|
|
|
|
2017-07-20 03:11:45 -03:00
|
|
|
// get total delta angle (in radians) measured by the wheel encoder
|
|
|
|
float AP_WheelEncoder::get_delta_angle(uint8_t instance) const
|
2017-07-11 23:01:48 -03:00
|
|
|
{
|
|
|
|
// for invalid instances return zero
|
|
|
|
if (instance >= WHEELENCODER_MAX_INSTANCES) {
|
|
|
|
return 0.0f;
|
|
|
|
}
|
2017-07-19 09:28:45 -03:00
|
|
|
// protect against divide by zero
|
|
|
|
if (_counts_per_revolution[instance] == 0) {
|
|
|
|
return 0.0f;
|
|
|
|
}
|
2017-07-20 03:11:45 -03:00
|
|
|
return M_2PI * state[instance].distance_count / _counts_per_revolution[instance];
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the total distance traveled in meters
|
|
|
|
float AP_WheelEncoder::get_distance(uint8_t instance) const
|
|
|
|
{
|
|
|
|
// for invalid instances return zero
|
|
|
|
return get_delta_angle(instance) * _wheel_radius[instance];
|
2017-07-11 23:01:48 -03:00
|
|
|
}
|
|
|
|
|
2018-08-07 10:18:30 -03:00
|
|
|
// get the instantaneous rate in radians/second
|
|
|
|
float AP_WheelEncoder::get_rate(uint8_t instance) const
|
|
|
|
{
|
|
|
|
// for invalid instances return zero
|
|
|
|
if (instance >= WHEELENCODER_MAX_INSTANCES) {
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// protect against divide by zero
|
|
|
|
if ((state[instance].dt_ms == 0) || _counts_per_revolution[instance] == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate delta_angle (in radians) per second
|
2018-11-05 18:08:56 -04:00
|
|
|
return M_2PI * (state[instance].dist_count_change / ((float)_counts_per_revolution[instance])) / (state[instance].dt_ms * 1e-3f);
|
2018-08-07 10:18:30 -03:00
|
|
|
}
|
|
|
|
|
2017-07-11 23:01:48 -03:00
|
|
|
// get the total number of sensor reading from the encoder
|
|
|
|
uint32_t AP_WheelEncoder::get_total_count(uint8_t instance) const
|
|
|
|
{
|
|
|
|
// for invalid instances return zero
|
|
|
|
if (instance >= WHEELENCODER_MAX_INSTANCES) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return state[instance].total_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the total distance traveled in meters
|
|
|
|
uint32_t AP_WheelEncoder::get_error_count(uint8_t instance) const
|
|
|
|
{
|
|
|
|
// for invalid instances return zero
|
|
|
|
if (instance >= WHEELENCODER_MAX_INSTANCES) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return state[instance].error_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the signal quality for a sensor
|
|
|
|
float AP_WheelEncoder::get_signal_quality(uint8_t instance) const
|
|
|
|
{
|
|
|
|
// protect against divide by zero
|
|
|
|
if (state[instance].total_count == 0) {
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
return constrain_float((1.0f - ((float)state[instance].error_count / (float)state[instance].total_count)) * 100.0f, 0.0f, 100.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the system time (in milliseconds) of the last update
|
|
|
|
uint32_t AP_WheelEncoder::get_last_reading_ms(uint8_t instance) const
|
|
|
|
{
|
|
|
|
// for invalid instances return zero
|
|
|
|
if (instance >= WHEELENCODER_MAX_INSTANCES) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return state[instance].last_reading_ms;
|
|
|
|
}
|
2020-11-27 04:17:01 -04:00
|
|
|
|
|
|
|
// singleton instance
|
|
|
|
AP_WheelEncoder *AP_WheelEncoder::_singleton;
|
|
|
|
|
|
|
|
namespace AP {
|
|
|
|
|
|
|
|
AP_WheelEncoder *wheelencoder()
|
|
|
|
{
|
|
|
|
return AP_WheelEncoder::get_singleton();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|