sra-romi

Dependencies:   BufferedSerial Matrix

Committer:
joaopsousa99
Date:
Tue May 11 18:10:22 2021 +0000
Revision:
4:1defb279922a
Parent:
2:e27733eaa594
as.djvblaskdvj

Who changed what in which revision?

UserRevisionLine numberNew contents of line
LuisRA 0:2b691d200d6f 1 /*
LuisRA 0:2b691d200d6f 2 * RoboPeak RPLIDAR Driver for Arduino
LuisRA 0:2b691d200d6f 3 * RoboPeak.com
LuisRA 0:2b691d200d6f 4 *
LuisRA 0:2b691d200d6f 5 * Copyright (c) 2014, RoboPeak
LuisRA 0:2b691d200d6f 6 * All rights reserved.
LuisRA 0:2b691d200d6f 7 *
LuisRA 0:2b691d200d6f 8 * Redistribution and use in source and binary forms, with or without modification,
LuisRA 0:2b691d200d6f 9 * are permitted provided that the following conditions are met:
LuisRA 0:2b691d200d6f 10 *
LuisRA 0:2b691d200d6f 11 * 1. Redistributions of source code must retain the above copyright notice,
LuisRA 0:2b691d200d6f 12 * this list of conditions and the following disclaimer.
LuisRA 0:2b691d200d6f 13 *
LuisRA 0:2b691d200d6f 14 * 2. Redistributions in binary form must reproduce the above copyright notice,
LuisRA 0:2b691d200d6f 15 * this list of conditions and the following disclaimer in the documentation
LuisRA 0:2b691d200d6f 16 * and/or other materials provided with the distribution.
LuisRA 0:2b691d200d6f 17 *
LuisRA 0:2b691d200d6f 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
LuisRA 0:2b691d200d6f 19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
LuisRA 0:2b691d200d6f 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
LuisRA 0:2b691d200d6f 21 * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
LuisRA 0:2b691d200d6f 22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
LuisRA 0:2b691d200d6f 23 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
LuisRA 0:2b691d200d6f 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
LuisRA 0:2b691d200d6f 25 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
LuisRA 0:2b691d200d6f 26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
LuisRA 0:2b691d200d6f 27 *
LuisRA 0:2b691d200d6f 28 */
LuisRA 0:2b691d200d6f 29
LuisRA 0:2b691d200d6f 30 #pragma once
LuisRA 0:2b691d200d6f 31 #include "mbed.h"
LuisRA 0:2b691d200d6f 32 #include "inc/rptypes.h"
LuisRA 0:2b691d200d6f 33 #include "inc/rplidar_cmd.h"
LuisRA 0:2b691d200d6f 34 #include <BufferedSerial.h>
LuisRA 0:2b691d200d6f 35 //#include "../BufferedSerial/BufferedSerial.h"
LuisRA 0:2b691d200d6f 36 struct RPLidarMeasurement
LuisRA 0:2b691d200d6f 37 {
LuisRA 0:2b691d200d6f 38 float distance;
LuisRA 0:2b691d200d6f 39 float angle;
LuisRA 0:2b691d200d6f 40 uint8_t quality;
LuisRA 0:2b691d200d6f 41 bool startBit;
LuisRA 0:2b691d200d6f 42 };
LuisRA 0:2b691d200d6f 43
LuisRA 0:2b691d200d6f 44 class RPLidar
LuisRA 0:2b691d200d6f 45 {
LuisRA 0:2b691d200d6f 46 public:
LuisRA 0:2b691d200d6f 47 enum {
LuisRA 0:2b691d200d6f 48 RPLIDAR_SERIAL_BAUDRATE = 115200,
LuisRA 0:2b691d200d6f 49 RPLIDAR_DEFAULT_TIMEOUT = 500,
LuisRA 0:2b691d200d6f 50 };
LuisRA 0:2b691d200d6f 51
LuisRA 0:2b691d200d6f 52 void Thread_readSensor(void);
LuisRA 0:2b691d200d6f 53 int32_t pollSensorData(struct RPLidarMeasurement *_data);
LuisRA 0:2b691d200d6f 54 RPLidar(Serial *_pc);
LuisRA 0:2b691d200d6f 55 RPLidar();
LuisRA 0:2b691d200d6f 56 ~RPLidar();
LuisRA 0:2b691d200d6f 57
LuisRA 0:2b691d200d6f 58 // open the given serial interface and try to connect to the RPLIDAR
LuisRA 0:2b691d200d6f 59 //bool begin(BufferedSerial &serialobj);
LuisRA 0:2b691d200d6f 60 void begin(BufferedSerial &serialobj);
LuisRA 0:2b691d200d6f 61 // close the currently opened serial interface
LuisRA 0:2b691d200d6f 62 void end();
LuisRA 0:2b691d200d6f 63
LuisRA 0:2b691d200d6f 64 // check whether the serial interface is opened
LuisRA 0:2b691d200d6f 65 // bool isOpen();
LuisRA 0:2b691d200d6f 66
LuisRA 0:2b691d200d6f 67 // ask the RPLIDAR for its health info
LuisRA 0:2b691d200d6f 68 u_result getHealth(rplidar_response_device_health_t & healthinfo, _u32 timeout = RPLIDAR_DEFAULT_TIMEOUT);
LuisRA 0:2b691d200d6f 69
LuisRA 0:2b691d200d6f 70 // ask the RPLIDAR for its device info like the serial number
LuisRA 0:2b691d200d6f 71 u_result getDeviceInfo(rplidar_response_device_info_t & info, _u32 timeout = RPLIDAR_DEFAULT_TIMEOUT);
LuisRA 0:2b691d200d6f 72
LuisRA 0:2b691d200d6f 73 // stop the measurement operation
LuisRA 0:2b691d200d6f 74 u_result stop();
LuisRA 0:2b691d200d6f 75
LuisRA 0:2b691d200d6f 76 // start the measurement operation
LuisRA 0:2b691d200d6f 77 u_result startThreadScan();
LuisRA 0:2b691d200d6f 78 u_result startScan(bool force = true, _u32 timeout = RPLIDAR_DEFAULT_TIMEOUT*2);
LuisRA 0:2b691d200d6f 79
LuisRA 0:2b691d200d6f 80
LuisRA 0:2b691d200d6f 81 // wait for one sample point to arrive
LuisRA 0:2b691d200d6f 82 u_result waitPoint(_u32 timeout = RPLIDAR_DEFAULT_TIMEOUT);
LuisRA 0:2b691d200d6f 83
LuisRA 0:2b691d200d6f 84 u_result _sendCommand(_u8 cmd, const void * payload, size_t payloadsize);
LuisRA 0:2b691d200d6f 85 // retrieve currently received sample point
LuisRA 0:2b691d200d6f 86 int Data[360];
LuisRA 0:2b691d200d6f 87 int ang,dis;
LuisRA 0:2b691d200d6f 88 int angMin,angMax;
LuisRA 0:2b691d200d6f 89 void setAngle(int min,int max);
LuisRA 0:2b691d200d6f 90 void get_lidar();
LuisRA 0:2b691d200d6f 91 const RPLidarMeasurement & getCurrentPoint()
LuisRA 0:2b691d200d6f 92 {
LuisRA 0:2b691d200d6f 93 return _currentMeasurement;
LuisRA 0:2b691d200d6f 94 }
LuisRA 0:2b691d200d6f 95
LuisRA 0:2b691d200d6f 96 private:
LuisRA 0:2b691d200d6f 97 _u8 recvPos_g;
LuisRA 0:2b691d200d6f 98 rplidar_response_measurement_node_t node_g;
LuisRA 0:2b691d200d6f 99
LuisRA 0:2b691d200d6f 100 //Thread *thread_p;
LuisRA 0:2b691d200d6f 101 Thread thread;
fabiofaria 2:e27733eaa594 102 //Mail<struct RPLidarMeasurement, 100> mail_box; //
fabiofaria 2:e27733eaa594 103 Mutex mutex_measurements;
LuisRA 0:2b691d200d6f 104 bool useThread;
LuisRA 0:2b691d200d6f 105
fabiofaria 2:e27733eaa594 106 struct RPLidarMeasurement currentMeasurement_fromThread;
fabiofaria 2:e27733eaa594 107 bool newMeasurement;
fabiofaria 2:e27733eaa594 108
LuisRA 0:2b691d200d6f 109 u_result pollPoint();
LuisRA 0:2b691d200d6f 110 //Serial *pc;
LuisRA 0:2b691d200d6f 111 protected:
LuisRA 0:2b691d200d6f 112 // u_result _sendCommand(_u8 cmd, const void * payload, size_t payloadsize);
LuisRA 0:2b691d200d6f 113 u_result _waitResponseHeader(rplidar_ans_header_t * header, _u32 timeout);
LuisRA 0:2b691d200d6f 114
LuisRA 0:2b691d200d6f 115 protected:
LuisRA 0:2b691d200d6f 116 BufferedSerial * _bined_serialdev;
LuisRA 0:2b691d200d6f 117 RPLidarMeasurement _currentMeasurement;
LuisRA 0:2b691d200d6f 118 };