Among multiple ways to move a robot, teleop is one of the most intuitive methods. In your project, you can add teleop feature easily for various purposes. For example, this code is used in the Robotag Project. Robotag Project is a game where robots play game of tag. In Robotag Project, this code is used to let the user take over the control and play as either cop or rubber.
How to use
The control is very intuitive, and there is short instruction built in to the system. w,a,d,x is for each directions, and s stops the robot. You can also play around with the original teleop and familiarize with the control using the original teleop.
What to add
Add the following code to the main class (not in a loop or if statement). These declares constants and mothods for teleop.
BURGER_MAX_LIN_VEL =0.22BURGER_MAX_ANG_VEL =2.84WAFFLE_MAX_LIN_VEL =0.26WAFFLE_MAX_ANG_VEL =1.82LIN_VEL_STEP_SIZE =0.01ANG_VEL_STEP_SIZE =0.1msg ="""Control Your TurtleBot3!---------------------------Moving around: w a s d xw/x : increase/decrease linear velocity (Burger : ~ 0.22, Waffle and Waffle Pi : ~ 0.26)a/d : increase/decrease angular velocity (Burger : ~ 2.84, Waffle and Waffle Pi : ~ 1.82)space key, s : force stopCTRL-C to quit"""e ="""Communications Failed"""defgetKey(): if os.name =='nt':if sys.version_info[0]>=3:return msvcrt.getch().decode()else:return msvcrt.getch() tty.setraw(sys.stdin.fileno()) rlist, _, _ = select.select([sys.stdin], [], [], 0.1)if rlist: key = sys.stdin.read(1)else: key ='' termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)return keydefvels(target_linear_vel,target_angular_vel):return"currently:\tlinear vel %s\t angular vel %s "% (target_linear_vel,target_angular_vel)defmakeSimpleProfile(output,input,slop):ifinput> output: output =min( input, output + slop )elifinput< output: output =max( input, output - slop )else: output =inputreturn outputdefconstrain(input,low,high):ifinput< low:input= lowelifinput> high:input= highelse:input=inputreturninputdefcheckLinearLimitVelocity(vel): vel =constrain(vel, -BURGER_MAX_LIN_VEL, BURGER_MAX_LIN_VEL)return veldefcheckAngularLimitVelocity(vel): vel =constrain(vel, -BURGER_MAX_ANG_VEL, BURGER_MAX_ANG_VEL)return vel
Then when you want to use teleop, add following code. These are the actual moving parts. Change 'use-teleop' to any other state name you need. If you want teleop to work in all times, take this code outside the if statement.