00001
00002
00003
00004
00005
00006
00007
00008
00009
00022 #include <fstream>
00023
00024 #include <ros/ros.h>
00025 #include <angles/angles.h>
00026
00027 #include <velodyne/data_scans.h>
00028
00029 namespace Velodyne
00030 {
00032
00033
00034
00036
00037 DataScans::DataScans(std::string ofile, std::string anglesFile):
00038 Data(ofile, anglesFile)
00039 {
00040
00041
00042 scans_.reserve(SCANS_PER_REV);
00043 }
00044
00046 void DataScans::packet2scans(const raw_packet_t *raw, laserscan_t *scans)
00047 {
00048 int index = 0;
00049 uint16_t revolution = raw->revolution;
00050
00051 for (int i = 0; i < BLOCKS_PER_PACKET; i++)
00052 {
00053 int bank_origin = 32;
00054 correction_angles *corrections = upper_;
00055 if (raw->blocks[i].header == LOWER_BANK)
00056 {
00057 bank_origin = 0;
00058 corrections = lower_;
00059 }
00060
00061 float rotation = angles::from_degrees(raw->blocks[i].rotation
00062 * ROTATION_RESOLUTION);
00063
00064 for (int j = 0, k = 0; j < SCANS_PER_BLOCK; j++, k += RAW_SCAN_SIZE)
00065 {
00066 scans[index].laser_number = j + bank_origin;
00067
00068
00069
00070
00071
00072 scans[index].heading =
00073 angles::normalize_angle(-(rotation - corrections[j].rotational));
00074 scans[index].pitch = corrections[j].vertical;
00075
00076 union two_bytes tmp;
00077 tmp.bytes[0] = raw->blocks[i].data[k];
00078 tmp.bytes[1] = raw->blocks[i].data[k+1];
00079
00080
00081 scans[index].range = tmp.uint * DISTANCE_RESOLUTION;
00082 scans[index].range =
00083 (corrections[j].offset1 * scans[index].range * scans[index].range
00084 + corrections[j].offset2 * scans[index].range
00085 + corrections[j].offset3);
00086
00087 scans[index].intensity = raw->blocks[i].data[k+2];
00088 scans[index].revolution = revolution;
00089
00090 ++index;
00091 }
00092 }
00093
00094 ROS_ASSERT(index == SCANS_PER_PACKET);
00095 }
00096
00098 void DataScans::processPacket(const velodyne_msgs::VelodynePacket *pkt,
00099 const std::string &frame_id)
00100 {
00101
00102 scans_.resize(SCANS_PER_PACKET);
00103 packet2scans((raw_packet_t *) &pkt->data[0], &scans_[0]);
00104
00105 if (!ros::ok())
00106 return;
00107
00108
00109 if (cb_)
00110 cb_(scans_, pkt->stamp, frame_id);
00111 }
00112
00122 int DataScans::print(void)
00123 {
00124 if (uninitialized_)
00125 return EBADF;
00126
00127 if (ofp_ == NULL)
00128 return 0;
00129
00130 for (unsigned i = 0; i < scans_.size(); i++)
00131 {
00132 int rc = fprintf(ofp_, "%+08.6f %+08.6f %+011.6f %03u %02u %05u\n",
00133 scans_[i].heading,
00134 scans_[i].pitch,
00135 scans_[i].range,
00136 scans_[i].intensity,
00137 scans_[i].laser_number,
00138 scans_[i].revolution);
00139 if (rc < 0)
00140 return errno;
00141
00142
00143
00144 if (!ros::ok())
00145 return EINTR;
00146 }
00147
00148 return 0;
00149 }
00150
00151 }