Advertisement
Guest User

Untitled

a guest
May 5th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. /*
  2. * RoboPeak RPLIDAR Arduino Example
  3. * This example shows the easy and common way to fetch data from an RPLIDAR
  4. *
  5. * You may freely add your application code based on this template
  6. *
  7. * USAGE:
  8. * ---------------------------------
  9. * 1. Download this sketch code to your Arduino board
  10. * 2. Connect the RPLIDAR's serial port (RX/TX/GND) to your Arduino board (Pin 0 and Pin1)
  11. * 3. Connect the RPLIDAR's motor ctrl pin to the Arduino board pin 3
  12. */
  13.  
  14. /*
  15. * Copyright (c) 2014, RoboPeak
  16. * All rights reserved.
  17. * RoboPeak.com
  18. *
  19. * Redistribution and use in source and binary forms, with or without modification,
  20. * are permitted provided that the following conditions are met:
  21. *
  22. * 1. Redistributions of source code must retain the above copyright notice,
  23. * this list of conditions and the following disclaimer.
  24. *
  25. * 2. Redistributions in binary form must reproduce the above copyright notice,
  26. * this list of conditions and the following disclaimer in the documentation
  27. * and/or other materials provided with the distribution.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  30. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  31. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  32. * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  33. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  34. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  36. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  37. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40.  
  41. // This sketch code is based on the RPLIDAR driver library provided by RoboPeak
  42. #include <RPLidar.h>
  43.  
  44. // You need to create an driver instance
  45. RPLidar lidar;
  46.  
  47. #define RPLIDAR_MOTOR 5 // The PWM pin for control the speed of RPLIDAR's motor.
  48. // This pin should connected with the RPLIDAR's MOTOCTRL signal
  49.  
  50. #define BAUD_RATE 9600
  51. #define WIDTH 450
  52. #define LENGTH 350
  53. #define Y_OFFSET 120
  54. #define RECORD 0
  55. #define PAUSE 1
  56. #define RECORD_PAUSE_PIN 2
  57. #define CLEAR_PIN 3
  58. #define STOP_PIN 4
  59.  
  60.  
  61. void setup() {
  62. // bind the RPLIDAR driver to the arduino hardware serial
  63. Serial.begin(BAUD_RATE);
  64. Serial2.begin(BAUD_RATE);
  65. lidar.begin(Serial1);
  66.  
  67. // set pin modes
  68. pinMode(RPLIDAR_MOTOR, OUTPUT);
  69. pinMode(RECORD_PAUSE_PIN, INPUT);
  70. pinMode(CLEAR_PIN, INPUT);
  71. pinMode(STOP_PIN, INPUT);
  72. attachInterrupt(digitalPinToInterrupt(CLEAR_PIN), clearBoard, RISING);
  73. }
  74.  
  75. float minDistance = 100000;
  76. float angleAtMinDist = 0;
  77. volatile int state = RECORD;
  78. volatile int boardClear = 0;
  79. volatile int doneRecording = 0;
  80.  
  81. void loop() {
  82. if (IS_OK(lidar.waitPoint())) {
  83. float distance = lidar.getCurrentPoint().distance; //distance value in mm unit
  84. float angle = lidar.getCurrentPoint().angle; //anglue value in degree
  85.  
  86. // data processing here...
  87. if (lidar.getCurrentPoint().startBit) {
  88. float r = minDistance;
  89. float theta = (360 - angleAtMinDist) * PI / 180;
  90. // calculate cartesian coordinate here
  91. float x = r * cos(theta) + WIDTH/2;
  92. float y = r * sin(theta) * -1 - Y_OFFSET;
  93. if (digitalRead(STOP_PIN) == HIGH) {
  94. Serial2.print("D");
  95. Serial.println("done recording");
  96. return;
  97. }
  98.  
  99. if (digitalRead(RECORD_PAUSE_PIN) == RECORD && 0 < x && x < WIDTH && 0 < y && y < LENGTH) {
  100. Serial.print("(");
  101. Serial.print(x);
  102. Serial.print(", ");
  103. Serial.print(y);
  104. Serial.print(")\n");
  105. // serial to edison
  106. Serial2.print("X");
  107. Serial2.println(x);
  108. Serial2.print("Y");
  109. Serial2.println(y);
  110. } else if (boardClear) {
  111. // board needs to be cleared
  112. Serial2.print("C");
  113. boardClear = 0;
  114. } else {
  115. // no point in the region OR paused recording
  116. Serial2.print("S");
  117. }
  118. // reset variables
  119. minDistance = 100000;
  120. angleAtMinDist = 0;
  121. } else {
  122. if (distance > 0 && distance < minDistance && angle < 180) {
  123. minDistance = distance;
  124. angleAtMinDist = angle;
  125. }
  126. }
  127.  
  128. } else {
  129. analogWrite(RPLIDAR_MOTOR, 0); //stop the rplidar motor
  130.  
  131. // try to detect RPLIDAR...
  132. rplidar_response_device_info_t info;
  133. if (IS_OK(lidar.getDeviceInfo(info, 100))) {
  134. // detected...
  135. lidar.startScan();
  136.  
  137. // start motor rotating at max allowed speed
  138. analogWrite(RPLIDAR_MOTOR, 255);
  139. delay(1000);
  140. }
  141. }
  142. }
  143.  
  144. void clearBoard() {
  145. Serial.println("clear pressed!");
  146. boardClear = 1;
  147. }
  148.  
  149. void stopRecording() {
  150. Serial.println("stopped recording");
  151. doneRecording = 1;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement