Simple library to read XV 11 Lidar

Committer:
achmad_fathoni
Date:
Mon Oct 02 06:03:22 2017 +0000
Revision:
0:b5c7dc5f1fc8
Penambahan Library Kak Evan

Who changed what in which revision?

UserRevisionLine numberNew contents of line
achmad_fathoni 0:b5c7dc5f1fc8 1 #ifndef MBED_LIDAR_H
achmad_fathoni 0:b5c7dc5f1fc8 2 #define MBED_LIDAR_H
achmad_fathoni 0:b5c7dc5f1fc8 3
achmad_fathoni 0:b5c7dc5f1fc8 4 #include "mbed.h"
achmad_fathoni 0:b5c7dc5f1fc8 5
achmad_fathoni 0:b5c7dc5f1fc8 6 /** A Library used to read data from Low-cost NEATO LIDAR
achmad_fathoni 0:b5c7dc5f1fc8 7 *
achmad_fathoni 0:b5c7dc5f1fc8 8 * Example:
achmad_fathoni 0:b5c7dc5f1fc8 9 * @code
achmad_fathoni 0:b5c7dc5f1fc8 10 * #include "mbed.h"
achmad_fathoni 0:b5c7dc5f1fc8 11 * #include "LIDAR.h"
achmad_fathoni 0:b5c7dc5f1fc8 12 *
achmad_fathoni 0:b5c7dc5f1fc8 13 * int main() {
achmad_fathoni 0:b5c7dc5f1fc8 14 * // The tx pin is PC_10
achmad_fathoni 0:b5c7dc5f1fc8 15 * // The rx pin is PC_11
achmad_fathoni 0:b5c7dc5f1fc8 16 * // The PWM pin to control motor is PA_15
achmad_fathoni 0:b5c7dc5f1fc8 17 * LIDAR lidar (PC_10, PC_11, PA_15);
achmad_fathoni 0:b5c7dc5f1fc8 18 *
achmad_fathoni 0:b5c7dc5f1fc8 19 * // Activate serial to PC for debugging
achmad_fathoni 0:b5c7dc5f1fc8 20 * Serial pc(USBTX, USBRX);
achmad_fathoni 0:b5c7dc5f1fc8 21 *
achmad_fathoni 0:b5c7dc5f1fc8 22 * // Set duty cycle for motor PWM
achmad_fathoni 0:b5c7dc5f1fc8 23 * lidar.SetDuty(0.25);
achmad_fathoni 0:b5c7dc5f1fc8 24 *
achmad_fathoni 0:b5c7dc5f1fc8 25 * // Start data aquisition
achmad_fathoni 0:b5c7dc5f1fc8 26 * lidar.StartData();
achmad_fathoni 0:b5c7dc5f1fc8 27 *
achmad_fathoni 0:b5c7dc5f1fc8 28 * while (1) {
achmad_fathoni 0:b5c7dc5f1fc8 29 * // Aquire LIDAR data from angle 0, 45, 90, 135... 315
achmad_fathoni 0:b5c7dc5f1fc8 30 * // Then send it to serial PC with 9600 baud
achmad_fathoni 0:b5c7dc5f1fc8 31 * data = lidar.GetData(i);
achmad_fathoni 0:b5c7dc5f1fc8 32 * speed = lidar.GetSpeed();
achmad_fathoni 0:b5c7dc5f1fc8 33 * pc.printf("Spd=%.1f; D[%d]=%.1f\n", speed, i, data);
achmad_fathoni 0:b5c7dc5f1fc8 34 * i += 45;
achmad_fathoni 0:b5c7dc5f1fc8 35 * if(i >= 360) i = 0;
achmad_fathoni 0:b5c7dc5f1fc8 36 * wait(0.5);
achmad_fathoni 0:b5c7dc5f1fc8 37 * }
achmad_fathoni 0:b5c7dc5f1fc8 38 * }
achmad_fathoni 0:b5c7dc5f1fc8 39 * @endcode
achmad_fathoni 0:b5c7dc5f1fc8 40 */
achmad_fathoni 0:b5c7dc5f1fc8 41 class LIDAR {
achmad_fathoni 0:b5c7dc5f1fc8 42
achmad_fathoni 0:b5c7dc5f1fc8 43 public:
achmad_fathoni 0:b5c7dc5f1fc8 44 /** Create an LIDAR object connected to the specified serial port and
achmad_fathoni 0:b5c7dc5f1fc8 45 * PWM port for LIDAR motor control
achmad_fathoni 0:b5c7dc5f1fc8 46 *
achmad_fathoni 0:b5c7dc5f1fc8 47 * @param tx Transmit pin
achmad_fathoni 0:b5c7dc5f1fc8 48 * @param rx Receive Pin
achmad_fathoni 0:b5c7dc5f1fc8 49 * @param motor_pwm PWM pin for motor control
achmad_fathoni 0:b5c7dc5f1fc8 50 *
achmad_fathoni 0:b5c7dc5f1fc8 51 * @note
achmad_fathoni 0:b5c7dc5f1fc8 52 * The LIDAR will be initiated with 1kHz PWM with 0% duty-cycle
achmad_fathoni 0:b5c7dc5f1fc8 53 * and 115200 baud (standard baud for NEATO LIDAR)
achmad_fathoni 0:b5c7dc5f1fc8 54 */
achmad_fathoni 0:b5c7dc5f1fc8 55 LIDAR(PinName tx, PinName rx, PinName motor_pwm);
achmad_fathoni 0:b5c7dc5f1fc8 56
achmad_fathoni 0:b5c7dc5f1fc8 57 /** Start data aquisition after setting up other initial parameter
achmad_fathoni 0:b5c7dc5f1fc8 58 * such as motor duty cycle and period.
achmad_fathoni 0:b5c7dc5f1fc8 59 *
achmad_fathoni 0:b5c7dc5f1fc8 60 * @note
achmad_fathoni 0:b5c7dc5f1fc8 61 * This function will activate Rx interrupt each time data is received
achmad_fathoni 0:b5c7dc5f1fc8 62 */
achmad_fathoni 0:b5c7dc5f1fc8 63 void StartData(void);
achmad_fathoni 0:b5c7dc5f1fc8 64
achmad_fathoni 0:b5c7dc5f1fc8 65 /** Stop data aquisition
achmad_fathoni 0:b5c7dc5f1fc8 66 *
achmad_fathoni 0:b5c7dc5f1fc8 67 * @note
achmad_fathoni 0:b5c7dc5f1fc8 68 * This function will deactivate Rx Interrupt
achmad_fathoni 0:b5c7dc5f1fc8 69 */
achmad_fathoni 0:b5c7dc5f1fc8 70 void StopData(void);
achmad_fathoni 0:b5c7dc5f1fc8 71
achmad_fathoni 0:b5c7dc5f1fc8 72 /** Set the PWM duty cycle precentage for LIDAR motor
achmad_fathoni 0:b5c7dc5f1fc8 73 *
achmad_fathoni 0:b5c7dc5f1fc8 74 * @param duty The duty cycle ranged from 0.0(0%) to 1.0(100%)
achmad_fathoni 0:b5c7dc5f1fc8 75 *
achmad_fathoni 0:b5c7dc5f1fc8 76 */
achmad_fathoni 0:b5c7dc5f1fc8 77 void SetPWMDuty(float duty);
achmad_fathoni 0:b5c7dc5f1fc8 78
achmad_fathoni 0:b5c7dc5f1fc8 79 /** Set the PWM duty cycle for LIDAR motor in microseconds. The
achmad_fathoni 0:b5c7dc5f1fc8 80 * generated frequency will be f = 1000000/period
achmad_fathoni 0:b5c7dc5f1fc8 81 *
achmad_fathoni 0:b5c7dc5f1fc8 82 * @param microseconds The PWM period in microseconds
achmad_fathoni 0:b5c7dc5f1fc8 83 *
achmad_fathoni 0:b5c7dc5f1fc8 84 */
achmad_fathoni 0:b5c7dc5f1fc8 85 void SetPWMPeriodUs(int microseconds);
achmad_fathoni 0:b5c7dc5f1fc8 86
achmad_fathoni 0:b5c7dc5f1fc8 87 /** Obtain the distance data read by LIDAR in certain angle
achmad_fathoni 0:b5c7dc5f1fc8 88 *
achmad_fathoni 0:b5c7dc5f1fc8 89 * @param degree The angle where the data want to be read, it is integer ranged from 0-359
achmad_fathoni 0:b5c7dc5f1fc8 90 *
achmad_fathoni 0:b5c7dc5f1fc8 91 * @return Distance data in cm
achmad_fathoni 0:b5c7dc5f1fc8 92 */
achmad_fathoni 0:b5c7dc5f1fc8 93 float GetData(int degree);
achmad_fathoni 0:b5c7dc5f1fc8 94
achmad_fathoni 0:b5c7dc5f1fc8 95 /** Obtain the rotation speed of LIDAR motor
achmad_fathoni 0:b5c7dc5f1fc8 96 *
achmad_fathoni 0:b5c7dc5f1fc8 97 * @return Speed data in rpm
achmad_fathoni 0:b5c7dc5f1fc8 98 */
achmad_fathoni 0:b5c7dc5f1fc8 99 float GetSpeed(void);
achmad_fathoni 0:b5c7dc5f1fc8 100
achmad_fathoni 0:b5c7dc5f1fc8 101 private:
achmad_fathoni 0:b5c7dc5f1fc8 102 Serial _lidar;
achmad_fathoni 0:b5c7dc5f1fc8 103 PwmOut _motor;
achmad_fathoni 0:b5c7dc5f1fc8 104
achmad_fathoni 0:b5c7dc5f1fc8 105 short _data[360];
achmad_fathoni 0:b5c7dc5f1fc8 106 char* _data_ptr;
achmad_fathoni 0:b5c7dc5f1fc8 107 unsigned short _speed;
achmad_fathoni 0:b5c7dc5f1fc8 108 char* _speed_ptr;
achmad_fathoni 0:b5c7dc5f1fc8 109
achmad_fathoni 0:b5c7dc5f1fc8 110 char _fsm_state;
achmad_fathoni 0:b5c7dc5f1fc8 111 char _fsm_count;
achmad_fathoni 0:b5c7dc5f1fc8 112 unsigned short _fsm_angle;
achmad_fathoni 0:b5c7dc5f1fc8 113
achmad_fathoni 0:b5c7dc5f1fc8 114 void data_parser(void);
achmad_fathoni 0:b5c7dc5f1fc8 115 };
achmad_fathoni 0:b5c7dc5f1fc8 116
achmad_fathoni 0:b5c7dc5f1fc8 117 #endif