Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

import os 

import sys 

import vtkAll as vtk 

import math 

import time 

import types 

import functools 

import numpy as np 

 

from director import transformUtils 

from director import lcmUtils 

from director.timercallback import TimerCallback 

from director.asynctaskqueue import AsyncTaskQueue 

from director import objectmodel as om 

from director import visualization as vis 

from director import applogic as app 

from director.debugVis import DebugData 

from director import ikplanner 

from director import ioUtils 

from director.simpletimer import SimpleTimer 

from director.utime import getUtime 

from director import affordanceitems 

from director import robotstate 

from director import robotplanlistener 

from director import segmentation 

from director import planplayback 

 

import drc as lcmdrc 

import copy 

 

from PythonQt import QtCore, QtGui 

 

 

 

class walkingTestDemo(object): 

 

    def __init__(self, robotModel, playbackRobotModel, teleopRobotModel, footstepPlanner, manipPlanner, ikPlanner, 

                 lhandDriver, rhandDriver, atlasDriver, multisenseDriver, sensorJointController, 

                 planPlaybackFunction, showPoseFunction): 

        self.robotModel = robotModel 

        self.playbackRobotModel = playbackRobotModel # not used inside the demo 

        self.teleopRobotModel = teleopRobotModel # not used inside the demo 

        self.footstepPlanner = footstepPlanner 

        self.manipPlanner = manipPlanner 

        self.ikPlanner = ikPlanner 

        self.lhandDriver = lhandDriver 

        self.rhandDriver = rhandDriver 

        self.atlasDriver = atlasDriver 

        self.multisenseDriver = multisenseDriver 

        self.sensorJointController = sensorJointController 

        self.planPlaybackFunction = planPlaybackFunction 

        self.showPoseFunction = showPoseFunction 

 

        self.goalTransform1 = transformUtils.frameFromPositionAndRPY([1,0,0],[0,0,0]) 

        self.goalTransform2 = transformUtils.frameFromPositionAndRPY([2,0,0],[0,0,10]) 

        #self.goalTransform2 = transformUtils.frameFromPositionAndRPY([1,1,0],[0,0,90]) 

 

        self.visOnly = False # True for development, False for operation 

        self.planFromCurrentRobotState = True # False for development, True for operation 

        useDevelopment = False 

        if (useDevelopment): 

            self.visOnly = True # True for development, False for operation 

            self.planFromCurrentRobotState = False # False for development, True for operation 

 

        self.optionalUserPromptEnabled = False 

        self.requiredUserPromptEnabled = True 

 

        self.constraintSet = None 

 

        self.plans = [] 

 

        self._setupSubscriptions() 

 

 

    def _setupSubscriptions(self): 

        sub0 = lcmUtils.addSubscriber('AUTONOMOUS_TEST_WALKING', lcmdrc.utime_t, self.autonomousTest) 

 

    def addPlan(self, plan): 

        self.plans.append(plan) 

 

 

    ### Planning Functions ############################################################### 

    def planNominal(self): 

        startPose = self.getPlanningStartPose() 

        self.standPlan = self.ikPlanner.computeNominalPlan(startPose) 

        self.addPlan(self.standPlan) 

 

    def planFootsteps(self, goalFrame): 

        startPose = self.getPlanningStartPose() 

        request = self.footstepPlanner.constructFootstepPlanRequest(startPose, goalFrame) 

        self.footstepPlan = self.footstepPlanner.sendFootstepPlanRequest(request, waitForResponse=True) 

 

    def planWalking(self): 

        startPose = self.getPlanningStartPose() 

        walkingPlan = self.footstepPlanner.sendWalkingPlanRequest(self.footstepPlan, startPose, waitForResponse=True) 

        self.addPlan(walkingPlan) 

 

 

    ########## Glue Functions #################################### 

    def moveRobotToStanceFrame(self, frame): 

        self.sensorJointController.setPose('q_nom') 

        stancePosition = frame.GetPosition() 

        stanceOrientation = frame.GetOrientation() 

 

        q = self.sensorJointController.q.copy() 

        q[:2] = [stancePosition[0], stancePosition[1]] 

        q[5] = math.radians(stanceOrientation[2]) 

        self.sensorJointController.setPose('EST_ROBOT_STATE', q) 

 

 

    def sendNeckPitchLookDown(self): 

        self.multisenseDriver.setNeckPitch(40) 

 

    def sendNeckPitchLookForward(self): 

        self.multisenseDriver.setNeckPitch(15) 

 

    def printAsync(self, s): 

        yield 

        print s 

 

    def optionalUserPrompt(self, message): 

        if not self.optionalUserPromptEnabled: 

            return 

 

        yield 

        result = raw_input(message) 

        if result != 'y': 

            raise Exception('user abort.') 

 

    def requiredUserPrompt(self, message): 

        if not self.requiredUserPromptEnabled: 

            return 

 

        yield 

        result = raw_input(message) 

        if result != 'y': 

            raise Exception('user abort.') 

 

 

    def delay(self, delayTimeInSeconds): 

        yield 

        t = SimpleTimer() 

        while t.elapsed() < delayTimeInSeconds: 

            yield 

 

 

    def getEstimatedRobotStatePose(self): 

        return self.sensorJointController.getPose('EST_ROBOT_STATE') 

 

    def getPlanningStartPose(self): 

        if self.planFromCurrentRobotState: 

            return self.getEstimatedRobotStatePose() 

        else: 

            if self.plans: 

                return robotstate.convertStateMessageToDrakePose(self.plans[-1].plan[-1]) 

            else: 

                return self.getEstimatedRobotStatePose() 

 

    def cleanupFootstepPlans(self): 

        om.removeFromObjectModel(om.findObjectByName('walking goal')) 

        om.removeFromObjectModel(om.findObjectByName('footstep plan')) 

        self.footstepPlan = None 

 

    def playSequenceNominal(self): 

        assert None not in self.plans 

        self.planPlaybackFunction(self.plans) 

 

    def commitManipPlan(self): 

        self.manipPlanner.commitManipPlan(self.plans[-1]) 

 

    def commitFootstepPlan(self): 

        self.footstepPlanner.commitFootstepPlan(self.footstepPlan) 

 

    def waitForPlanExecution(self): 

        while self.atlasDriver.getControllerStatus() != 'manipulating': 

            yield 

        while self.atlasDriver.getControllerStatus() == 'manipulating': 

            yield 

 

    def waitForWalkExecution(self): 

        while self.atlasDriver.getControllerStatus() != 'walking': 

            yield 

        while self.atlasDriver.getControllerStatus() == 'walking': 

            yield 

 

    def waitForPlanAnimation(self, plan): 

        planElapsedTime = planplayback.PlanPlayback.getPlanElapsedTime(plan) 

        print 'waiting for plan animation:', planElapsedTime 

        return self.delay(planElapsedTime) 

 

    def animateLastPlan(self): 

        plan = self.plans[-1] 

        if self.visOnly: 

            return self.waitForPlanAnimation(plan) 

        else: 

            self.commitManipPlan() 

            return self.waitForPlanExecution() 

 

 

    ######### Nominal Plans and Execution  ################################################################# 

    def planSequence(self, playbackNominal=True): 

 

        self.planFromCurrentRobotState = False 

        self.plans = [] 

 

        #self.moveRobotToStanceFrame(self.goalTransform1) 

 

        self.planFootsteps(self.goalTransform1) 

        self.planWalking() 

 

        self.planFootsteps(self.goalTransform2) 

        self.planWalking() 

 

        self.planNominal() 

 

        if (playbackNominal is True): 

            self.playSequenceNominal() 

 

 

    def autonomousTest(self, msg): 

        print "Got the autonomousTest message, executing walking test sequence" 

        q = self.autonomousExecute() 

        q.start() 

 

 

    def sendAutonmousTestDone(self): 

        msg = lcmdrc.utime_t() 

        msg.utime = getUtime() 

        lcmUtils.publish('AUTONOMOUS_TEST_WALKING_DONE', msg) 

 

 

    def autonomousExecute(self): 

 

        self.planFromCurrentRobotState = True 

        self.visOnly = False 

 

        taskQueue = AsyncTaskQueue() 

 

        taskQueue.addTask(self.printAsync('Plan and execute walking')) 

        taskQueue.addTask( functools.partial(self.planFootsteps, self.goalTransform1) ) 

        taskQueue.addTask(self.optionalUserPrompt('Send footstep plan. continue? y/n: ')) 

        taskQueue.addTask(self.commitFootstepPlan) 

        taskQueue.addTask(self.waitForWalkExecution) 

 

        taskQueue.addTask(self.printAsync('Plan and execute walking')) 

        taskQueue.addTask( functools.partial(self.planFootsteps, self.goalTransform2) ) 

        taskQueue.addTask(self.optionalUserPrompt('Send footstep plan. continue? y/n: ')) 

        taskQueue.addTask(self.commitFootstepPlan) 

        taskQueue.addTask(self.waitForWalkExecution) 

 

        taskQueue.addTask(self.sendAutonmousTestDone) 

 

        return taskQueue