class RobotControl(object): """ Class used to interface with the rover. Gets sensor measurements through ROS subscribers, and transforms them into the 2D plane, and publishes velocity commands. """ def __init__(self, world_map, occupancy_map, pos_init, pos_goal, max_speed, max_omega, x_spacing, y_spacing, t_cam_to_body): """ Initialize the class """ # Handles all the ROS related items self.ros_interface = ROSInterface(t_cam_to_body) self.kalman_filter = KalmanFilter(world_map) print("INITSTATE", self.kalman_filter.state) self.diff_drive_controller = DiffDriveController(max_speed, max_omega) self.vel = 0 self.omega = 0 self.curInd = 0 self.path = dijkstras(occupancy_map, x_spacing, y_spacing, pos_init, pos_goal) print(self.path) self.curGoal = self.path[0] self.done = False def process_measurements(self): """ This function is called at 60Hz """ meas = self.ros_interface.get_measurements() print("Mesurements", meas) imu_meas = self.ros_interface.get_imu() print(imu_meas) updatedPosition = self.kalman_filter.step_filter( self.vel, self.omega, imu_meas, meas) print(np.linalg.norm(self.curGoal - updatedPosition[0:1])) if ((np.abs(self.curGoal[0] - updatedPosition[0]) > 0.1) or (np.abs(self.curGoal[1] - updatedPosition[1]) > 0.1)): (v, omega, done) = self.diff_drive_controller.compute_vel( updatedPosition, self.curGoal) self.vel = v self.omega = omega print("commanded vel:", v, omega) self.ros_interface.command_velocity(v, omega) else: print("updating") self.curInd = self.curInd + 1 if self.curInd < len(self.path): self.curGoal = self.path[self.curInd] else: self.done = True updatedPosition.shape = (3, 1) return
class RobotControl(object): """ Class used to interface with the rover. Gets sensor measurements through ROS subscribers, and transforms them into the 2D plane, and publishes velocity commands. """ def __init__(self, world_map,occupancy_map, pos_init, pos_goal, max_speed, max_omega, x_spacing, y_spacing, t_cam_to_body): """ Initialize the class Inputs: (all loaded from the parameter YAML file) world_map - a P by 4 numpy array specifying the location, orientation, and identification of all the markers/AprilTags in the world. The format of each row is (x,y,theta,id) with x,y giving 2D position, theta giving orientation, and id being an integer specifying the unique identifier of the tag. occupancy_map - an N by M numpy array of boolean values (represented as integers of either 0 or 1). This represents the parts of the map that have obstacles. It is mapped to metric coordinates via x_spacing and y_spacing pos_init - a 3 by 1 array specifying the initial position of the robot, formatted as usual as (x,y,theta) pos_goal - a 3 by 1 array specifying the final position of the robot, also formatted as (x,y,theta) max_speed - a parameter specifying the maximum forward speed the robot can go (i.e. maximum control signal for v) max_omega - a parameter specifying the maximum angular speed the robot can go (i.e. maximum control signal for omega) x_spacing - a parameter specifying the spacing between adjacent columns of occupancy_map y_spacing - a parameter specifying the spacing between adjacent rows of occupancy_map t_cam_to_body - numpy transformation between the camera and the robot (not used in simulation) """ # TODO for student: Comment this when running on the robot self.markers = world_map self.robot_sim = RobotSim(world_map, occupancy_map, pos_init, pos_goal, max_speed, max_omega, x_spacing, y_spacing) self.vel = np.array([0, 0]) self.imu_meas = np.array([]) self.meas = [] # TODO for student: Use this when transferring code to robot # Handles all the ROS related items #self.ros_interface = ROSInterface(t_cam_to_body) # YOUR CODE AFTER THIS # Uncomment as completed self.goals = dijkstras(occupancy_map, x_spacing, y_spacing, pos_init, pos_goal) self.total_goals = self.goals.shape[0] self.cur_goal = 1 self.kalman_filter = KalmanFilter(world_map) self.diff_drive_controller = DiffDriveController(max_speed, max_omega) def process_measurements(self): """ YOUR CODE HERE Main loop of the robot - where all measurements, control, and esimtaiton are done. This function is called at 60Hz """ # TODO for student: Comment this when running on the robot """ measurements - a N by 5 list of visible tags or None. The tags are in the form in the form (x,y,theta,id,time) with x,y being the 2D position of the marker relative to the robot, theta being the relative orientation of the marker with respect to the robot, id being the identifier from the map, and time being the current time stamp. If no tags are seen, the function returns None. """ # meas: measurements coming from tags meas = self.robot_sim.get_measurements() self.meas = meas; # imu_meas: measurment comig from the imu imu_meas = self.robot_sim.get_imu() self.imu_meas = imu_meas pose = self.kalman_filter.step_filter(self.vel, self.imu_meas, np.asarray(self.meas)) self.robot_sim.set_est_state(pose) # goal = self.goals[self.cur_goal] # vel = self.diff_drive_controller.compute_vel(pose, goal) # self.vel = vel[0:2] # self.robot_sim.command_velocity(vel[0], vel[1]) # close_enough = vel[2] # if close_enough: # print 'goal reached' # if self.cur_goal < (self.total_goals - 1): # self.cur_goal = self.cur_goal + 1 # vel = (0, 0) # self.vel = vel # self.robot_sim.command_velocity(vel[0], vel[1]) # else: # vel = (0, 0) # self.vel = vel # self.robot_sim.command_velocity(vel[0], vel[1]) # Code to follow AprilTags if(meas != None and meas): cur_meas = meas[0] tag_robot_pose = cur_meas[0:3] tag_world_pose = self.tag_pos(cur_meas[3]) state = self.robot_pos(tag_world_pose, tag_robot_pose) goal = tag_world_pose vel = self.diff_drive_controller.compute_vel(pose, goal) self.vel = vel[0:2]; if(not vel[2]): self.robot_sim.command_velocity(vel[0], vel[1]) else: vel = (0.1, 0.1) self.vel = vel self.robot_sim.command_velocity(vel[0], vel[1]) else: vel = (0.1, 0.1) self.vel = vel self.robot_sim.command_velocity(vel[0], vel[1]) # goal = [-0.5, 2.5] # goal = self.goals[self.cur_goal] # vel = self.diff_drive_controller.compute_vel(pose, goal) # self.vel = vel[0:2]; # if(not vel[2]): # self.robot_sim.command_velocity(vel[0], vel[1]) # else: # vel = (0, 0) # if self.cur_goal < (self.total_goals - 1): # self.cur_goal = self.cur_goal + 1 # self.vel = vel # TODO for student: Use this when transferring code to robot # meas = self.ros_interface.get_measurements() # imu_meas = self.ros_interface.get_imu() return def tag_pos(self, marker_id): for i in range(len(self.markers)): marker_i = np.copy(self.markers[i]) if marker_i[3] == marker_id: return marker_i[0:3] return None def robot_pos(self, w_pos, r_pos): H_W = np.array([[math.cos(w_pos[2]), -math.sin(w_pos[2]), w_pos[0]], [math.sin(w_pos[2]), math.cos(w_pos[2]), w_pos[1]], [0, 0, 1]]) H_R = np.array([[math.cos(r_pos[2]), -math.sin(r_pos[2]), r_pos[0]], [math.sin(r_pos[2]), math.cos(r_pos[2]), r_pos[1]], [0, 0, 1]]) w_r = H_W.dot(inv(H_R)) robot_pose = np.array([[w_r[0,2]], [w_r[1,2]], [math.atan2(w_r[1,0], w_r[0, 0])]]) return robot_pose
class RobotControl(object): """ Class used to interface with the rover. Gets sensor measurements through ROS subscribers, and transforms them into the 2D plane, and publishes velocity commands. """ def __init__(self, world_map,occupancy_map, pos_init, pos_goal, max_speed, max_omega, x_spacing, y_spacing, t_cam_to_body): """ Initialize the class Inputs: (all loaded from the parameter YAML file) world_map - a P by 4 numpy array specifying the location, orientation, and identification of all the markers/AprilTags in the world. The format of each row is (x,y,theta,id) with x,y giving 2D position, theta giving orientation, and id being an integer specifying the unique identifier of the tag. occupancy_map - an N by M numpy array of boolean values (represented as integers of either 0 or 1). This represents the parts of the map that have obstacles. It is mapped to metric coordinates via x_spacing and y_spacing pos_init - a 3 by 1 array specifying the initial position of the robot, formatted as usual as (x,y,theta) pos_goal - a 3 by 1 array specifying the final position of the robot, also formatted as (x,y,theta) max_speed - a parameter specifying the maximum forward speed the robot can go (i.e. maximum control signal for v) max_omega - a parameter specifying the maximum angular speed the robot can go (i.e. maximum control signal for omega) x_spacing - a parameter specifying the spacing between adjacent columns of occupancy_map y_spacing - a parameter specifying the spacing between adjacent rows of occupancy_map t_cam_to_body - numpy transformation between the camera and the robot (not used in simulation) """ # TODO for student: Comment this when running on the robot self.robot_sim = RobotSim(world_map, occupancy_map, pos_init, pos_goal, max_speed, max_omega, x_spacing, y_spacing) # TODO for student: Use this when transferring code to robot # Handles all the ROS related items #self.ros_interface = ROSInterface(t_cam_to_body) # Uncomment as completed self.kalman_filter = KalmanFilter(world_map) self.diff_drive_controller = DiffDriveController(max_speed, max_omega) plan = dijkstras(occupancy_map, x_spacing, y_spacing, pos_init, pos_goal) self.state_tol = 0.1 self.path = plan.tolist() print "Path: ", self.path, type(self.path) self.path.reverse() self.path.pop() self.state = pos_init self.goal = self.path.pop() self.x_offset = x_spacing self.vw = (0, 0, False) # self.goal[0] += self.x_offset/2 # self.goal[1] += y_spacing print "INIT GOAL: ", self.goal # def dijkstras(occupancy_map, x_spacing, y_spacing, start, goal): def process_measurements(self): """ Main loop of the robot - where all measurements, control, and estimation are done. This function is called at 60Hz """ meas = self.robot_sim.get_measurements() imu_meas = self.robot_sim.get_imu() self.vw = self.diff_drive_controller.compute_vel(self.state, self.goal) print "VW: ", self.vw print "Running Controller." if self.vw[2] == False: self.robot_sim.command_velocity(self.vw[0], self.vw[1]) else: self.robot_sim.command_velocity(0, 0) est_x = self.kalman_filter.step_filter(self.vw, imu_meas, meas) print "EST X: ", est_x, est_x[2][0] if est_x[2][0] > 2.617991667: est_x[2][0] = 2.617991667 if est_x[2][0] < 0.523598333: est_x[2][0] = 0.523598333 self.state = est_x print "Get GT Pose: ", self.robot_sim.get_gt_pose() print "EKF Pose: ", est_x self.robot_sim.get_gt_pose() self.robot_sim.set_est_state(est_x) if imu_meas != None: self.kalman_filter.prediction(self.vw, imu_meas) if meas != None and meas != []: print("Measurements: ", meas) if imu_meas != None: # self.kalman_filter.prediction(self.vw, imu_meas) self.kalman_filter.update(meas) pos_x_check = ((self.goal[0] + self.state_tol) > est_x.item(0)) and \ ((self.goal[0] - self.state_tol) < est_x.item(0)) pos_y_check = ((self.goal[1] + self.state_tol) > est_x.item(1)) and \ ((self.goal[1] - self.state_tol) < est_x.item(1)) if pos_x_check and pos_y_check: if self.path != []: self.goal = self.path.pop() # self.goal[0] += self.x_offset/2 # self.goal[1] += y_spacing else: self.goal = est_x
class RobotControl(object): """ Class used to interface with the rover. Gets sensor measurements through ROS subscribers, and transforms them into the 2D plane, and publishes velocity commands. """ def __init__(self, world_map, occupancy_map, pos_init, pos_goal, max_speed, max_omega, x_spacing, y_spacing, t_cam_to_body): """ Initialize the class Inputs: (all loaded from the parameter YAML file) world_map - a P by 4 numpy array specifying the location, orientation, and identification of all the markers/AprilTags in the world. The format of each row is (x,y,theta,id) with x,y giving 2D position, theta giving orientation, and id being an integer specifying the unique identifier of the tag. occupancy_map - an N by M numpy array of boolean values (represented as integers of either 0 or 1). This represents the parts of the map that have obstacles. It is mapped to metric coordinates via x_spacing and y_spacing pos_init - a 3 by 1 array specifying the initial position of the robot, formatted as usual as (x,y,theta) pos_goal - a 3 by 1 array specifying the final position of the robot, also formatted as (x,y,theta) max_speed - a parameter specifying the maximum forward speed the robot can go (i.e. maximum control signal for v) max_omega - a parameter specifying the maximum angular speed the robot can go (i.e. maximum control signal for omega) x_spacing - a parameter specifying the spacing between adjacent columns of occupancy_map y_spacing - a parameter specifying the spacing between adjacent rows of occupancy_map t_cam_to_body - numpy transformation between the camera and the robot (not used in simulation) """ # Handles all the ROS related items self.ros_interface = ROSInterface(t_cam_to_body) self.kalman_filter = KalmanFilter(world_map) self.prev_v = 0 self.est_pose = np.array([[0], [0], [0]]) self.diff_drive_controller = DiffDriveController(max_speed, max_omega) self.prev_imu_meas = np.array([[0], [0], [0], [0], [0]]) def process_measurements(self, waypoint): """ waypoint is a 1D list [x,y] containing the next waypoint the rover needs to go YOUR CODE HERE Main loop of the robot - where all measurements, control, and esimtaiton are done. This function is called at 60Hz """ #This gives the xy location and the orientation of the tag in the rover frame #The orientation is zero when the rover faces directly at the tag meas = self.ros_interface.get_measurements() self.est_pose = self.kalman_filter.step_filter(self.prev_v, self.prev_imu_meas, meas) state = [self.est_pose[0, 0], self.est_pose[1, 0], self.est_pose[2, 0]] goal = [waypoint[0], waypoint[1]] controls = self.diff_drive_controller.compute_vel(state, goal) self.ros_interface.command_velocity(controls[0], controls[1]) self.prev_v = controls[0] imu_meas = self.ros_interface.get_imu() if imu_meas != None: imu_meas[3, 0] = -imu_meas[ 3, 0] #clockwise angular vel is positive from IMU self.prev_imu_meas = imu_meas return controls[2]
class RobotControl(object): """ Class used to interface with the rover. Gets sensor measurements through ROS subscribers, and transforms them into the 2D plane, and publishes velocity commands. """ def __init__(self, world_map, occupancy_map, pos_init, pos_goal, max_speed, max_omega, x_spacing, y_spacing, t_cam_to_body): """ Initialize the class Inputs: (all loaded from the parameter YAML file) world_map - a P by 4 numpy array specifying the location, orientation, and identification of all the markers/AprilTags in the world. The format of each row is (x,y,theta,id) with x,y giving 2D position, theta giving orientation, and id being an integer specifying the unique identifier of the tag. occupancy_map - an N by M numpy array of boolean values (represented as integers of either 0 or 1). This represents the parts of the map that have obstacles. It is mapped to metric coordinates via x_spacing and y_spacing pos_init - a 3 by 1 array specifying the initial position of the robot, formatted as usual as (x,y,theta) pos_goal - a 3 by 1 array specifying the final position of the robot, also formatted as (x,y,theta) max_speed - a parameter specifying the maximum forward speed the robot can go (i.e. maximum control signal for v) max_omega - a parameter specifying the maximum angular speed the robot can go (i.e. maximum control signal for omega) x_spacing - a parameter specifying the spacing between adjacent columns of occupancy_map y_spacing - a parameter specifying the spacing between adjacent rows of occupancy_map t_cam_to_body - numpy transformation between the camera and the robot (not used in simulation) """ # TODO for student: Comment this when running on the robot #self.robot_sim = RobotSim(world_map, occupancy_map, pos_init, pos_goal, # max_speed, max_omega, x_spacing, y_spacing) # TODO for student: Use this when transferring code to robot # Handles all the ROS related items self.ros_interface = ROSInterface(t_cam_to_body) # YOUR CODE AFTER THIS # speed control variables self.v = 0.1 # allows persistent cmds through detection misses self.omega = -0.1 # allows persistent cmds through detection misses self.last_detect_time = rospy.get_time() #TODO on bot only self.missed_vision_debounce = 1 self.start_time = 0 # generate the path assuming we know our start location, goal, and environment self.path = dijkstras(occupancy_map, x_spacing, y_spacing, pos_init, pos_goal) self.path_idx = 0 self.mission_complete = False self.carrot_distance = 0.22 # Uncomment as completed self.kalman_filter = KalmanFilter(world_map) self.diff_drive_controller = DiffDriveController(max_speed, max_omega) def process_measurements(self): """ YOUR CODE HERE Main loop of the robot - where all measurements, control, and esimtaiton are done. This function is called at 60Hz """ print(' ') # TODO for student: Comment this when running on the robot #meas = self.robot_sim.get_measurements() #imu_meas = self.robot_sim.get_imu() # TODO for student: Use this when transferring code to robot meas = self.ros_interface.get_measurements() imu_meas = self.ros_interface.get_imu() # meas is the position of the robot with respect to the AprilTags # print(meas) # now that we have the measurements, update the predicted state self.kalman_filter.step_filter(self.v, imu_meas, meas) # print(self.kalman_filter.x_t) # TODO remove on bot, shows predicted state on simulator #self.robot_sim.set_est_state(self.kalman_filter.x_t) # pull the next path point from the list cur_goal = self.getCarrot() # cur_goal = self.path[self.path_idx] # TODO test to just go to a goal # cur_goal[0] = 0.43 # cur_goal[1] = 2 # calculate the control commands need to reach next path point #print('') #print('current goal:') #print(cur_goal) #print('current state:') #print(self.kalman_filter.x_t) control_cmd = self.diff_drive_controller.compute_vel( self.kalman_filter.x_t, cur_goal) self.v = control_cmd[0] self.omega = control_cmd[1] #print('control command:') # print(control_cmd) if self.mission_complete: self.v = 0 self.omega = 0 #print(control_cmd) if control_cmd[2]: if len(self.path) > (self.path_idx + 1): self.path_idx = self.path_idx + 1 print('next goal') else: self.mission_complete = True #TODO calibration test on bot only for linear velocity ''' if self.start_time - 0 < 0.0001: self.start_time = rospy.get_time() if(rospy.get_time() - self.start_time > 4): self.v = 0 self.omega = 0 else: self.v = 0.15 self.omega = 0 ''' #TODO on bot only self.ros_interface.command_velocity(self.v, self.omega) #TODO for simulation #self.robot_sim.command_velocity(self.v,self.omega) return def getCarrot(self): ''' getCarrot - generates an artificial goal location along a path out infront of the robot. path - the set of points which make up the waypoints in the path position - the current position of the robot ''' path = self.path idx = self.path_idx pos = self.kalman_filter.x_t # if the current line segment ends in the goal point, set that to the goal if self.path_idx + 1 == len(self.path): return self.path[(len(self.path) - 1)] else: # find the point on the current line closest to the robot # calculate current line's slope and intercept pt1 = self.path[self.path_idx] pt2 = self.path[self.path_idx + 1] x_diff = pt2[0] - pt1[0] y_diff = pt2[1] - pt1[1] vert = abs(x_diff) < 0.001 # using the current line's slope and intercept find the point on that # line closest to the robots current point # assumes all lines are either veritcal or horizontal x_bot = pos[0][0] y_bot = pos[1][0] if vert: x_norm = pt2[0] y_norm = y_bot else: x_norm = x_bot y_norm = pt2[1] # if the normal point is past the end point of this segment inc path idx # assumes all lines are either vertical or horizontal inc = False if vert: if (y_diff > 0 and y_norm > pt2[1]) or (y_diff < 0 and y_norm < pt2[1]): inc = True else: if (x_diff > 0 and x_norm > pt2[0]) or (x_diff < 0 and x_norm < pt2[0]): inc = True if (inc): self.path_idx = self.path_idx + 1 print('increment path index') # find a point L distance infront of the normal point on this line # assumes all lines are either vertical or horizontal if vert: x_goal = pt2[0] if y_diff > 0: y_goal = y_bot + self.carrot_distance else: y_goal = y_bot - self.carrot_distance else: y_goal = pt2[1] if x_diff > 0: x_goal = x_bot + self.carrot_distance else: x_goal = x_bot - self.carrot_distance goal = np.array([x_goal, y_goal]) #print(goal) return goal
class RobotControl(object): """ Class used to interface with the rover. Gets sensor measurements through ROS subscribers, and transforms them into the 2D plane, and publishes velocity commands. """ def __init__(self, world_map, occupancy_map, pos_init, pos_goal, max_speed, max_omega, x_spacing, y_spacing, t_cam_to_body): """ Initialize the class Inputs: (all loaded from the parameter YAML file) world_map - a P by 4 numpy array specifying the location, orientation, and identification of all the markers/AprilTags in the world. The format of each row is (x,y,theta,id) with x,y giving 2D position, theta giving orientation, and id being an integer specifying the unique identifier of the tag. occupancy_map - an N by M numpy array of boolean values (represented as integers of either 0 or 1). This represents the parts of the map that have obstacles. It is mapped to metric coordinates via x_spacing and y_spacing pos_init - a 3 by 1 array specifying the initial position of the robot, formatted as usual as (x,y,theta) pos_goal - a 3 by 1 array specifying the final position of the robot, also formatted as (x,y,theta) max_speed - a parameter specifying the maximum forward speed the robot can go (i.e. maximum control signal for v) max_omega - a parameter specifying the maximum angular speed the robot can go (i.e. maximum control signal for omega) x_spacing - a parameter specifying the spacing between adjacent columns of occupancy_map y_spacing - a parameter specifying the spacing between adjacent rows of occupancy_map t_cam_to_body - numpy transformation between the camera and the robot (not used in simulation) """ # TODO for student: Comment this when running on the robot self.robot_sim = RobotSim(world_map, occupancy_map, pos_init, pos_goal, max_speed, max_omega, x_spacing, y_spacing) # TODO for student: Use this when transferring code to robot # Handles all the ROS related items #self.ros_interface = ROSInterface(t_cam_to_body) # YOUR CODE AFTER THIS self.pos_goal = pos_goal self.world_map = world_map self.vel = np.array([0., 0.]) self.imu_meas = np.array([]) self.meas = [] self.max_speed = max_speed self.max_omega = max_omega self.goals = dijkstras(occupancy_map, x_spacing, y_spacing, pos_init, pos_goal) # print(self.goals) self.total_goals = self.goals.shape[0] self.cur_goal = 2 self.end_goal = self.goals.shape[0] - 1 self.est_pose = None # Uncomment as completed self.kalman_filter = KalmanFilter(world_map) self.diff_drive_controller = DiffDriveController(max_speed, max_omega) def process_measurements(self): """ YOUR CODE HERE Main loop of the robot - where all measurements, control, and esimtaiton are done. This function is called at 60Hz """ # TODO for student: Comment this when running on the robot meas = self.robot_sim.get_measurements() imu_meas = self.robot_sim.get_imu() # TODO for student: Use this when transferring code to robot # meas = self.ros_interface.get_measurements() # imu_meas = self.ros_interface.get_imu() self.est_pose = self.kalman_filter.step_filter(self.vel, imu_meas, np.asarray(meas)) # print(self.est_pose) # set goal # print(self.cur_goal) self.pos_goal = self.goals[self.cur_goal] # get command v, w, done = self.diff_drive_controller.compute_vel( self.est_pose, self.pos_goal) # while not at goal (waypoint), command velocity if done: v, w = (0, 0) if self.cur_goal < self.end_goal: self.cur_goal = self.cur_goal + 1 done = False #self.ros_interface.command_velocity(v,w) self.robot_sim.command_velocity(v, w) self.robot_sim.done = done self.vel = np.array([v, w]) # print(done) return
class RobotControl(object): """Class used to interface with the rover. Gets sensor measurements through ROS subscribers, and transforms them into the 2D plane, and publishes velocity commands. """ def __init__(self, markers, occupancy_map, pos_init, pos_goal, max_speed, min_speed, max_omega, x_spacing, y_spacing, t_cam_to_body, mode): """ Initialize the class """ # plan a path around obstacles using dijkstra's algorithm print('Planning path...') path = findShortestPath(occupancy_map, x_spacing, y_spacing, pos_init[0:2], pos_goal[0:2], dilate=2) print('Done!') self.path_manager = PathManager(path) self.kalman_filter = KalmanFilter(markers, pos_init) self.diff_drive_controller = DiffDriveController( max_speed, min_speed, max_omega) if 'HARDWARE' in mode: # Handles all the ROS related items self.ros_interface = ROSInterface(t_cam_to_body) elif 'SIMULATE' in mode: self.robot_sim = RobotSim(markers, occupancy_map, pos_init, pos_goal, max_speed, max_omega, x_spacing, y_spacing, self.path_manager.path, mode) self.user_control = UserControl() self.vel = 0 # save velocity to use for kalman filter self.goal = self.path_manager.getNextWaypoint() # get first waypoint # for logging postion data to csv file self.stateSaved = [] self.tagsSaved = [] self.waypoints = [] def process_measurements(self): """Main loop of the robot - where all measurements, control, and estimation are done. """ v, omega, wayptReached = self.user_control.compute_vel() self.robot_sim.command_velocity(v, omega) return #meas = None # for testing purposes #imu_meas = None # for testing purpose #pdb.set_trace() if (imu_meas is None) and (meas is None): pass else: state = self.kalman_filter.step_filter(self.vel, imu_meas, meas) if 'SIMULATE' in mode: self.robot_sim.set_est_state(state) #print("X = {} cm, Y = {} cm, Theta = {} deg".format(100*state[0],100*state[1],state[2]*180/np.pi)) # save the estimated state and tag statuses for offline animation self.stateSaved.append(state) self.waypoints.append(self.path_manager.getActiveWaypointsPos()) if meas is None: self.tagsSaved.append(None) else: meas = np.array(meas) tagIDs = [int(i) for i in meas[:, 3]] self.tagsSaved.append(tagIDs) v, omega, wayptReached = self.diff_drive_controller.compute_vel( state, self.goal.pos) self.vel = v if wayptReached: self.goal = self.path_manager.getNextWaypoint() self.diff_drive_controller.done = False # reset diff controller status if self.goal is None: print('Goal has been reached!') np.savez('savedState.npz', stateSaved=self.stateSaved, tagsSaved=self.tagsSaved, waypoints=self.waypoints) print('Position data saved!') if 'HARDWARE' in mode: self.ros_interface.command_velocity(0, 0) elif 'SIMULATE' in mode: self.robot_sim.command_velocity(0, 0) self.robot_sim.done = True return else: if 'HARDWARE' in mode: self.ros_interface.command_velocity(self.vel, omega) elif 'SIMULATE' in mode: self.robot_sim.command_velocity(self.vel, omega) return def myhook(): print "shutdown time!"
class RobotControl(object): """ Class used to interface with the rover. Gets sensor measurements through ROS subscribers, and transforms them into the 2D plane, and publishes velocity commands. """ def __init__(self, world_map,occupancy_map, pos_init, pos_goal, max_speed, max_omega, x_spacing, y_spacing, t_cam_to_body, sample_time): """ Initialize the class Inputs: (all loaded from the parameter YAML file) world_map - a P by 4 numpy array specifying the location, orientation, and identification of all the markers/AprilTags in the world. The format of each row is (x,y,theta,id) with x,y giving 2D position, theta giving orientation, and id being an integer specifying the unique identifier of the tag. occupancy_map - an N by M numpy array of boolean values (represented as integers of either 0 or 1). This represents the parts of the map that have obstacles. It is mapped to metric coordinates via x_spacing and y_spacing pos_init - a 3 by 1 array specifying the initial position of the robot, formatted as usual as (x,y,theta) pos_goal - a 3 by 1 array specifying the final position of the robot, also formatted as (x,y,theta) max_speed - a parameter specifying the maximum forward speed the robot can go (i.e. maximum control signal for v) max_omega - a parameter specifying the maximum angular speed the robot can go (i.e. maximum control signal for omega) x_spacing - a parameter specifying the spacing between adjacent columns of occupancy_map y_spacing - a parameter specifying the spacing between adjacent rows of occupancy_map t_cam_to_body - numpy transformation between the camera and the robot (not used in simulation) sample_time - the sample time in seconds between each measurement (added by me) """ # TODO for student: Comment this when running on the robot self.robot_sim = RobotSim(world_map, occupancy_map, pos_init, pos_goal, max_speed, max_omega, x_spacing, y_spacing) # TODO for student: Use this when transferring code to robot # Handles all the ROS related items #self.ros_interface = ROSInterface(t_cam_to_body) # YOUR CODE AFTER THIS # Uncomment as completed self.kalman_filter = KalmanFilter(world_map, sample_time) self.diff_drive_controller = DiffDriveController(max_speed, max_omega) self.v_last = 0.0 self.omega_last = 0.0 def process_measurements(self, goal): """ YOUR CODE HERE Main loop of the robot - where all measurements, control, and esimtaiton are done. This function is called at 60Hz """ # TODO for student: Comment this when running on the robot meas = np.array(self.robot_sim.get_measurements()) # measured pose of tag in robot frame (x,y,theta,id,time) imu_meas = self.robot_sim.get_imu() # 5 by 1 numpy vector (acc_x, acc_y, acc_z, omega, time) # Do KalmanFilter step # Note: The imu_meas could be None if it is sampled at a lower rate than the integration time step. # For the simulation, assume that imu_meas will always return a valid value because we control it in RobotSim.py. # For running on the robot, you should include protection for potentially bad measurements. pose_est = self.kalman_filter.step_filter(self.v_last, imu_meas, meas) at_goal = False v, omega, at_goal = self.diff_drive_controller.compute_vel(pose_est, goal) self.robot_sim.command_velocity(v, omega) self.v_last = v self.omega_last = omega #print("estimated state = ", est_state) #print("true state = ", self.robot_sim.get_gt_pose()) # Draw ghost robot est_state = np.array([[pose_est[0]],[pose_est[1]],[pose_est[2]]]) self.robot_sim.set_est_state(est_state) # TODO for student: Use this when transferring code to robot # meas = self.ros_interface.get_measurements() # imu_meas = self.ros_interface.get_imu() return at_goal
class RobotControl(object): """ Class used to interface with the rover. Gets sensor measurements through ROS subscribers, and transforms them into the 2D plane, and publishes velocity commands. """ def __init__(self, world_map,occupancy_map, pos_init, pos_goal, max_speed, max_omega, x_spacing, y_spacing, t_cam_to_body): """ Initialize the class Inputs: (all loaded from the parameter YAML file) world_map - a P by 4 numpy array specifying the location, orientation, and identification of all the markers/AprilTags in the world. The format of each row is (x,y,theta,id) with x,y giving 2D position, theta giving orientation, and id being an integer specifying the unique identifier of the tag. occupancy_map - an N by M numpy array of boolean values (represented as integers of either 0 or 1). This represents the parts of the map that have obstacles. It is mapped to metric coordinates via x_spacing and y_spacing pos_init - a 3 by 1 array specifying the initial position of the robot, formatted as usual as (x,y,theta) pos_goal - a 3 by 1 array specifying the final position of the robot, also formatted as (x,y,theta) max_speed - a parameter specifying the maximum forward speed the robot can go (i.e. maximum control signal for v) max_omega - a parameter specifying the maximum angular speed the robot can go (i.e. maximum control signal for omega) x_spacing - a parameter specifying the spacing between adjacent columns of occupancy_map y_spacing - a parameter specifying the spacing between adjacent rows of occupancy_map t_cam_to_body - numpy transformation between the camera and the robot (not used in simulation) """ # TODO for student: Comment this when running on the robot self.robot_sim = RobotSim(world_map, occupancy_map, pos_init, pos_goal, max_speed, max_omega, x_spacing, y_spacing) # TODO for student: Use this when transferring code to robot # Handles all the ROS related items #self.ros_interface = ROSInterface(t_cam_to_body) # YOUR CODE AFTER THIS # Uncomment as completed self.kalman_filter = KalmanFilter(world_map) self.diff_drive_controller = DiffDriveController(max_speed, max_omega) self.v_last = 0.0 self.omega_last = 0.0 def process_measurements(self): """ YOUR CODE HERE Main loop of the robot - where all measurements, control, and esimtaiton are done. This function is called at 60Hz """ # TODO for student: Comment this when running on the robot meas = np.array(self.robot_sim.get_measurements()) # tag wrt robot in robot frame (x,y,theta,id,time) imu_meas = self.robot_sim.get_imu() # (5,1) np array (xacc,yacc,zacc,omega,time) done = False goal = np.array([1., 1.2]) # z_t is the Nx4 numpy array (x,y,theta,id) of tag wrt robot if meas is not None and meas != []: #print(meas) #print(imu_meas) z_t = meas[:,:-1] # (3, 4) np array (x, y, theta, id) else: z_t = meas #print("v_last = ", self.v_last) #print("imu_meas = ", imu_meas) #print("z_t = ", z_t) state = self.kalman_filter.step_filter(self.v_last, self.omega_last, imu_meas, z_t) #print("estimated state = ", state) #print("true state = ", self.robot_sim.get_gt_pose()) # Draw ghost robot est_pose = np.array([[state[0]],[state[1]],[state[2]]]) self.robot_sim.set_est_state(est_pose) if done is False: v, omega, done = self.diff_drive_controller.compute_vel(state, goal) self.robot_sim.command_velocity(v, omega) self.v_last = v self.omega_last = omega #if meas is not None and meas != []: #print meas #state = np.array(meas[0][0:3]) #goal = np.array([0, 0]) #v, omega, done = self.diff_drive_controller.compute_vel(state, goal) #self.robot_sim.command_velocity(v, omega) # TODO for student: Use this when transferring code to robot # meas = self.ros_interface.get_measurements() # imu_meas = self.ros_interface.get_imu() return done
class RobotControl(object): """ Class used to interface with the rover. Gets sensor measurements through ROS subscribers, and transforms them into the 2D plane, and publishes velocity commands. """ def __init__(self, world_map,occupancy_map, pos_init, pos_goal, max_speed, max_omega, x_spacing, y_spacing, t_cam_to_body): """ Initialize the class """ # Handles all the ROS related items self.ros_interface = ROSInterface(t_cam_to_body) # YOUR CODE AFTER THIS # Uncomment as completed self.markers = world_map self.vel = np.array([0, 0]) self.imu_meas = np.array([]) self.meas = [] # TODO for student: Use this when transferring code to robot # Handles all the ROS related items #self.ros_interface = ROSInterface(t_cam_to_body) # YOUR CODE AFTER THIS # Uncomment as completed self.goals = dijkstras(occupancy_map, x_spacing, y_spacing, pos_init, pos_goal) # self.total_goals = self.goals.shape[0] self.cur_goal = 2 self.end_goal = self.goals.shape[0] - 1 self.kalman_filter = KalmanFilter(world_map) self.diff_drive_controller = DiffDriveController(max_speed, max_omega) def process_measurements(self): """ YOUR CODE HERE This function is called at 60Hz """ meas = self.ros_interface.get_measurements() self.meas = meas; print 'tag output' print meas # imu_meas: measurment comig from the imu imu_meas = self.ros_interface.get_imu() self.imu_meas = imu_meas print 'imu measurement' print imu_meas pose = self.kalman_filter.step_filter(self.vel, self.imu_meas, np.asarray(self.meas)) # Code to follow AprilTags ''' if(meas != None and meas): cur_meas = meas[0] tag_robot_pose = cur_meas[0:3] tag_world_pose = self.tag_pos(cur_meas[3]) state = self.robot_pos(tag_world_pose, tag_robot_pose) goal = tag_world_pose vel = self.diff_drive_controller.compute_vel(state, goal) self.vel = vel[0:2]; print vel if(not vel[2]): self.ros_interface.command_velocity(vel[0], vel[1]) else: vel = (0.01, 0.1) self.vel = vel self.ros_interface.command_velocity(vel[0], vel[1]) ''' # Code to move autonomously goal = self.goals[self.cur_goal] print 'pose' print pose print 'goal' print goal vel = self.diff_drive_controller.compute_vel(pose, goal) self.vel = vel[0:2]; print 'speed' print vel if(not vel[2]): self.ros_interface.command_velocity(vel[0], vel[1]) else: vel = (0, 0) if self.cur_goal < self.end_goal: self.cur_goal = self.cur_goal + 1 self.ros_interface.command_velocity(vel[0], vel[1]) self.vel = vel return def tag_pos(self, marker_id): for i in range(len(self.markers)): marker_i = np.copy(self.markers[i]) if marker_i[3] == marker_id: return marker_i[0:3] return None def robot_pos(self, w_pos, r_pos): H_W = np.array([[math.cos(w_pos[2]), -math.sin(w_pos[2]), w_pos[0]], [math.sin(w_pos[2]), math.cos(w_pos[2]), w_pos[1]], [0, 0, 1]]) H_R = np.array([[math.cos(r_pos[2]), -math.sin(r_pos[2]), r_pos[0]], [math.sin(r_pos[2]), math.cos(r_pos[2]), r_pos[1]], [0, 0, 1]]) w_r = H_W.dot(inv(H_R)) robot_pose = np.array([[w_r[0,2]], [w_r[1,2]], [math.atan2(w_r[1,0], w_r[0, 0])]]) return robot_pose
class RobotControl(object): """ Class used to interface with the rover. Gets sensor measurements through ROS subscribers, and transforms them into the 2D plane, and publishes velocity commands. """ def __init__(self, world_map, occupancy_map, pos_init, pos_goal, max_speed, max_omega, x_spacing, y_spacing, t_cam_to_body): """ Initialize the class Inputs: (all loaded from the parameter YAML file) world_map - a P by 4 numpy array specifying the location, orientation, and identification of all the markers/AprilTags in the world. The format of each row is (x,y,theta,id) with x,y giving 2D position, theta giving orientation, and id being an integer specifying the unique identifier of the tag. occupancy_map - an N by M numpy array of boolean values (represented as integers of either 0 or 1). This represents the parts of the map that have obstacles. It is mapped to metric coordinates via x_spacing and y_spacing pos_init - a 3 by 1 array specifying the initial position of the robot, formatted as usual as (x,y,theta) pos_goal - a 3 by 1 array specifying the final position of the robot, also formatted as (x,y,theta) max_speed - a parameter specifying the maximum forward speed the robot can go (i.e. maximum control signal for v) max_omega - a parameter specifying the maximum angular speed the robot can go (i.e. maximum control signal for omega) x_spacing - a parameter specifying the spacing between adjacent columns of occupancy_map y_spacing - a parameter specifying the spacing between adjacent rows of occupancy_map t_cam_to_body - numpy transformation between the camera and the robot (not used in simulation) """ # TODO for student: Comment this when running on the robot self.markers = world_map self.robot_sim = RobotSim(world_map, occupancy_map, pos_init, pos_goal, max_speed, max_omega, x_spacing, y_spacing) self.vel = np.array([0, 0]) self.imu_meas = np.array([]) self.meas = [] # TODO for student: Use this when transferring code to robot # Handles all the ROS related items #self.ros_interface = ROSInterface(t_cam_to_body) # YOUR CODE AFTER THIS # Uncomment as completed self.goals = dijkstras(occupancy_map, x_spacing, y_spacing, pos_init, pos_goal) self.total_goals = self.goals.shape[0] self.cur_goal = 1 self.kalman_filter = KalmanFilter(world_map) self.diff_drive_controller = DiffDriveController(max_speed, max_omega) def process_measurements(self): """ YOUR CODE HERE Main loop of the robot - where all measurements, control, and esimtaiton are done. This function is called at 60Hz """ # TODO for student: Comment this when running on the robot """ measurements - a N by 5 list of visible tags or None. The tags are in the form in the form (x,y,theta,id,time) with x,y being the 2D position of the marker relative to the robot, theta being the relative orientation of the marker with respect to the robot, id being the identifier from the map, and time being the current time stamp. If no tags are seen, the function returns None. """ # meas: measurements coming from tags meas = self.robot_sim.get_measurements() self.meas = meas # imu_meas: measurment comig from the imu imu_meas = self.robot_sim.get_imu() self.imu_meas = imu_meas pose = self.kalman_filter.step_filter(self.vel, self.imu_meas, np.asarray(self.meas)) self.robot_sim.set_est_state(pose) # goal = self.goals[self.cur_goal] # vel = self.diff_drive_controller.compute_vel(pose, goal) # self.vel = vel[0:2] # self.robot_sim.command_velocity(vel[0], vel[1]) # close_enough = vel[2] # if close_enough: # print 'goal reached' # if self.cur_goal < (self.total_goals - 1): # self.cur_goal = self.cur_goal + 1 # vel = (0, 0) # self.vel = vel # self.robot_sim.command_velocity(vel[0], vel[1]) # else: # vel = (0, 0) # self.vel = vel # self.robot_sim.command_velocity(vel[0], vel[1]) # Code to follow AprilTags if (meas != None and meas): cur_meas = meas[0] tag_robot_pose = cur_meas[0:3] tag_world_pose = self.tag_pos(cur_meas[3]) state = self.robot_pos(tag_world_pose, tag_robot_pose) goal = tag_world_pose vel = self.diff_drive_controller.compute_vel(pose, goal) self.vel = vel[0:2] if (not vel[2]): self.robot_sim.command_velocity(vel[0], vel[1]) else: vel = (0.1, 0.1) self.vel = vel self.robot_sim.command_velocity(vel[0], vel[1]) else: vel = (0.1, 0.1) self.vel = vel self.robot_sim.command_velocity(vel[0], vel[1]) # goal = [-0.5, 2.5] # goal = self.goals[self.cur_goal] # vel = self.diff_drive_controller.compute_vel(pose, goal) # self.vel = vel[0:2]; # if(not vel[2]): # self.robot_sim.command_velocity(vel[0], vel[1]) # else: # vel = (0, 0) # if self.cur_goal < (self.total_goals - 1): # self.cur_goal = self.cur_goal + 1 # self.vel = vel # TODO for student: Use this when transferring code to robot # meas = self.ros_interface.get_measurements() # imu_meas = self.ros_interface.get_imu() return def tag_pos(self, marker_id): for i in range(len(self.markers)): marker_i = np.copy(self.markers[i]) if marker_i[3] == marker_id: return marker_i[0:3] return None def robot_pos(self, w_pos, r_pos): H_W = np.array([[math.cos(w_pos[2]), -math.sin(w_pos[2]), w_pos[0]], [math.sin(w_pos[2]), math.cos(w_pos[2]), w_pos[1]], [0, 0, 1]]) H_R = np.array([[math.cos(r_pos[2]), -math.sin(r_pos[2]), r_pos[0]], [math.sin(r_pos[2]), math.cos(r_pos[2]), r_pos[1]], [0, 0, 1]]) w_r = H_W.dot(inv(H_R)) robot_pose = np.array([[w_r[0, 2]], [w_r[1, 2]], [math.atan2(w_r[1, 0], w_r[0, 0])]]) return robot_pose
class RobotControl(object): """ Class used to interface with the rover. Gets sensor measurements through ROS subscribers, and transforms them into the 2D plane, and publishes velocity commands. """ def __init__(self, world_map, occupancy_map, pos_init, pos_goal, max_speed, max_omega, x_spacing, y_spacing, t_cam_to_body): """ Initialize the class """ # Handles all the ROS related items self.ros_interface = ROSInterface(t_cam_to_body) self.pos_goal = pos_goal # YOUR CODE AFTER THIS #-------------------------------------------# self.time = rospy.get_time() self.controlOut = (0.0, 0.0, False) self.count_noMeasurement = 0 #-------------------------------------------# self.markers = world_map self.idx_target_marker = 0 # Calculate the optimal path # From pos_init to pos_goal self.path_2D = dijkstras(occupancy_map, x_spacing, y_spacing, pos_init, pos_goal) self.idx_path = 0 self.size_path = self.path_2D.shape[0] print "path.shape[0]", self.size_path # Generate the 3D path (include "theta") self.path = np.zeros((self.size_path, 3)) theta = 0.0 for idx in range(self.size_path - 1): delta = self.path_2D[(idx + 1), :] - self.path_2D[idx, :] theta = atan2(delta[1], delta[0]) if theta > np.pi: theta -= np.pi * 2 elif theta < -np.pi: theta += np.pi * 2 self.path[idx, :] = np.concatenate( (self.path_2D[idx, :], np.array([theta])), axis=1) self.path[self.size_path - 1, 0:2] = self.path_2D[self.size_path - 1, 0:2] self.path[self.size_path - 1, 2] = pos_goal[2] # theta # self.path[0, 2] = pos_init[2] # theta print "3D path:" print self.path # Uncomment as completed # Kalman filter self.kalman_filter = KalmanFilter(world_map) self.kalman_filter.mu_est = pos_init # 3*pos_init # For test # Differential drive self.diff_drive_controller = DiffDriveController(max_speed, max_omega) self.wayPointFollowing = wayPointFollowing(max_speed, max_omega) # self.task_done = False def process_measurements(self): """ YOUR CODE HERE This function is called at 60Hz """ meas = self.ros_interface.get_measurements() imu_meas = self.ros_interface.get_imu() # print 'meas',meas print 'imu_meas', imu_meas """ # Control the robot to track the tag if (meas is None) or (meas == []): # stop # self.ros_interface.command_velocity(0.0,0.0) # if self.count_noMeasurement > 30: # Actually the ros_interface will stop the motor itself if we don't keep sending new commands self.ros_interface.command_velocity(0.0,0.0) else: # Keep the old command self.ros_interface.command_velocity(self.controlOut[0],self.controlOut[1]) self.count_noMeasurement += 1 else: print 'meas',meas self.count_noMeasurement = 0 # Thew differential drive controller that lead the robot to the tag self.controlOut = self.diff_drive_controller.compute_vel((-1)*np.array(meas[0][0:3]),np.array([0.0,0.0,0.0])) self.ros_interface.command_velocity(self.controlOut[0],self.controlOut[1]) (v,omega,done) = (self.controlOut[0], self.controlOut[1],False ) """ """ if (rospy.get_time() - self.time) < 1.0: self.ros_interface.command_velocity(0.0, 3.0) # Linear velocity = 0.3 m/s, Angular velocity = 0.5 rad/s else: self.ros_interface.command_velocity(0.0,0.0) """ """ (v,omega,done) = (0.0, 0.0, False) self.ros_interface.command_velocity(v, omega) """ """ # Directly move to the goal position if self.controlOut[2]: # done self.ros_interface.command_velocity(0.0, 0.0) else: self.controlOut = self.wayPointFollowing.compute_vel(self.kalman_filter.mu_est, self.pos_goal ) # self.controlOut = self.diff_drive_controller.compute_vel(self.kalman_filter.mu_est, self.pos_goal ) if self.controlOut[2]: # done self.ros_interface.command_velocity(0.0, 0.0) else: self.ros_interface.command_velocity(self.controlOut[0], self.controlOut[1]) """ """ #----------------------------------------# # Switch the targets (way-point of the optimal path) and do the position control # if self.controlOut[2] and self.idx_path == self.size_path-1: # all done if self.idx_path == self.size_path-1 and self.controlOut[0] < 0.05 and self.controlOut[1] < 0.1 : # all done self.ros_interface.command_velocity(0.0, 0.0) else: self.controlOut = self.diff_drive_controller.compute_vel(self.kalman_filter.mu_est, self.path[self.idx_path,:] ) self.ros_interface.command_velocity(self.controlOut[0], self.controlOut[1]) if self.controlOut[2] and self.idx_path < self.size_path-1: # way-point done self.idx_path += 1 #----------------------------------------# """ #----------------------------------------# print "self.idx_path", self.idx_path # Switch the targets (way-point of the optimal path) and do the position control # way-point following (no reducing speeed when reach a way-point) # if self.controlOut[2] and self.idx_path == self.size_path-1: # all done if self.task_done: # all done self.ros_interface.command_velocity(0.0, 0.0) elif self.idx_path == self.size_path - 1: # The last one # Change the threshold self.diff_drive_controller.threshold = 0.02 # 3 cm # Using diff_drive_controller to reach the goal position with right direction self.controlOut = self.diff_drive_controller.compute_vel( self.kalman_filter.mu_est, self.path[self.idx_path, :]) # if self.controlOut[0] < 0.05 and self.controlOut[1] < 0.1 : # all done if self.controlOut[2]: # all done self.ros_interface.command_velocity(0.0, 0.0) self.task_done = True # all done else: self.ros_interface.command_velocity(self.controlOut[0], self.controlOut[1]) else: # The way-points, using wayPointFollowing to trace the trajectory without pausing if self.idx_path == self.size_path - 2: # The last 2nd one self.controlOut = self.diff_drive_controller.compute_vel( self.kalman_filter.mu_est, self.path[self.idx_path, :]) else: self.controlOut = self.wayPointFollowing.compute_vel( self.kalman_filter.mu_est, self.path[self.idx_path, :]) # self.controlOut = self.wayPointFollowing.compute_vel(self.kalman_filter.mu_est, self.path[self.idx_path,:] ) self.ros_interface.command_velocity(self.controlOut[0], self.controlOut[1]) if self.controlOut[ 2] and self.idx_path < self.size_path - 1: # way-point done self.idx_path += 1 #----------------------------------------# # self.time = rospy.get_time() # print "self.time",self.time # Kalman filter self.kalman_filter.step_filter(self.controlOut[0], (-1) * imu_meas, meas, rospy.get_time()) print "mu_est", self.kalman_filter.mu_est return