RangeSensor.cpp
Go to the documentation of this file.
1 
8 
9 namespace rs
10 {
11 
12 double correctYawAngle(const double theta, const double increment)
13 {
14  double yaw = 0;
15  double angle = theta + increment;
16  if (angle > 180.0)
17  {
18  yaw = -180.0 - (180.0 - angle);
19  }
20  else if (angle < -180.0)
21  {
22  yaw = 180.0 - (-180.0 - angle);
23  }
24  else
25  {
26  yaw = angle;
27  }
28  return yaw;
29 }
30 
31 const int sgn(const int x) { return (x > 0) ? 1 : (x < 0) ? -1 : 0; }
32 
33 double degToRad(double angle) { return angle / 180.0 * CV_PI; }
34 
35 double radToDeg(double angle) { return angle / CV_PI * 180.0; }
36 }
37 
38 RangeSensor::RangeSensor(const SensorType& type) : type(type){}
39 
40 void RangeSensor::setConfig(pses_simulation::RangeSensorConfig& config)
41 {
42  this->config = config;
43 }
44 
45 void RangeSensor::setMap(const cv::Mat& map) { this->map = map; }
46 
47 void RangeSensor::setMapMetaData(const nav_msgs::MapMetaData& mapInfo)
48 {
49  this->mapInfo = mapInfo;
50 }
51 
52 const rs::rangeArray_ptr RangeSensor::getLaserScan(const cv::Point sensorPos,
53  const double theta) const
54 {
55  // yaw needs to be shifted by -90deg because robot people put their yaw=0 on
56  // the positive x-axis
57  // double yaw = rs::correctYawAngle(theta, -90);
58  // yaw = theta;
59  // get sensor configuration
60  double FOV, resolution, range;
61  int obstacleColor;
62  if (type == laser)
63  {
64  FOV = config.laser_field_of_view;
65  range = config.laser_max_sensor_distance + 0.5;
66  resolution = config.laser_angular_resolution;
67  obstacleColor = config.laser_obstacle_color;
68  }
69  else
70  {
71  FOV = config.us_field_of_view;
72  range = config.us_max_sensor_distance + 0.5;
73  resolution = config.us_angular_resolution;
74  obstacleColor = config.us_obstacle_color;
75  }
76 
77  // init loop for iteratiting over n-angles
78  int nAngles = FOV / resolution;
79  std::vector<float> rangeArray = std::vector<float>(nAngles);
80  double angle = rs::correctYawAngle(theta, -FOV / 2);
81  double maxDistance = range / mapInfo.resolution;
82 
83  for (int a = 0; a < nAngles; a++)
84  {
85  cv::Point P2;
86 
87  P2.x = (int)round(sensorPos.x +
88  maxDistance * std::cos((-angle) * CV_PI /
89  180.0)); // - vor angle entfernt
90  P2.y = (int)round(sensorPos.y +
91  maxDistance * std::sin((-angle) * CV_PI /
92  180.0)); // - vor angle entfernt
93 
94  cv::LineIterator it(map, sensorPos, P2, 8);
95  cv::Point currentPos = it.pos();
96  for (int i = 0; i < it.count; i++, ++it)
97  {
98  currentPos = it.pos();
99  if (map.at<uchar>(currentPos) == obstacleColor)
100  {
101  break;
102  }
103  }
104  double scannedRange = cv::norm(sensorPos - currentPos) * mapInfo.resolution;
105  rangeArray[a] = scannedRange;
106  angle = rs::correctYawAngle(angle, resolution);
107  }
108  return std::make_shared<std::vector<float>>(rangeArray);
109 }
110 const double RangeSensor::getUSScan(const cv::Point sensorPos,
111  const double theta) const
112 {
113  std::vector<float> rangeArray = *getLaserScan(sensorPos, theta);
114  double minOfRange = rangeArray[0];
115  for (auto current : rangeArray)
116  {
117  if (current < minOfRange)
118  {
119  minOfRange = current;
120  }
121  }
122  return minOfRange;
123 }
Header file for the rs::RangeSensor class.
pses_simulation::RangeSensorConfig config
Definition: RangeSensor.h:149
nav_msgs::MapMetaData mapInfo
Definition: RangeSensor.h:163
cv::Mat map
Definition: RangeSensor.h:156
void setConfig(pses_simulation::RangeSensorConfig &config)
Sets the RangeSensorConfig object of this sensor simulation.
Definition: RangeSensor.cpp:40
const rs::rangeArray_ptr getLaserScan(const cv::Point sensorPos, const double theta) const
Get a Laserscan at the current position/orientation.
Definition: RangeSensor.cpp:52
RangeSensor(const SensorType &type)
rs::RangeSensor constructor.
Definition: RangeSensor.cpp:38
This namespace groups typedefs and enums used in the rs::RangeSensor class and the rs::RangeSensor cl...
SensorType
Enumerator for supported sensor types.
Definition: RangeSensor.h:35
std::shared_ptr< std::vector< float > > rangeArray_ptr
shortcut for a pointer to a vector of floats representing range readings in meters.
Definition: RangeSensor.h:30
double radToDeg(double angle)
Conversion from radiants to degrees.
Definition: RangeSensor.cpp:35
void setMap(const cv::Mat &map)
Sets the occupancy grid to be used by this sensor simulation.
Definition: RangeSensor.cpp:45
const double getUSScan(const cv::Point sensorPos, const double theta) const
Get a ultra sonic range information at the current position/orientation.
const int sgn(const int x)
Get the sign of an interger.
Definition: RangeSensor.cpp:31
void setMapMetaData(const nav_msgs::MapMetaData &mapInfo)
Sets the meta data of the occupancy grid to be used by this sensor simulation.
Definition: RangeSensor.cpp:47
rs::SensorType type
Definition: RangeSensor.h:142
double degToRad(double angle)
Conversion from degrees to radiants.
Definition: RangeSensor.cpp:33
double correctYawAngle(const double theta, const double increment)
Calculate a correct euler angle after incrementing/decrementing.
Definition: RangeSensor.cpp:12


pses_simulation
Author(s): Sebastian Ehmes
autogenerated on Wed Dec 13 2017 19:37:54