Let’s Move the Robot the Way You Want!
Project Name: AI Robot Control System
Open the provided jupyter notebook
03_01_wheel.ipynb
- Running the cell codeCtrl + Enter
Import the necessary libraries
import rospy
import json
from std_msgs.msg import String
import time
import math
Create zetabot node with ros publisher. This will allow us to have object to control the movements of our robot.
Create robot_command Topic Publisher object.
pub = rospy.Publisher('/robot_command', String, queue_size=1)
rospy.init_node('zetabot', anonymous=True)
time.sleep(1)
Create a move() function
Convert {“MoveForward”: 1} to Json String
Publish the converted message
def move():
tmp = {"MoveForward": 1}
msg = json.dumps(tmp)
rospy.loginfo("Sent: %s", msg)
pub.publish(msg)
Create stop() function
Convert {“Stop”: 0} to Json String
Publish the converted message
def stop():
tmp = {"Stop": 0}
msg = json.dumps(tmp)
rospy.loginfo("Sent: %s", msg)
pub.publish(msg)
Create a moveTo() function
Convert {“MoveDelta”: -0.5} to Json String
Publish the converted message
Run the move() function
2 second time delay
Run the stop() function
move()
time.sleep(2)
stop()
Execute the moveTo(distance) function
1 distance forward
def moveTo():
tmp = {"MoveDelta": -0.5}
msg = json.dumps(tmp)
rospy.loginfo("Sent: %s", msg)
pub.publish(msg)
Execute the moveTo() function which will move the robot backwards for 0.5 meters
moveTo() # Move backwards for 0.5 meters
On top of moveTo() function, add distance parameter so that distance and directional information may be given.
def turnTo():
tmp = {"TurnDelta": math.radians(90)}
msg = json.dumps(tmp)
rospy.loginfo("Sent: %s", msg)
pub.publish(msg)
Execute the turnTo() function which will turn the robot 90 degrees of angle from its initial position.
turnTo()
On top of turnTo() function, add degree parameter so that we may specify how much to turn the robot.
def moveTo(distance):
tmp = {"MoveDelta": distance}
msg = json.dumps(tmp)
rospy.loginfo("Sent: %s", msg)
pub.publish(msg)
Create turnTo() function
Convert 45° to radians
Convert {“TurnDelta”: math.radians(45)} to Json string
Publish the converted message
def turnTo(degree):
tmp = {"TurnDelta": math.radians(int(degree))}
msg = json.dumps(tmp)
rospy.loginfo("Sent: %s", msg)
pub.publish(msg)
moveTo(1)
Run the turnTo(degree) function
180 degree clockwise rotation
turnTo(-180)
Run the turnTo(degree) function
90 degree counterclockwise rotation
turnTo(90)