2018-05-16 19:31:22 -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_RangeFinder_Wasp.h"
2019-11-01 07:23:16 -03:00
# include <AP_HAL/AP_HAL.h>
2018-05-16 19:31:22 -03:00
# include <ctype.h>
extern const AP_HAL : : HAL & hal ;
const AP_Param : : GroupInfo AP_RangeFinder_Wasp : : var_info [ ] = {
// @Param: WSP_MAVG
// @DisplayName: Moving Average Range
// @Description: Sets the number of historic range results to use for calculating the current range result. When MAVG is greater than 1, the current range result will be the current measured value averaged with the N-1 previous results
2018-05-24 23:00:02 -03:00
// @Range: 0 255
2018-05-16 19:31:22 -03:00
// @User: Advanced
AP_GROUPINFO ( " WSP_MAVG " , 1 , AP_RangeFinder_Wasp , mavg , 4 ) ,
// @Param: WSP_MEDF
// @DisplayName: Moving Median Filter
// @Description: Sets the window size for the real-time median filter. When MEDF is greater than 0 the median filter is active
2018-05-24 23:00:02 -03:00
// @Range: 0 255
2018-05-16 19:31:22 -03:00
// @User: Advanced
AP_GROUPINFO ( " WSP_MEDF " , 2 , AP_RangeFinder_Wasp , medf , 4 ) ,
// @Param: WSP_FRQ
// @DisplayName: Frequency
// @Description: Sets the repetition frequency of the ranging operation in Hertz. Upon entering the desired frequency the system will calculate the nearest frequency that it can handle according to the resolution of internal timers.
2018-05-24 23:00:02 -03:00
// @Range: 0 10000
2018-05-16 19:31:22 -03:00
// @User: Advanced
2018-05-30 00:47:08 -03:00
AP_GROUPINFO ( " WSP_FRQ " , 3 , AP_RangeFinder_Wasp , frq , 20 ) ,
2018-05-16 19:31:22 -03:00
// @Param: WSP_AVG
// @DisplayName: Multi-pulse averages
// @Description: Sets the number of pulses to be used in multi-pulse averaging mode. In this mode, a sequence of rapid fire ranges are taken and then averaged to improve the accuracy of the measurement
2018-05-24 23:00:02 -03:00
// @Range: 0 255
2018-05-16 19:31:22 -03:00
// @User: Advanced
2018-05-30 00:47:08 -03:00
AP_GROUPINFO ( " WSP_AVG " , 4 , AP_RangeFinder_Wasp , avg , 2 ) ,
2018-05-16 19:31:22 -03:00
// @Param: WSP_THR
// @DisplayName: Sensitivity threshold
// @Description: Sets the system sensitivity. Larger values of THR represent higher sensitivity. The system may limit the maximum value of THR to prevent excessive false alarm rates based on settings made at the factory. Set to -1 for automatic threshold adjustments
2018-05-24 23:00:02 -03:00
// @Range: -1 255
2018-05-16 19:31:22 -03:00
// @User: Advanced
AP_GROUPINFO ( " WSP_THR " , 5 , AP_RangeFinder_Wasp , thr , - 1 ) ,
2018-05-30 00:47:08 -03:00
// @Param: WSP_BAUD
// @DisplayName: Baud rate
// @Description: Desired baud rate
// @Values: 0:Low Speed,1:High Speed
// @User: Advanced
AP_GROUPINFO ( " WSP_BAUD " , 6 , AP_RangeFinder_Wasp , baud , 0 ) ,
2018-05-16 19:31:22 -03:00
AP_GROUPEND
} ;
AP_RangeFinder_Wasp : : AP_RangeFinder_Wasp ( RangeFinder : : RangeFinder_State & _state ,
2018-07-04 11:22:17 -03:00
AP_RangeFinder_Params & _params ,
2018-05-16 19:31:22 -03:00
uint8_t serial_instance ) :
2019-11-01 07:23:16 -03:00
AP_RangeFinder_Backend_Serial ( _state , _params , serial_instance )
{
2018-05-16 19:31:22 -03:00
AP_Param : : setup_object_defaults ( this , var_info ) ;
2019-11-01 07:23:16 -03:00
state . var_info = var_info ;
2018-05-16 19:31:22 -03:00
}
// read - return last value measured by sensor
bool AP_RangeFinder_Wasp : : get_reading ( uint16_t & reading_cm ) {
if ( uart = = nullptr ) {
return false ;
}
// read any available lines from the lidar
float sum = 0 ;
uint16_t count = 0 ;
int16_t nbytes = uart - > available ( ) ;
while ( nbytes - - > 0 ) {
char c = uart - > read ( ) ;
if ( c = = ' \n ' ) {
linebuf [ linebuf_len ] = 0 ;
linebuf_len = 0 ;
AP_RangeFinder: support last_reading_ms
Benewake, LeddarOne, LightWareSerial, MAVLink, MaxsonarI2CXL, MaxsonarSerialLV, NMEA, PX4_PWM, uLanding and Wasp already stored the last read time so for these drivers, this change just moves that storage to the state structure
analog, BBB_PRU, Bebop, LightWareI2C, PulsedLightLRF, TeraRangerI2C, VL53L0X did not store the last read time so this was added
2018-08-27 04:02:51 -03:00
state . last_reading_ms = AP_HAL : : millis ( ) ;
2018-05-16 19:31:22 -03:00
if ( isalpha ( linebuf [ 0 ] ) ) {
parse_response ( ) ;
} else {
2019-10-27 08:47:51 -03:00
float read_value = strtof ( linebuf , NULL ) ;
2018-05-16 19:31:22 -03:00
if ( read_value > 0 ) {
sum + = read_value ;
count + + ;
}
}
} else if ( isalnum ( c ) | | c = = ' . ' | | c = = ' - ' ) {
linebuf [ linebuf_len + + ] = c ;
}
// discard excessively long buffers
if ( linebuf_len = = sizeof ( linebuf ) ) {
linebuf_len = 0 ;
}
}
if ( configuration_state = = WASP_CFG_RATE & & uart - > tx_pending ( ) = = 0 ) {
configuration_state = WASP_CFG_ENCODING ;
}
if ( count = = 0 ) {
return false ;
}
reading_cm = 100 * sum / count ;
2019-11-01 02:10:52 -03:00
set_status ( RangeFinder : : Status : : Good ) ;
2018-05-16 19:31:22 -03:00
return true ;
}
# define COMMAND_BUFFER_LEN 15
void AP_RangeFinder_Wasp : : update ( void ) {
if ( ! get_reading ( state . distance_cm ) ) {
2019-11-01 02:10:52 -03:00
set_status ( RangeFinder : : Status : : NoData ) ;
2018-05-16 19:31:22 -03:00
}
AP_RangeFinder: support last_reading_ms
Benewake, LeddarOne, LightWareSerial, MAVLink, MaxsonarI2CXL, MaxsonarSerialLV, NMEA, PX4_PWM, uLanding and Wasp already stored the last read time so for these drivers, this change just moves that storage to the state structure
analog, BBB_PRU, Bebop, LightWareI2C, PulsedLightLRF, TeraRangerI2C, VL53L0X did not store the last read time so this was added
2018-08-27 04:02:51 -03:00
if ( AP_HAL : : millis ( ) - state . last_reading_ms > 500 ) {
2018-05-16 19:31:22 -03:00
// attempt to reconfigure on the assumption this was a bad baud setting
configuration_state = WASP_CFG_RATE ;
}
char command [ COMMAND_BUFFER_LEN ] = { } ;
switch ( configuration_state ) {
case WASP_CFG_RATE :
hal . util - > snprintf ( command , COMMAND_BUFFER_LEN , " >BAUD %s \n " , baud > 0 ? " HIGH " : " LOW " ) ;
break ;
case WASP_CFG_ENCODING :
uart - > end ( ) ;
2018-05-30 00:47:08 -03:00
uart - > begin ( baud > 0 ? 921600 : 115200 ) ;
2018-05-16 19:31:22 -03:00
hal . util - > snprintf ( command , COMMAND_BUFFER_LEN , " >LBE LITTLE \n " ) ;
break ;
case WASP_CFG_PROTOCOL :
hal . util - > snprintf ( command , COMMAND_BUFFER_LEN , " >FMT ASCII \n " ) ;
break ;
case WASP_CFG_FRQ :
hal . util - > snprintf ( command , COMMAND_BUFFER_LEN , " >FRQ %d \n " , constrain_int16 ( frq , 20 , baud > 0 ? 10000 : 1440 ) ) ;
break ;
case WASP_CFG_GO :
hal . util - > snprintf ( command , COMMAND_BUFFER_LEN , " >GO \n " ) ;
break ;
case WASP_CFG_AUT :
hal . util - > snprintf ( command , COMMAND_BUFFER_LEN , " >AUT %d \n " , thr > = 0 ? 0 : 1 ) ;
break ;
case WASP_CFG_THR :
if ( thr > = 0 ) {
hal . util - > snprintf ( command , COMMAND_BUFFER_LEN , " >THR %d \n " , constrain_int16 ( thr , 0 , 255 ) ) ;
} else {
configuration_state = WASP_CFG_MAVG ;
}
break ;
case WASP_CFG_MAVG :
hal . util - > snprintf ( command , COMMAND_BUFFER_LEN , " >MAVG %d \n " , constrain_int16 ( mavg , 0 , 255 ) ) ;
break ;
case WASP_CFG_MEDF :
hal . util - > snprintf ( command , COMMAND_BUFFER_LEN , " >MEDF %d \n " , constrain_int16 ( medf , 0 , 255 ) ) ;
break ;
case WASP_CFG_AVG :
hal . util - > snprintf ( command , COMMAND_BUFFER_LEN , " >AVG %d \n " , constrain_int16 ( avg , 0 , 255 ) ) ;
break ;
case WASP_CFG_AUV :
hal . util - > snprintf ( command , COMMAND_BUFFER_LEN , " >AUV 1 \n " ) ;
break ;
case WASP_CFG_DONE :
return ;
}
if ( command [ 0 ] ! = 0 ) {
uart - > write ( ( uint8_t * ) command , strlen ( command ) ) ;
}
}
void AP_RangeFinder_Wasp : : parse_response ( void ) {
switch ( configuration_state ) {
case WASP_CFG_RATE :
configuration_state = WASP_CFG_ENCODING ;
break ;
case WASP_CFG_ENCODING :
if ( strncmp ( linebuf , " LBE " , 3 ) = = 0 ) {
configuration_state = WASP_CFG_PROTOCOL ;
}
break ;
case WASP_CFG_PROTOCOL :
if ( strncmp ( linebuf , " FMT " , 3 ) = = 0 ) {
configuration_state = WASP_CFG_FRQ ;
}
break ;
case WASP_CFG_FRQ :
if ( strncmp ( linebuf , " FRQ " , 3 ) = = 0 ) {
configuration_state = WASP_CFG_GO ;
}
break ;
case WASP_CFG_GO :
if ( strncmp ( linebuf , " GO " , 2 ) = = 0 ) {
configuration_state = WASP_CFG_AUT ;
}
break ;
case WASP_CFG_AUT :
if ( strncmp ( linebuf , " AUT " , 3 ) = = 0 ) {
configuration_state = WASP_CFG_THR ;
}
break ;
case WASP_CFG_THR :
if ( strncmp ( linebuf , " THR " , 3 ) = = 0 ) {
configuration_state = WASP_CFG_MAVG ;
}
break ;
case WASP_CFG_MAVG :
if ( strncmp ( linebuf , " MAVG " , 4 ) = = 0 ) {
configuration_state = WASP_CFG_MEDF ;
}
break ;
case WASP_CFG_MEDF :
if ( strncmp ( linebuf , " MEDF " , 4 ) = = 0 ) {
configuration_state = WASP_CFG_AVG ;
}
break ;
case WASP_CFG_AVG :
if ( strncmp ( linebuf , " AVG " , 3 ) = = 0 ) {
configuration_state = WASP_CFG_AUV ;
}
break ;
case WASP_CFG_AUV :
if ( strncmp ( linebuf , " AUV " , 3 ) = = 0 ) {
configuration_state = WASP_CFG_DONE ;
}
break ;
case WASP_CFG_DONE :
return ;
}
}