33 tkmobilerobotsimulator_spec = [
"implementation_id",
"TkMobileRobotSimulator",
34 "type_name",
"TkMobileRobotSimulator",
35 "description",
"sample component for Python and Tkinter",
37 "vendor",
"Noriaki Ando, AIST",
38 "category",
"example",
39 "activity_type",
"DataFlowComponent",
42 "lang_type",
"SCRIPT",
49 OpenRTM_aist.DataFlowComponentBase.__init__(self, manager)
56 self.
_d_vel = RTC.TimedFloatSeq(RTC.Time(0,0),[])
59 self.
_d_pos = RTC.TimedFloatSeq(RTC.Time(0,0),[])
120 ToggleItem.__init__(self)
148 ToggleItem.__init__(self)
150 self.
trans = simulator.get_translation()
165 rx, ry, rt = self.
robot.get_pos()
171 x0, y0 = self.
trans(tmp_x0, tmp_y0, rx, ry, 0)
172 x1, y1 = self.
trans(tmp_x1, tmp_y1, rx, ry, 0)
176 fill=
"", outline=
"#aaaaaa")
181 lx0 = xo + (r / math.sqrt(2))
182 ly0 = yo - (r / math.sqrt(2))
191 anchor=E, text=self.
name)
192 pos_text =
'(%5.2f, %5.2f, %5.2f)' % (rx, ry, (rt*180/math.pi)%360)
194 anchor=E, text=pos_text)
213 def __init__(self, canvas, x0, y0, width, height, pitch, color, linewd):
214 ToggleItem.__init__(self)
234 x_start = int(self.
x0 % self.
pitch)
235 x_num = int((self.
width - x_start) / self.
pitch) + 1
236 for x
in range(x_num):
237 x0 = x_start + self.
pitch * x
242 y_start = int(self.
y0 % self.
pitch)
243 y_num = int((self.
height - y_start) / self.
pitch) + 1
244 for y
in range(y_num):
245 y0 = y_start + self.
pitch * y
246 id = self.
canvas.create_line(0, y0, self.
width, y0,
273 ToggleItem.__init__(self)
309 self.
tick = simulator.get_tick()
311 self.
trans = simulator.get_translation()
315 return self.
trans(x, y, dx, dy, dth)
321 import tkinter.simpledialog
352 self.
param.
append({
"label":label,
"var":variable,
"unit":unit})
358 self.
input.
append({
"label":label,
"var":variable,
"unit":unit})
364 self.
output.
append({
"label":label,
"var":variable,
"unit":unit})
385 w0 = LabelFrame(f, label=
"Robot's Profile",
386 options=
"frame.anchor w frame.justify left")
387 prof_frame = w0.subwidget(
'frame')
390 w1 = LabelFrame(f, label=
"Robot's Parameters")
391 param_frame = w1.subwidget(
'frame')
396 w2 = LabelFrame(f, label=
"Robot's Input Values")
397 input_frame = w2.subwidget(
'frame')
402 w3 = LabelFrame(f, label=
"Robot's Output Values")
403 output_frame = w3.subwidget(
'frame')
409 for w
in [w0, w1, w2, w3]:
410 w.pack(side=TOP, anchor=W, fill=X)
421 bt = Button(master, text=label, command=func, width=10,
423 bt.pack(side=TOP, padx=5, pady=5)
428 t = [
"Robot's name: ",
"Robot's type: ",
"Description: ",
"Vendor: "]
429 for i
in range(len(t)):
430 Label(master, text=t[i], anchor=W).grid(row=i, sticky=W,
433 for i
in range(len(l)):
434 Label(master, text=l[i], anchor=W).grid(row=i, column=1, sticky=W,
441 l0 = Label(f, text=label0, width=self.
label_len, justify=LEFT, anchor=W)
442 e =
Entry(f, width=7, textvariable=var,
443 justify=RIGHT, relief=GROOVE, bd=2)
444 l1 = Label(f, text=label1, width=self.
unit_len, justify=LEFT, anchor=W)
445 for w
in [l0, e, l1]:
446 w.pack(side=LEFT, anchor=W, padx=3, pady=3)
457 def __init__(self, radius, wheeld, pos = (0, 0, math.pi/2), dt=0.1):
504 x_dot = self.
radius * (w1 + w2) * math.cos(self.
pre_t)
505 y_dot = self.
radius * (w1 + w2) * math.sin(self.
pre_t)
527 pos = (0, 0, math.pi/2)):
528 SimulatedObject.__init__(self, simulator)
531 self.
fig = [[10, 0], [5, 10], [-10, 10], [-10, -10], [5, -10]]
535 self.
name =
"DDMobileRobot" + str(self.__class__.count)
536 self.__class__.count += 1
537 self.
comp = OpenRTM_aist.Manager.instance().createComponent(
"TkMobileRobotSimulator")
569 (x, y, th) = xxx_todo_changeme
588 v = self.
comp.get_velocity()
593 self.
comp.set_position((self.x, self.y, self.
th_deg))
594 self.
xentry.set(
'%5.2f'%self.x)
595 self.
yentry.set(
'%5.2f'%self.y)
606 robotfig.append(self.
translate(pos[0], pos[1],
607 self.x, self.y, self.
th))
610 self.
id = self.
canvas.create_polygon(robotfig,
624 p.set_profile(self.
name,
"DDMobileRobot",
625 "Differential Drive Mobile Robot",
"AIST")
626 p.append_parameter(
"Wheel radius r: ", self.
rentry,
"[m]")
627 p.append_parameter(
"Wheel distance d: ", self.
dentry,
"[m]")
628 p.append_input(
"Angular velocity (LEFT) wl: ", self.
wlentry,
"[rad/s]")
629 p.append_input(
"Angular velocity (RIGHT) wr: ", self.
wrentry,
"[rad/s]")
630 p.append_output(
"Robot position x : ", self.
xentry,
"[m]")
631 p.append_output(
"Robot position y : ", self.
yentry,
"[m]")
632 p.append_output(
"Robot position th: ", self.
tentry,
"[deg]")
641 self.
set_pos((0.0, 0.0, math.pi/2))
646 r = float(self.
rentry.get())
647 d = float(self.
dentry.get())
648 self.
model.set_wheel_radius(r)
649 self.
model.set_wheel_distance(d)
662 def __init__(self, master=None, width=800, height=600):
663 Frame.__init__(self, master)
713 self.
canvas.pack(side=LEFT)
721 self.
frame.pack(side=LEFT)
740 for o
in list(self.
robots.keys()):
742 for r
in list(self.
rnames.keys()):
763 dummy = Frame(frame, width=self.
wd)
765 sl = Scale(frame, from_=0, to=10, resolution=0.01,
766 label=
"Scale Factor", command=self.
on_scale,
767 variable=self.
scale_var, orient=HORIZONTAL)
768 bt = Button(frame, text=
"Reset Scale", command=self.
reset_scale)
769 sl.pack(side=TOP, fill=X)
770 bt.pack(side=TOP, fill=X)
772 sl = Scale(frame, from_=0.001, to=1, resolution=0.001,
776 sl.pack(side=TOP, fill=X)
777 bt.pack(side=TOP, fill=X)
818 axis = Checkbutton(frame, text=
"Axis",
819 onvalue=
"on", offvalue=
"off",
820 justify=LEFT, anchor=W,
823 grid = Checkbutton(frame, text=
"Grid",
824 onvalue=
"on", offvalue=
"off",
825 justify=LEFT, anchor=W,
828 rname = Checkbutton(frame, text=
"Robots' name",
829 onvalue=
"on", offvalue=
"off",
830 justify=LEFT, anchor=W,
833 for w
in [axis, grid, rname]:
834 w.pack(side=TOP, anchor=W, fill=X)
839 for r
in list(self.
rnames.keys()):
851 lb = Label(frame, text=
"Robot Type", anchor=W, justify=LEFT)
853 om = OptionMenu(frame, label=
"Type: ", variable=self.
robot_kind_var)
855 om.add_command(opt, label=opt)
858 creater = Button(frame, text=
"Create", command=self.
create_robot)
859 deleter = Button(frame, text=
"Delete", command=self.
delete_robot)
861 om.pack(side=TOP, fill=X)
862 creater.pack(side=TOP, fill=X)
863 deleter.pack(side=TOP, fill=X)
868 f = Frame(frame, width=self.
wd, height=200)
869 f.pack(side=TOP, fill=BOTH, expand=1)
870 ys = Scrollbar(f, orient = VERTICAL)
871 ys.grid(row = 0, column=1, sticky = N+S)
872 xs = Scrollbar(f, orient = HORIZONTAL)
873 xs.grid(row = 1, column=0, sticky = E+W)
876 xscrollcommand = xs.set,
877 yscrollcommand = ys.set,
878 selectmode =
'single',
881 self.
rlistbox.grid(row = 0, column = 0, sticky = N+S+E+W,)
889 index = self.
rlistbox.curselection()
891 robot_name = self.
rlistbox.get(index[0])
892 self.
robots[robot_name].property_page()
905 self.
robots[robot.get_name()] = robot
910 index = self.
rlistbox.curselection()
912 robot_name = self.
rlistbox.get(index[0])
913 r = self.
rnames.pop(robot_name)
915 r = self.
robots.pop(robot_name)
931 x_tmp = (math.cos(dt) * x - math.sin(dt) * y + dx)/self.
scale
932 y_tmp = (math.sin(dt) * x + math.cos(dt) * y + dy)/self.
scale
935 yo = -y_tmp + self.
y0
941 manager.registerFactory(profile,
942 TkMobileRobotSimulator,
949 m.master.title(
"Tk Mobile Robot Simulator")
950 mgr = OpenRTM_aist.Manager.init(sys.argv)
951 mgr.activateManager()
953 mgr.registerFactory(profile, TkMobileRobotSimulator, OpenRTM_aist.Delete)
957 if __name__ ==
'__main__':