Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arduino - stepper - Lidar #14

Open
beginner2222 opened this issue Jun 3, 2019 · 0 comments
Open

Arduino - stepper - Lidar #14

beginner2222 opened this issue Jun 3, 2019 · 0 comments

Comments

@beginner2222
Copy link

Hello,

I'm using Arduino UNO with RPLIDAR A1 and Stepper motor. I'm trying to run both together and get the output in this way:
Step no:
Angle:
Distance:
Step no:
Angle:
Distance:
and so on.

When I run the code, stepper runs for some time and then LIDAR comes into motion along with stepper after some rotations finished by stepper motor.
On the serial monitor also I'm not seeing in the format that I want to see.

How do I run both simultaneously and get the LIDAR's angle and distance output for each step of the stepper motor? I need help with running both together.

I've researched some resources online and I've got to know that delay function should not be used in stepper mode code. micros() function is used in my code.

How do I approach this issue such that I can make both LIDAR and my motor run at once and fetch data together? Please help me with this situation.

RPLIDAR A1 code is from https://github.com/robopeak/rplidar_arduino

I've edited the distance_to_color.ino file from the examples section in the above URL according to my requirement. Let me know how I can solve this problem.

`
/*
RoboPeak RPLIDAR Arduino Example
This example shows how to control an RGB led based on the scan data of the RPLIDAR

The RGB led will change its hue based on the direction of the closet object RPLIDAR has been detected.
Also, the light intensity changes according to the object distance.

USAGE:

  1. Download this sketch code to your Arduino board
  2. Connect the RPLIDAR's serial port (RX/TX/GND) to your Arduino board (Pin 0 and Pin1)
  3. Connect the RPLIDAR's motor ctrl pin to the Arduino board pin 3
  4. Connect an RGB LED to your Arduino board, with the Red led to pin 9, Blue led to pin 10, Green led to pin 11
  5. Connect the required power supplies.
  6. RPLIDAR will start rotating when the skecth code has successfully detected it.
  7. Remove objects within the 0.5 meters' radius circule range of the RPLIDAR
  8. Place some object inside the 0.5 meters' range, check what will happen to the RGB led :)
    */

/*
Copyright (c) 2014, RoboPeak
All rights reserved.
RoboPeak.com

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

// This sketch code is based on the RPLIDAR driver library provided by RoboPeak
#include <RPLidar.h>

// You need to create an driver instance
RPLidar lidar;
// define pin used
const int stepPin = 9;
const int dirPin = 8;

long previoustime = 0;
long interval = 1000;

float distance, angle;
/////////////////////////////////////////////////////////////////////////////

#define RPLIDAR_MOTOR 3 // The PWM pin for control the speed of RPLIDAR's motor.
// This pin should connected with the RPLIDAR's MOTOCTRL signal
//////////////////////////////////////////////////////////////////////////////

void setup() {
// bind the RPLIDAR driver to the arduino hardware serial
lidar.begin(Serial);

// set pin mode for LIDAR Motor
pinMode(RPLIDAR_MOTOR, OUTPUT);

// set the two pins for Stepper motor as outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);

}

// digitalWrite(dirPin,HIGH); //Enables the motor to move in a cw direction
// // for one full rotation required 960 pulses
// for(int x = 0; x < 960; x++){
// digitalWrite(stepPin,HIGH);
// delayMicroseconds(1000);
// digitalWrite(stepPin,LOW);
// delayMicroseconds(1000);
// }
//
// digitalWrite(dirPin, LOW); //Enables the motor to move in the opposite direction
// for(int x = 0; x < 960; x++){
// digitalWrite(stepPin,HIGH);
// delayMicroseconds(1000);
// digitalWrite(stepPin,LOW);
// delayMicroseconds(1000);
// }

void loop() {
digitalWrite(dirPin, HIGH); //Enables the motor to move in a cw direction
// for one full rotation required 960 pulses
for (int x = 0; x < 960; x++) {

unsigned long currenttime = micros();
if (currenttime - previoustime > interval)
{
  digitalWrite(stepPin, LOW);
  digitalWrite(stepPin, HIGH);
  distance = lidar.getCurrentPoint().distance;
  angle = lidar.getCurrentPoint().angle;
  Serial.println("1Angle is:" + String(angle));
  Serial.println("1Distance is:" + String(distance));

  previoustime = currenttime;
}
Serial.println("Step:" + String(x));

}

digitalWrite(dirPin, LOW); //Enables the motor to move in the opposite direction
// for one full rotation required 960 pulses
for (int x = 0; x < 960; x++) {

unsigned long currenttime = micros();
if (currenttime - previoustime > interval)
{
  digitalWrite(stepPin, LOW);
  digitalWrite(stepPin, HIGH);
  distance = lidar.getCurrentPoint().distance;
  angle = lidar.getCurrentPoint().angle;
  Serial.println("2Angle is:" + String(angle));
  Serial.println("2Distance is:" + String(distance));
  previoustime = currenttime;
}
Serial.println("Step:" + String(x));

}

if (IS_OK(lidar.waitPoint())) {
//perform data processing here...

distance = lidar.getCurrentPoint().distance;
angle = lidar.getCurrentPoint().angle;
Serial.println("3Angle is:" + String(angle));
Serial.println("3Distance is:" + String(distance));

}
else {
analogWrite(RPLIDAR_MOTOR, 0); //stop the rplidar motor
// try to detect RPLIDAR...
rplidar_response_device_info_t info;
if (IS_OK(lidar.getDeviceInfo(info, 100))) {
//detected...
lidar.startScan();
analogWrite(RPLIDAR_MOTOR, 255);
delay(1000);
}
}
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant