Reputation: 62
Suppose I have an array
sensor_data=[10,0,5,1,10,1,20,1,20,1,15]
Now in this:
0 means that the robot/drone has taken a right turn and
1 means that the robot/drone has taken a left turn.
The remaining of the digits are distances traveled.
So according to the above array, first the robot/drone travels a distance of 10 cm. Then it turn right. After turning right the robot/drone travels 5 cm and then it takes a left. After taking a left turn it travels 10 cm and so on. So initially the robot/drone is at (0,0). Then it travels straight i.e in the y direction.
Thus the coordinates would be (0,10). Then after the right turn and after traveling 5 cm, the coordinates would be (-5,10). Following such a pattern, the rest of the coordinates are: (-5,20),(15,20) and (15,0). What code can be written so that these coordinates can be generated from the above given array.
Upvotes: 0
Views: 227
Reputation: 434
Chew on this until you figure it out lol.
import numpy as np
from numpy import cos,sin,pi
import matplotlib.pyplot as plt
# Convert data into floats, for
sensor_data = tuple(map(lambda x: float(x),[10,0,5,1,10,1,20,1,20,1,15]))
# Start at 0,0 in a 2D plane and start out in x-Direction
Starting_Position = np.array((0.,0.))
Starting_Direction = np.array((1.,0.))
def Rotation_Matrix(direction):
'''Can be expanded for any angle of rotation in a 2D plane. Google rotation matrix in 2D space.'''
a = {'left':pi/2,'right':-pi/2}[direction]
matrix = np.array(((round(cos(a),7),round(-sin(a),7)),
(round(sin(a),7),round(cos(a),7))))
return matrix
def DronePosition(Number_input,Current_Position,Current_Direction):
if Number_input == 1.:
New_Direction = Current_Direction.dot(Rotation_Matrix('left'))
New_Position = Current_Position
elif Number_input == 0.:
New_Direction = Current_Direction.dot(Rotation_Matrix('right'))
New_Position = Current_Position
else:
New_Position = Current_Position + Current_Direction*Number_input
New_Direction = Current_Direction
return New_Position,New_Direction
Drone_Path = np.zeros(shape=(len(sensor_data),2))
for step in range(len(sensor_data)):
Drone_Path[step,0] = Starting_Position[0]
Drone_Path[step,1] = Starting_Position[1]
Starting_Position, Starting_Direction = DronePosition(sensor_data[step],Starting_Position,Starting_Direction)
fig, ax = plt.subplots(figsize=(6,6))
ax.plot(Drone_Path[:,0],Drone_Path[:,1])
plt.show()
Upvotes: 1