2019-05-06 20:23:31 +00:00
|
|
|
# startup:
|
2019-06-11 14:26:53 +00:00
|
|
|
# roscore -> start ros
|
|
|
|
# rosparam set cv_camera/device_id 0 -> set appropriate camera device
|
|
|
|
# rosrun cv_camera cv_camera_node -> start the camera
|
|
|
|
# roslaunch aruco_detect aruco_detect.launch camera:=cv_camera image:=image_raw dictionary:=16 transport:= fiducial_len:=0.1 # aruco marker detection
|
|
|
|
# python fiducial_to_2d_pos_angle.py -> compute position and angle of markers in 2d plane
|
2019-05-06 20:23:31 +00:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import rospy
|
|
|
|
import pygame
|
|
|
|
import numpy as np
|
|
|
|
import socket
|
2019-05-24 14:21:54 +00:00
|
|
|
import scipy.integrate
|
|
|
|
|
|
|
|
import threading
|
|
|
|
from copy import deepcopy
|
|
|
|
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
import matplotlib.animation as anim
|
|
|
|
|
|
|
|
import time
|
2019-05-06 20:23:31 +00:00
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
from marker_pos_angle.msg import id_pos_angle
|
2019-05-24 14:21:54 +00:00
|
|
|
|
2019-05-06 20:23:31 +00:00
|
|
|
class Robot:
|
|
|
|
def __init__(self, id, ip=None):
|
|
|
|
self.pos = None
|
2019-05-24 14:21:54 +00:00
|
|
|
self.orient = None
|
2019-05-06 20:23:31 +00:00
|
|
|
|
|
|
|
self.id = id
|
|
|
|
|
2019-05-24 14:21:54 +00:00
|
|
|
self.pos = None
|
|
|
|
self.euler = None
|
|
|
|
|
|
|
|
self.ip = ip
|
|
|
|
|
|
|
|
def f_ode(t, x, u):
|
|
|
|
# dynamical model of the two-wheeled robot
|
|
|
|
# TODO: find exact values for these parameters
|
|
|
|
r = 0.03
|
|
|
|
R = 0.05
|
|
|
|
d = 0.02
|
|
|
|
|
|
|
|
theta = x[2]
|
|
|
|
|
|
|
|
omega_r = u[0]
|
|
|
|
omega_l = u[1]
|
|
|
|
|
|
|
|
dx = np.zeros(3)
|
|
|
|
|
|
|
|
dx[0] = (r/2.0 * np.cos(theta) - r*d/(2.0*R) * np.sin(theta)) * omega_r \
|
|
|
|
+ (r/2.0 * np.cos(theta) + r*d/(2.0 * R) * np.sin(theta)) * omega_l
|
|
|
|
dx[1] = (r/2.0 * np.sin(theta) + r*d/(2.0*R) * np.cos(theta)) * omega_r \
|
|
|
|
+ (r/2 * np.sin(theta) - r*d/(2.0*R) * np.cos(theta)) * omega_l
|
|
|
|
dx[2] = -r/(2.0*R) * (omega_r - omega_l)
|
|
|
|
|
|
|
|
return dx
|
2019-05-06 20:23:31 +00:00
|
|
|
|
|
|
|
class RemoteController:
|
|
|
|
def __init__(self):
|
2019-05-24 14:21:54 +00:00
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
self.robots = [Robot(3)]
|
2019-05-06 20:23:31 +00:00
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
self.robot_ids = {}
|
|
|
|
for r in self.robots:
|
|
|
|
self.robot_ids[r.id] = r
|
2019-05-06 20:23:31 +00:00
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
# connect to robot
|
2019-05-06 20:23:31 +00:00
|
|
|
self.rc_socket = socket.socket()
|
|
|
|
try:
|
|
|
|
pass
|
2019-06-11 14:26:53 +00:00
|
|
|
self.rc_socket.connect(('192.168.1.101', 1234)) # connect to robot
|
2019-05-06 20:23:31 +00:00
|
|
|
except socket.error:
|
|
|
|
print("could not connect to socket")
|
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
self.t = time.time()
|
|
|
|
|
|
|
|
# variables for simulated state
|
|
|
|
self.x0 = None
|
|
|
|
self.ts = np.array([])
|
|
|
|
self.xs = []
|
|
|
|
|
|
|
|
# variables for measurements
|
|
|
|
self.tms_0 = None
|
|
|
|
self.xm_0 = None
|
|
|
|
self.tms = None
|
|
|
|
self.xms = None
|
|
|
|
|
2019-05-24 14:21:54 +00:00
|
|
|
self.mutex = threading.Lock()
|
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
marker_sub = rospy.Subscriber("/marker_id_pos_angle", id_pos_angle, self.measurement_callback)
|
|
|
|
|
|
|
|
|
|
|
|
# pid parameters
|
2019-05-24 14:21:54 +00:00
|
|
|
self.k = 0
|
|
|
|
self.ii = 0.1
|
|
|
|
self.pp = 0.4
|
|
|
|
|
|
|
|
self.inc = 0.0
|
|
|
|
|
|
|
|
self.alphas = []
|
|
|
|
|
|
|
|
self.speed = 1.0
|
|
|
|
self.controlling = False
|
|
|
|
|
|
|
|
self.u1 = 0.0
|
|
|
|
self.u2 = 0.0
|
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
# animation
|
2019-05-24 14:21:54 +00:00
|
|
|
self.fig = plt.figure()
|
|
|
|
self.ax = self.fig.add_subplot(1,1,1)
|
|
|
|
self.xdata, self.ydata = [], []
|
|
|
|
self.line, = self.ax.plot([],[])
|
|
|
|
self.line_sim, = self.ax.plot([], [])
|
2019-06-11 14:26:53 +00:00
|
|
|
self.dirm, = self.ax.plot([], [])
|
|
|
|
self.dirs, = self.ax.plot([], [])
|
2019-05-24 14:21:54 +00:00
|
|
|
plt.xlabel('x-position')
|
|
|
|
plt.ylabel('y-position')
|
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
def ani(self):
|
|
|
|
self.ani = anim.FuncAnimation(self.fig, init_func=self.ani_init, func=self.ani_update, interval=10, blit=True)
|
|
|
|
plt.ion()
|
|
|
|
plt.show(block=True)
|
2019-05-24 14:21:54 +00:00
|
|
|
|
|
|
|
def ani_init(self):
|
|
|
|
self.ax.set_xlim(-2, 2)
|
|
|
|
self.ax.set_ylim(-2, 2)
|
|
|
|
self.ax.set_aspect('equal', adjustable='box')
|
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
return self.line, self.line_sim, self.dirm, self.dirs,
|
2019-05-24 14:21:54 +00:00
|
|
|
|
|
|
|
def ani_update(self, frame):
|
2019-06-11 14:26:53 +00:00
|
|
|
#print("plotting")
|
2019-05-24 14:21:54 +00:00
|
|
|
self.mutex.acquire()
|
2019-05-06 20:23:31 +00:00
|
|
|
try:
|
2019-05-24 14:21:54 +00:00
|
|
|
# copy data for plot from global arrays
|
|
|
|
if self.tms is not None:
|
|
|
|
tm_local = deepcopy(self.tms)
|
|
|
|
xm_local = deepcopy(self.xms)
|
|
|
|
|
|
|
|
if len(tm_local) > 0:
|
2019-06-11 14:26:53 +00:00
|
|
|
# plot path of the robot
|
2019-05-24 14:21:54 +00:00
|
|
|
self.line.set_data(xm_local[:,0], xm_local[:,1])
|
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
# compute and plot direction the robot is facing
|
2019-05-24 14:21:54 +00:00
|
|
|
a = xm_local[-1, 0]
|
2019-06-11 14:26:53 +00:00
|
|
|
b = xm_local[-1, 1]
|
2019-05-24 14:21:54 +00:00
|
|
|
|
|
|
|
a2 = a + np.cos(xm_local[-1, 2]) * 1.0
|
|
|
|
b2 = b + np.sin(xm_local[-1, 2]) * 1.0
|
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
self.dirm.set_data(np.array([a, a2]), np.array([b, b2]))
|
2019-05-24 14:21:54 +00:00
|
|
|
|
|
|
|
ts_local = deepcopy(self.ts)
|
|
|
|
xs_local = deepcopy(self.xs)
|
|
|
|
|
|
|
|
if len(ts_local) > 0:
|
2019-06-11 14:26:53 +00:00
|
|
|
# plot simulated path of the robot
|
2019-05-24 14:21:54 +00:00
|
|
|
self.line_sim.set_data(xs_local[:,0], xs_local[:,1])
|
2019-06-11 14:26:53 +00:00
|
|
|
|
|
|
|
# compute and plot direction the robot is facing
|
|
|
|
a = xs_local[-1, 0]
|
|
|
|
b = xs_local[-1, 1]
|
|
|
|
|
|
|
|
a2 = a + np.cos(xs_local[-1, 2]) * 1.0
|
|
|
|
b2 = b + np.sin(xs_local[-1, 2]) * 1.0
|
|
|
|
|
|
|
|
self.dirs.set_data(np.array([a, a2]), np.array([b, b2]))
|
2019-05-24 14:21:54 +00:00
|
|
|
finally:
|
|
|
|
self.mutex.release()
|
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
return self.line, self.line_sim, self.dirm, self.dirs,
|
2019-05-24 14:21:54 +00:00
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
def measurement_callback(self, data):
|
|
|
|
#print("data = {}".format(data))
|
|
|
|
if data.id in self.robot_ids:
|
|
|
|
r = self.robot_ids[data.id]
|
2019-05-06 20:23:31 +00:00
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
r.pos = (data.x, data.y) # only x and y component are important for us
|
|
|
|
r.euler = data.angle
|
2019-05-06 20:23:31 +00:00
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
#print("r.pos = {}".format(r.pos))
|
|
|
|
#print("r.angle = {}".format(r.euler))
|
2019-05-06 20:23:31 +00:00
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
# save measured position and angle for plotting
|
|
|
|
measurement = np.array([r.pos[0], r.pos[1], r.euler])
|
|
|
|
if self.tms_0 is None:
|
|
|
|
self.tms_0 = time.time()
|
|
|
|
self.xm_0 = measurement
|
2019-05-06 20:23:31 +00:00
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
self.mutex.acquire()
|
|
|
|
try:
|
|
|
|
self.tms = np.array([0.0])
|
|
|
|
self.xms = measurement
|
|
|
|
finally:
|
|
|
|
self.mutex.release()
|
2019-05-24 14:21:54 +00:00
|
|
|
else:
|
2019-06-11 14:26:53 +00:00
|
|
|
self.mutex.acquire()
|
|
|
|
try:
|
|
|
|
self.tms = np.vstack((self.tms, time.time() - self.tms_0))
|
|
|
|
self.xms = np.vstack((self.xms, measurement))
|
|
|
|
finally:
|
|
|
|
self.mutex.release()
|
2019-05-24 14:21:54 +00:00
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
def controller(self):
|
|
|
|
print("starting control")
|
2019-05-06 20:23:31 +00:00
|
|
|
while True:
|
2019-06-11 14:26:53 +00:00
|
|
|
|
|
|
|
keyboard_control = False
|
2019-05-24 14:21:54 +00:00
|
|
|
keyboard_control_speed_test = False
|
2019-06-11 14:26:53 +00:00
|
|
|
pid = True
|
2019-05-24 14:21:54 +00:00
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
if keyboard_control: # keyboard controller
|
2019-05-24 14:21:54 +00:00
|
|
|
events = pygame.event.get()
|
2019-06-11 14:26:53 +00:00
|
|
|
speed = 0.5
|
2019-05-24 14:21:54 +00:00
|
|
|
for event in events:
|
|
|
|
if event.type == pygame.KEYDOWN:
|
|
|
|
if event.key == pygame.K_LEFT:
|
|
|
|
self.u1 = -speed
|
|
|
|
self.u2 = speed
|
2019-06-11 14:26:53 +00:00
|
|
|
#print("turn left: ({},{})".format(u1, u2))
|
|
|
|
elif event.key == pygame.K_RIGHT:
|
|
|
|
self.u1 = speed
|
|
|
|
self.u2 = -speed
|
2019-05-24 14:21:54 +00:00
|
|
|
#print("turn right: ({},{})".format(u1, u2))
|
|
|
|
elif event.key == pygame.K_UP:
|
|
|
|
self.u1 = speed
|
|
|
|
self.u2 = speed
|
2019-06-11 14:26:53 +00:00
|
|
|
#print("forward: ({},{})".format(self.u1, self.u2))
|
|
|
|
elif event.key == pygame.K_DOWN:
|
|
|
|
self.u1 = -speed
|
|
|
|
self.u2 = -speed
|
2019-05-24 14:21:54 +00:00
|
|
|
#print("forward: ({},{})".format(u1, u2))
|
|
|
|
self.rc_socket.send('({},{})\n'.format(self.u1, self.u2))
|
|
|
|
elif event.type == pygame.KEYUP:
|
|
|
|
self.u1 = 0
|
|
|
|
self.u2 = 0
|
|
|
|
#print("key released, resetting: ({},{})".format(u1, u2))
|
|
|
|
self.rc_socket.send('({},{})\n'.format(self.u1, self.u2))
|
|
|
|
|
|
|
|
tnew = time.time()
|
|
|
|
dt = tnew - self.t
|
|
|
|
r = scipy.integrate.ode(f_ode)
|
|
|
|
r.set_f_params(np.array([self.u1 * 13.32, self.u2 * 13.32]))
|
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
#print(self.x0)
|
|
|
|
if self.x0 is None:
|
|
|
|
if self.xm_0 is not None:
|
|
|
|
self.x0 = self.xm_0
|
|
|
|
self.xs = self.x0
|
|
|
|
else:
|
|
|
|
print("error: no measurement available to initialize simulation")
|
2019-05-24 14:21:54 +00:00
|
|
|
x = self.x0
|
|
|
|
r.set_initial_value(x, self.t)
|
|
|
|
xnew = r.integrate(r.t + dt)
|
|
|
|
|
|
|
|
self.t = tnew
|
|
|
|
self.x0 = xnew
|
|
|
|
|
|
|
|
self.mutex.acquire()
|
|
|
|
try:
|
2019-06-11 14:26:53 +00:00
|
|
|
self.ts = np.append(self.ts, tnew)
|
2019-05-24 14:21:54 +00:00
|
|
|
self.xs = np.vstack((self.xs, xnew))
|
|
|
|
finally:
|
|
|
|
self.mutex.release()
|
|
|
|
|
|
|
|
|
|
|
|
elif keyboard_control_speed_test:
|
|
|
|
events = pygame.event.get()
|
|
|
|
for event in events:
|
|
|
|
if event.type == pygame.KEYDOWN:
|
|
|
|
if event.key == pygame.K_LEFT:
|
|
|
|
self.speed = self.speed / np.sqrt(np.sqrt(np.sqrt(10.0)))
|
|
|
|
elif event.key == pygame.K_RIGHT:
|
|
|
|
self.speed = self.speed * np.sqrt(np.sqrt(np.sqrt(10.0)))
|
|
|
|
elif event.key == pygame.K_UP:
|
|
|
|
u1 = self.speed
|
|
|
|
u2 = -self.speed
|
|
|
|
elif event.key == pygame.K_DOWN:
|
|
|
|
u1 = 0.0
|
|
|
|
u2 = 0.0
|
|
|
|
print("speed = {}".format(self.speed))
|
|
|
|
self.rc_socket.send('({},{})\n'.format(u1, u2))
|
|
|
|
|
|
|
|
elif pid:
|
2019-06-11 14:26:53 +00:00
|
|
|
# pid controller
|
|
|
|
|
2019-05-24 14:21:54 +00:00
|
|
|
events = pygame.event.get()
|
|
|
|
for event in events:
|
|
|
|
if event.type == pygame.KEYDOWN:
|
|
|
|
if event.key == pygame.K_LEFT:
|
|
|
|
self.ii = self.ii / np.sqrt(np.sqrt(np.sqrt(10.0)))
|
|
|
|
print("ii = {}".format(self.pp))
|
|
|
|
elif event.key == pygame.K_RIGHT:
|
|
|
|
self.ii = self.ii * np.sqrt(np.sqrt(np.sqrt(10.0)))
|
|
|
|
print("ii = {}".format(self.pp))
|
|
|
|
elif event.key == pygame.K_UP:
|
|
|
|
self.controlling = True
|
|
|
|
elif event.key == pygame.K_DOWN:
|
|
|
|
self.controlling = False
|
|
|
|
self.rc_socket.send('({},{})\n'.format(0, 0))
|
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
dt = 0.05
|
2019-05-24 14:21:54 +00:00
|
|
|
|
|
|
|
if self.controlling:
|
|
|
|
# test: turn robot such that angle is zero
|
|
|
|
for r in self.robots:
|
|
|
|
if r.euler is not None:
|
|
|
|
self.k = self.k + 1
|
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
alpha = r.euler
|
2019-05-24 14:21:54 +00:00
|
|
|
self.alphas.append(alpha)
|
2019-06-11 14:26:53 +00:00
|
|
|
|
|
|
|
# compute error
|
2019-05-24 14:21:54 +00:00
|
|
|
e = alpha - 0
|
2019-06-11 14:26:53 +00:00
|
|
|
|
|
|
|
# compute integral of error (approximately)
|
2019-05-24 14:21:54 +00:00
|
|
|
self.inc += e * dt
|
2019-06-11 14:26:53 +00:00
|
|
|
|
|
|
|
# PID
|
|
|
|
p = self.pp * e
|
|
|
|
i = self.ii * self.inc
|
2019-05-24 14:21:54 +00:00
|
|
|
d = 0.0
|
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
# compute controls for robot from PID
|
|
|
|
u1 = p + i + d
|
|
|
|
u2 = - p - i - d
|
2019-05-24 14:21:54 +00:00
|
|
|
print("alpha = {}, u = ({}, {})".format(alpha, u1, u2))
|
|
|
|
self.rc_socket.send('({},{})\n'.format(u1, u2))
|
2019-05-06 20:23:31 +00:00
|
|
|
|
2019-06-11 14:26:53 +00:00
|
|
|
time.sleep(dt)
|
2019-05-06 20:23:31 +00:00
|
|
|
|
|
|
|
def main(args):
|
|
|
|
rospy.init_node('controller_node', anonymous=True)
|
|
|
|
|
|
|
|
rc = RemoteController()
|
|
|
|
|
|
|
|
pygame.init()
|
2019-06-11 14:26:53 +00:00
|
|
|
|
|
|
|
screenheight = 480
|
|
|
|
screenwidth = 640
|
|
|
|
screen = pygame.display.set_mode([screenwidth, screenheight])
|
|
|
|
|
|
|
|
threading.Thread(target=rc.controller).start()
|
|
|
|
|
|
|
|
rc.ani()
|
2019-05-06 20:23:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main(sys.argv)
|