Welcome to Muto RS Robot repository
8)Motion learning
8.Action learning
1. Introduction
This course mainly implements the learning function by reading and writing the servo angle. The idea of action learning is to record each attitude into a local txt file, and then run multiple postures in sequence to form an action.
2. Core content analysis
ACTION_FILE_NAME records the location where the action is saved. For the convenience of viewing, the text file is placed in the same directory of the program. Generally, it does not need to be modified.
ACTION_FILE_NAME = "Action_Data.txt"
Since robot motion learning requires unloading of the two front leg servos, the positions of the two middle legs need to be set first to ensure a stable state when learning is started.
xxxxxxxxxxdef ready_study():g_bot.reset()time.sleep(1)g_bot.motor(5, -60, 500)g_bot.motor(14, -60, 500)time.sleep(.5)g_bot.motor(4, 30, 500)g_bot.motor(13, -30, 500)time.sleep(.5)g_bot.motor(5, -35, 500)g_bot.motor(14, -35, 500)time.sleep(.5)
Start learning the movements and uninstall the two front leg servos, leg1 and leg6 respectively.
xxxxxxxxxxdef start_study():g_bot.unload_leg(1)time.sleep(.1)g_bot.unload_leg(6)
Learn the action, read the robot's current attitude and servo angle, and use Python's own open function to open and write a line of data (the two front leg servo angle values).
xxxxxxxxxxdef study_action():read_angle = g_bot.read_motor()angle_value = [0,0,0,0,0,0]for i in range(3):angle_value[i] = read_angle[i]angle_value[5-i] = read_angle[17-i]write_action(ACTION_FILE_NAME, angle_value)def write_action(file_name, value):if len(value) != 6:returnbase_dir = str(os.getcwd())base_dir = base_dir.replace('\\', '/')file_path = base_dir + "/" + str(file_name)with open(file_path, "a") as f:wf_str = str(value[0]) + ', ' + str(value[1]) + ', ' + \str(value[2]) + ', ' + str(value[3]) + ', ' + \str(value[4]) + ', ' + str(value[5]) + '\n'f.write(wf_str)f.flush()
Stop the learning action and reload the two front leg servos.
xxxxxxxxxxdef stop_study():g_bot.load_leg(1)time.sleep(.1)g_bot.load_leg(6)
Read all actions, read the actions in the txt text line by line, save them to the group, and return the group array.
xxxxxxxxxxdef read_all_action(file_name):base_dir = str(os.getcwd())base_dir = base_dir.replace('\\', '/')file_path = base_dir + "/" + str(file_name)group = ()try:with open(file_path, "r+") as f:lines = f.readlines()for line in lines:if len(line) != 0:list = line.split(',')if len(list) == 6:action = ([int(list[0]), int(list[1]), int(list[2]),int(list[3]), int(list[4]), int(list[5])], )group = group + actionexcept FileNotFoundError:print(str(file_name), "File not Found")return Noneexcept:return Nonereturn group
Run the action group and run the read group action groups in sequence. You can modify the runtime time of the motor function (default 800) and the sleep time below to control the speed of the servo.
xxxxxxxxxxdef play_action():group = read_all_action(ACTION_FILE_NAME)if group == None:print("No action found")returnlen_group = len(group)print("group:", len_group, group)