-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_obst.cpp
More file actions
126 lines (99 loc) · 3.1 KB
/
find_obst.cpp
File metadata and controls
126 lines (99 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// Created by anna on 2/5/17.
// Edited by Chong Swee 4/2/17
/*!
Created: Feb 5, 2017
Finds obstacles, converts their location to (x,y) coords, and stores as object
Identifies obstacles, find a clear path for the robot to go through
Publishes Twist (cmd_vel) of clearest path
Publish boolean (whether there is an obstacle within lesss than THRESHOLD_STOP)
*/
#include <string>
#include <iostream>
#include <vector>
#include "ros/ros.h"
#include "sensor_msgs/LaserScan.h"
#include "std_msgs/Bool.h"
#include "geometry_msgs/Twist.h"
#define THRESHOLD 0.3 // THRESHOLD FOR CLEAR
#define VELOCITY 22
#define ROBO_WIDTH 0.5 // in meters
sensor_msgs::LaserScan scan;
sensor_msgs::LaserScan filtered_scan;
ros::Publisher obstacle_flag;
ros::Publisher lidar_vel;
geometry_msgs::Twist twist;
std_msgs::Bool flag;
float smallest;
float angle_val;
void getLIDAR(const sensor_msgs::LaserScan lidar_scan)
;
void controlSpeed(const sensor_msgs::LaserScan lidar_scan)
{
float forward_distance = 100;
// set default speed
twist.linear.x = VELOCITY;
twist.angular.z = 0; // straight
int number_of_ranges = (int) lidar_scan.ranges.size();
scan = lidar_scan;
// Remove junk values from scan data (0.0 is out of range or too close to be accurate)
for(int i=0; i < sizeof(lidar_scan.ranges) / sizeof(lidar_scan.ranges[0]); i++)
{
if(lidar_scan.ranges[i] < 0.1 || lidar_scan.ranges[i] > 3.5)
{
scan.ranges[i] = 0.0;
}
}
// Calculate output array using some portion of scan -> set "forward distance" to the smallest value -> the closest object.
for (int i = number_of_ranges/3; i<2*number_of_ranges/3;i++)
{
if ((forward_distance > scan.ranges[i]) && (scan.ranges[i] >= 0.1)) {
forward_distance = scan.ranges[i];
smallest = i;
}
}
if (forward_distance < .3)
{
// Move backward
flag.data=1; //avoiding obstacles
twist.angular.z = 0; //go straight backwards
twist.linear.x = -12; //back up, slowly
}
else if (forward_distance < 0.7)
{
flag.data = 1;
angle_val = (smallest)/ (number_of_ranges);
if (angle_val < 0.5) {
//turn left
twist.angular.z = -30;
}
else if (angle_val >= 0.5){
//turn right
twist.angular.z = 30;
}
}
else {
// Move forward
flag.data=0; //not avoiding obstacles
twist.angular.z = 0; //go straight forward
twist.linear.x = 12; //drive forward
//ROS_INFO_STREAM(cmd_array);
}
obstacle_flag.publish(flag);
lidar_vel.publish(twist);
forward_distance = 100.0;
return;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "midbrain");
ros::NodeHandle n;
ros::Rate loop_rate(5);
// create topics to publish to
obstacle_flag = n.advertise<std_msgs::Bool>("obst/flag",1000);
lidar_vel =n.advertise<geometry_msgs::Twist>("obst/cmd_vel", 1000);
// subsribe to topics
ros::Subscriber sub_lidar = n.subscribe("scan",1000,controlSpeed);
ros::spin();
return 0;
}