I need to transform the radar sensor data from local coordinates to global coordinates. My approach is to first convert the radar laser data to local coordinates, then obtain the rotation quaternion through /tf, convert the quaternion to yaw angle, and obtain the rotated coordinates through the rotation matrix. Finally, based on the robot's position obtained from /odom, the global coordinates are calculated.
def getPos(self):
pos = self.odom_data.pose.pose.position
rotation = self.tf_data.transforms[0].transform.rotation
roll, pitch, yaw = euler_from_quaternion((rotation.x, rotation.y,
rotation.z, rotation.w))
return [pos.x, pos.y, yaw]
def getScan(self):
scan = self.scan_data.ranges
rpos = self.getPos()
a_start = self.scan_data.angle_min
a_increment = self.scan_data.angle_increment
scan_xy = []
for i in range(len(scan)):
if scan[i] < 0.1:
continue
x = scan[i] * math.cos(a_start + i * a_increment)
y = scan[i] * math.sin(a_start + i * a_increment)
x = x * math.cos(rpos[2]) - y * math.sin(rpos[2]) + rpos[0]
y = y * math.cos(rpos[2]) + x * math.sin(rpos[2]) + rpos[1]
scan_xy.append([x, y])
return np.array(scan_xy)
The above code is my calculation process. I want to transform the local coordinate positions of the LiDAR into global coordinate positions, but now, after the conversion, it does not achieve what I want. Where is the problem with the code above?
The issue with your method is that when you update
xand then use the newxto calculatey. It is the incorrect wayt to do it, because the calculation ofydepends on the originalxvalue, not the updated one.So, to solve this, you need to temporarily store the new
xvalue in a separate variable before updatingx. I actually did the same mistake you did in a previous project. You weren't far off, it you just missed a little detail.Here is a suggested code with dummy data:
which gives you: