forked from yominx/Capstone1_2021Spring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua_script_ball.txt
108 lines (86 loc) · 3.39 KB
/
lua_script_ball.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
function callback_cmdvel_sub(msg)
local Vl = msg.linear.x
local Vr = msg.angular.z
local Vcollector = msg.angular.x
sim.setJointMaxForce(joint11, 3.1)
sim.setJointMaxForce(joint21, 3.1)
sim.setJointMaxForce(joint12, 3.1)
sim.setJointMaxForce(joint22, 3.1)
sim.setJointMaxForce(joint13, 3.1)
sim.setJointMaxForce(joint23, 3.1)
sim.setJointTargetVelocity(joint11,Vl - 1.83*Vr)
sim.setJointTargetVelocity(joint21,Vl + 1.83*Vr)
sim.setJointTargetVelocity(joint12,Vl - 1.5*Vr)
sim.setJointTargetVelocity(joint22,Vl + 1.5*Vr)
sim.setJointTargetVelocity(joint13,Vl - 1.83*Vr)
sim.setJointTargetVelocity(joint23,Vl + 1.83*Vr)
if(Vcollector~=0) then
sim.setJointTargetVelocity(joint00,Vcollector)
end
sim.addStatusbarMessage(string.format("Vl:%f Vr:%f",Vl, Vr))
end
function callback_ball_delivery_sub(msg)
local velocity=7
local velocity2=50
local th1=1
local thdown=2
local delivery_mode=msg.data
angle=sim.getJointPosition(joint00)
sim.addStatusbarMessage(string.format("delivery/ %f count %f angle %f", delivery_mode, count, angle))
if (delivery_mode==1)then
if (count<=80) then
if(math.abs(angle)>th1) then
sim.setJointTargetVelocity(joint00, -velocity)
elseif ( math.abs(angle)>0.1 ) then
sim.setJointTargetVelocity(joint00, -velocity*(math.abs(angle)+0.1)/(th1+0.1))
elseif ( math.abs(angle)<0.1 ) then
count=count+1
sim.setJointTargetVelocity(joint00,0)
end
end
if( count > 80 ) then
if ( math.abs(angle)<th1) then
sim.setJointTargetVelocity(joint00, velocity*(math.abs(angle))/(th1))
elseif ( math.abs(angle)>th1) then
sim.setJointTargetVelocity(joint00, velocity*(thdown-math.abs(angle))/(thdown-th1))
end
end
elseif delivery_mode==2 then
if(count2<100) then
sim.setJointTargetVelocity(joint01, -velocity2)
end
if(count2>=100) then
sim.setJointTargetVelocity(joint02, -velocity2)
end
count2=count2+1
elseif delivery_mode==0 then
sim.setJointTargetVelocity(joint00, 10)
count=0
end
end
function sysCall_init()
-- do some initialization here
joint00 = sim.getObjectHandle("collector_joint")
joint01 = sim.getObjectHandle("door_joint_l")
joint02 = sim.getObjectHandle("door_joint_r")
joint11 = sim.getObjectHandle("11_joint")
joint21 = sim.getObjectHandle("21_joint")
joint12 = sim.getObjectHandle("12_joint")
joint22 = sim.getObjectHandle("22_joint")
joint13 = sim.getObjectHandle("13_joint")
joint23 = sim.getObjectHandle("23_joint")
cmdvelSub=simROS.subscribe('/command_vel','geometry_msgs/Twist','callback_cmdvel_sub')
count=0
count2=0
delvelSub=simROS.subscribe('/ball_delivery','std_msgs/Int8','callback_ball_delivery_sub')
transform={header={stamp=simROS.getTime(), frame_id='map'}, child_frame_id='velodyneVPL', transform={translation={x=0, y=0, z=0}, rotation={x=0, y=0, z=0, w=1}}}
simROS.sendTransform(transform)
end
function sysCall_actuation()
-- put your actuation code here
end
function sysCall_cleanup()
-- do some clean-up here
simROS.shutdownSubscriber(cmdvelSub)
simROS.shutdownSubscriber(delvelSub)
end