-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMotion.lua
More file actions
267 lines (213 loc) · 8.63 KB
/
Motion.lua
File metadata and controls
267 lines (213 loc) · 8.63 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
-- Generate a sample from a Gaussian distribution
function gaussian (mean, variance)
return math.sqrt(-2 * variance * math.log(math.random())) *
math.cos(2 * math.pi * math.random()) + mean
end
-- Return robot to a location
function resetBase(handle, matrix)
allModelObjects = sim.getObjectsInTree(handle) -- get all objects in the model
sim.setThreadAutomaticSwitch(false)
for i=1,#allModelObjects,1 do
sim.resetDynamicObject(allModelObjects[i]) -- reset all objects in the model
end
sim.setObjectMatrix(handle,-1,matrix)
sim.setThreadAutomaticSwitch(true)
end
function createRandomBumpyFloor()
print ("Generating new random bumpy floor.")
sim.setThreadAutomaticSwitch(false)
-- Remove existing bumpy floor if there already is one
if (heightField ~= nil) then
sim.setObjectPosition(heightField, heightField, {0.05, 0, 0})
return
end
-- Create random bumpy floor for robot to drive on
floorSize = 5
--heightFieldResolution = 0.3
--heightFieldNoise = 0.00000005
heightFieldResolution = 0.1
heightFieldNoise = 0.0000008
cellsPerSide = floorSize / heightFieldResolution
cellHeights = {}
for i=1,cellsPerSide*cellsPerSide,1 do
table.insert(cellHeights, gaussian(0, heightFieldNoise))
end
heightField=sim.createHeightfieldShape(0, 0, cellsPerSide, cellsPerSide, floorSize, cellHeights)
-- Make the floor invisible
--sim.setObjectInt32Param(heightField,10,0)
sim.setThreadAutomaticSwitch(true)
end
-- This function is executed exactly once when the scene is initialised
function sysCall_init()
tt = sim.getSimulationTime()
print("Init hello", tt)
robotBase=sim.getObjectHandle(sim.handle_self) -- robot handle
leftMotor=sim.getObjectHandle("leftMotor") -- Handle of the left motor
rightMotor=sim.getObjectHandle("rightMotor") -- Handle of the right motor
turretMotor=sim.getObjectHandle("turretMotor") -- Handle of the turret motor
turretSensor=sim.getObjectHandle("turretSensor")
-- We only update graphs every few steps because the simulation slows down otherwise
UPDATE_GRAPHS_EVERY = 20
graphSteps = 0
motorGraph=sim.getObjectHandle("motorGraph")
trajectoryGraph=sim.getObjectHandle("trajectoryGraph")
-- Create bumpy floor for robot to drive on
createRandomBumpyFloor()
-- Save robot start position so we can return it there later
robotStartMatrix=sim.getObjectMatrix(robotBase,-1)
-- Usual rotation rate for wheels (radians per second)
speedBase = 4.5
-- Which step are we in?
-- 0 is a dummy value which is immediately completed
stepCounter = 0
stepList = {}
stepList[1] = {"forward", 1.00}
stepList[2] = {"stop"}
stepList[3] = {"turn", math.rad(90)}
stepList[4] = {"stop"}
stepList[5] = {"forward", 1.00}
stepList[6] = {"stop"}
stepList[7] = {"turn", math.rad(90)}
stepList[8] = {"stop"}
stepList[9] = {"forward", 1.00}
stepList[10] = {"stop"}
stepList[11] = {"turn", math.rad(90)}
stepList[12] = {"stop"}
stepList[13] = {"forward", 1.00}
stepList[14] = {"stop"}
stepList[15] = {"turn", math.rad(90)}
stepList[16] = {"stop"}
stepList[17] = {"repeat"}
-- Target positions for joints
motorAngleTargetL = 0.0
motorAngleTargetR = 0.0
-- To calibrate
motorAnglePerMetre = 24.9
motorAnglePerRadian = 3.08
-- History and analysis
expCounter = 0
finalPostionsList = {}
meanX = 0
meanY = 0
varX = 0
varY = 0
covXY = 0
end
function sysCall_sensing()
end
function isCurrentTargetAchieved(posL, posR)
if (stepList[stepCounter][1] == "stop") then
-- Check to see if the robot is stationary to within a small threshold
linearVelocity,angularVelocity=sim.getVelocity(robotBase)
vLin = math.sqrt(linearVelocity[1]^2 + linearVelocity[2]^2 + linearVelocity[3]^2)
vAng = math.sqrt(angularVelocity[1]^2 + angularVelocity[2]^2 + angularVelocity[3]^2)
--print ("stop", linearVelocity, vLin, vAng)
if (vLin < 0.001 and vAng < 0.01) then
return true
else
return false
end
end
-- Start with returnVal = true and negate it if any parts of target are not reached
returnVal = true
if (speedBaseL > 0 and posL < motorAngleTargetL) then
returnVal = false
end
if (speedBaseL < 0 and posL > motorAngleTargetL) then
returnVal = false
end
if (speedBaseR > 0 and posR < motorAngleTargetR) then
returnVal = false
end
if (speedBaseR < 0 and posR > motorAngleTargetR) then
returnVal = false
end
return returnVal
end
function sysCall_actuation()
tt = sim.getSimulationTime()
-- print("actuation hello", tt)
-- Get and plot current angles of motor joints
posL = sim.getJointPosition(leftMotor)
posR = sim.getJointPosition(rightMotor)
if graphSteps % UPDATE_GRAPHS_EVERY == 0 then
sim.setGraphUserData(motorGraph,"leftPos",posL)
sim.setGraphUserData(motorGraph,"rightPos",posR)
sim.handleGraph(sim.handle_all, tt+sim.getSimulationTimeStep())
end
graphSteps = graphSteps + 1
-- If we have got to the target of this step: move to the next step
if (stepCounter == 0 or isCurrentTargetAchieved(posL, posR)) then
stepCounter = stepCounter + 1
print("Starting step", stepCounter)
newStepType = stepList[stepCounter][1]
newStepAmount = stepList[stepCounter][2]
print("Step type", newStepType, "Step amount", newStepAmount)
if (newStepType == "forward") then
-- Forward step: set new joint targets
motorAngleTargetL = posL + newStepAmount * motorAnglePerMetre
motorAngleTargetR = posR + newStepAmount * motorAnglePerMetre
speedBaseL = speedBase
speedBaseR = speedBase
elseif (newStepType == "turn") then
-- Turn step: set new targets
motorAngleTargetL = posL - newStepAmount * motorAnglePerRadian
motorAngleTargetR = posR + newStepAmount * motorAnglePerRadian
speedBaseL = -speedBase
speedBaseR = speedBase
elseif (newStepType == "stop") then
speedBaseL = 0
speedBaseR = 0
elseif (newStepType == "repeat") then
-- Count the experiment as completed
expCounter = expCounter + 1
print("EXPERIMENT ROUND", expCounter)
-- Drop a `dummy' as a marker of the robot's final position
newDummy = sim.createDummy(0.05)
linearPosition = sim.getObjectPosition(robotBase,-1)
sim.setObjectPosition(newDummy, -1, {linearPosition[1],linearPosition[2],0.0})
-- Save the final position
finalPostionsList[expCounter] = linearPosition
-- Teleport robot back to the origin
-- Just for the purposes of our experiment where we want the robot
-- to repeat the same motion multiple times
resetBase(robotBase, robotStartMatrix)
-- Regenerate new bumpy floor for next run
createRandomBumpyFloor()
stepCounter = 0
graphSteps = 0
if (expCounter == 10) then
-- Compute the covariance matrix
for i=1,expCounter,1
do
meanX = meanX + finalPostionsList[i][1]
meanY = meanX + finalPostionsList[i][2]
end
meanX = meanX / expCounter
meanY = meanY / expCounter
for i=1,expCounter,1
do
varX = varX + (finalPostionsList[i][1]-meanX)^2
varY = varY + (finalPostionsList[i][2]-meanY)^2
covXY = covXY + (finalPostionsList[i][1]-meanX)*(finalPostionsList[i][2]-meanY)
end
varX = varX / expCounter
varY = varY / expCounter
covXY = covXY / expCounter
covMat = {{varX, covXY}, {covXY, varY}}
print("Means:")
print({meanX, meanY})
print("Cov Mat:")
print(covMat)
-- Plot the graph
sim.pauseSimulation()
end
end
end
-- Set the motor velocities for the current step
sim.setJointTargetVelocity(leftMotor,speedBaseL)
sim.setJointTargetVelocity(rightMotor,speedBaseR)
end
function sysCall_cleanup()
--simUI.destroy(ui)
end