lidar code for ROC318

Dependencies:   BufferedSerial mbed

Committer:
BenRJG
Date:
Fri Oct 19 17:42:05 2018 +0000
Revision:
0:0791d48ee421
Imported lidar code

Who changed what in which revision?

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