/* * Copyright (C) 2012 Open Source Robotics Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ #include #include #include "gazebo/test/ServerFixture.hh" using namespace gazebo; class RaySensor_TEST : public ServerFixture { }; static std::string raySensorString = "" " " " 1" " 1" " 20.000000" " " " " " " " 640" " 1.000000" " -2.2689" " 2.2689" " " " " " " " 0.08" " 10.0" " 0.01" " " " " " " ""; static std::string raySensorScanResString = "" " " " 1" " 1" " 20.000000" " " " " " " " 120" " 2" " -2.2689" " 2.2689" " " " " " 2" " 3" " -2.2689" " 2.2689" " " " " " " " 0.08" " 10.0" " 0.01" " " " " " " ""; ///////////////////////////////////////////////// /// \brief Test Creation of a Ray sensor TEST_F(RaySensor_TEST, CreateLaser) { Load("worlds/empty.world"); sensors::SensorManager *mgr = sensors::SensorManager::Instance(); sdf::ElementPtr sdf(new sdf::Element); sdf::initFile("sensor.sdf", sdf); sdf::readString(raySensorString, sdf); // Create the Ray sensor std::string sensorName = mgr->CreateSensor(sdf, "default", "ground_plane::link", 0); // Make sure the returned sensor name is correct EXPECT_EQ(sensorName, std::string("default::ground_plane::link::laser")); // Update the sensor manager so that it can process new sensors. mgr->Update(); // Get a pointer to the Ray sensor sensors::RaySensorPtr sensor = std::dynamic_pointer_cast (mgr->GetSensor(sensorName)); // Make sure the above dynamic cast worked. EXPECT_TRUE(sensor != NULL); double angleRes = (sensor->AngleMax() - sensor->AngleMin()).Radian() / sensor->RayCount(); EXPECT_EQ(sensor->AngleMin(), ignition::math::Angle(-2.2689)); EXPECT_EQ(sensor->AngleMax(), ignition::math::Angle(2.2689)); EXPECT_NEAR(sensor->RangeMin(), 0.08, 1e-6); EXPECT_NEAR(sensor->RangeMax(), 10.0, 1e-6); EXPECT_NEAR(sensor->AngleResolution(), angleRes, 1e-3); EXPECT_NEAR(sensor->RangeResolution(), 0.01, 1e-3); EXPECT_EQ(sensor->RayCount(), 640); EXPECT_EQ(sensor->RangeCount(), 640); EXPECT_EQ(sensor->VerticalRayCount(), 1); EXPECT_EQ(sensor->VerticalRangeCount(), 1); EXPECT_EQ(sensor->VerticalAngleMin(), 0); EXPECT_EQ(sensor->VerticalAngleMax(), 0); EXPECT_TRUE(sensor->IsActive()); // Update the sensor sensor->Update(true); // Get all the range values std::vector ranges; sensor->Ranges(ranges); EXPECT_EQ(ranges.size(), static_cast(640)); // Check that all the range values for (unsigned int i = 0; i < ranges.size(); ++i) { EXPECT_DOUBLE_EQ(ranges[i], IGN_DBL_INF); EXPECT_DOUBLE_EQ(sensor->Range(i), ranges[i]); EXPECT_NEAR(sensor->Retro(i), 0, 1e-6); EXPECT_EQ(sensor->Fiducial(i), -1); } } ///////////////////////////////////////////////// /// \brief Test Creation of a Ray sensor with a scan resolution higher than 1 TEST_F(RaySensor_TEST, LaserScanResolution) { Load("worlds/empty.world"); sensors::SensorManager *mgr = sensors::SensorManager::Instance(); sdf::ElementPtr sdf(new sdf::Element); sdf::initFile("sensor.sdf", sdf); sdf::readString(raySensorScanResString, sdf); // Create the Ray sensor std::string sensorName = mgr->CreateSensor(sdf, "default", "ground_plane::link", 0); // Make sure the returned sensor name is correct EXPECT_EQ(sensorName, std::string("default::ground_plane::link::laser")); // Update the sensor manager so that it can process new sensors. mgr->Update(); // Get a pointer to the Ray sensor sensors::RaySensorPtr sensor = std::dynamic_pointer_cast (mgr->GetSensor(sensorName)); // Make sure the above dynamic cast worked. EXPECT_TRUE(sensor != NULL); // range count = ray count * resolution EXPECT_EQ(sensor->RayCount(), 120); EXPECT_EQ(sensor->RangeCount(), 240); EXPECT_EQ(sensor->VerticalRayCount(), 2); EXPECT_EQ(sensor->VerticalRangeCount(), 6); EXPECT_TRUE(sensor->IsActive()); // Update the sensor sensor->Update(true); // Get all the range values std::vector ranges; sensor->Ranges(ranges); EXPECT_EQ(ranges.size(), static_cast(240 * 6)); // Check that all the range values for (unsigned int i = 0; i < ranges.size(); ++i) { EXPECT_DOUBLE_EQ(ranges[i], GZ_DBL_INF); EXPECT_DOUBLE_EQ(sensor->Range(i), ranges[i]); EXPECT_NEAR(sensor->Retro(i), 0, 1e-6); EXPECT_EQ(sensor->Fiducial(i), -1); } } ///////////////////////////////////////////////// int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }