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

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

from director.componentgraph import ComponentFactory 

from director.fieldcontainer import FieldContainer 

 

 

class RobotSystemFactory(ComponentFactory): 

 

    def initDefaultOptions(self, options): 

        ''' 

        Components are enabled by default.  This function 

        determines which components should be disabled. 

        ''' 

        options.useConvexHullModel = False 

        options.useRobotLinkSelector = False 

 

    def addComponents(self, componentGraph): 

 

        addComponent = componentGraph.addComponent 

 

        addComponent('DirectorConfig', []) 

        addComponent('RobotState', ['DirectorConfig']) 

        addComponent('Segmentation', []) 

        addComponent('SegmentationRobotState', ['Segmentation', 'RobotState']) 

        addComponent('SegmentationAffordances', ['Segmentation', 'Affordances']) 

        addComponent('PerceptionDrivers', ['RobotState', 'Planning']) 

        addComponent('HandDrivers', []) 

        addComponent('Footsteps', ['RobotState']) 

        addComponent('RaycastDriver', ['Footsteps']) 

        addComponent('IRISDriver', ['RobotState', 'Footsteps']) 

        addComponent('AtlasDriver', []) 

        addComponent('Planning', ['RobotState']) 

        addComponent('Playback', ['Planning']) 

        addComponent('Teleop', ['Planning', 'Playback', 'Affordances']) 

        addComponent('ConvexHullModel', ['Playback']) 

        addComponent('FootstepsPlayback', ['Footsteps', 'Playback']) 

        addComponent('Affordances', []) 

        addComponent('PlannerPublisher', ['Planning', 'Affordances']) 

        addComponent('ViewBehaviors', ['Footsteps', 'PerceptionDrivers', 'Planning', 'Affordances']) 

        addComponent('RobotLinkSelector', ['ViewBehaviors']) 

 

    def initDirectorConfig(self, robotSystem): 

 

        from director import drcargs 

 

        directorConfig = drcargs.getDirectorConfig() 

 

        if 'colorMode' not in directorConfig: 

            defaultColorMode = 'URDF Colors' 

            directorConfig['colorMode'] = defaultColorMode 

 

        assert directorConfig['colorMode'] in ['URDF Colors', 'Solid Color', 'Textures'] 

 

        return FieldContainer(directorConfig=directorConfig) 

 

    def initRobotState(self, robotSystem): 

 

        from director import roboturdf 

 

        robotStateModel, robotStateJointController = roboturdf.loadRobotModel( 

            'robot state model', 

            robotSystem.view, 

            urdfFile=robotSystem.directorConfig['urdfConfig']['robotState'], 

            color=roboturdf.getRobotGrayColor(), 

            colorMode=robotSystem.directorConfig['colorMode'], 

            parent='sensors', 

            visible=True) 

 

        robotStateJointController.setPose('EST_ROBOT_STATE', robotStateJointController.getPose('q_nom')) 

        roboturdf.startModelPublisherListener([(robotStateModel, robotStateJointController)]) 

        robotStateJointController.addLCMUpdater('EST_ROBOT_STATE') 

 

        return FieldContainer(robotStateModel=robotStateModel, 

                                robotStateJointController=robotStateJointController) 

 

    def initSegmentation(self, robotSystem): 

        from director import segmentation 

 

    def initSegmentationRobotState(self, robotSystem): 

 

        from director import segmentationroutines 

        segmentationroutines.SegmentationContext.initWithRobot(robotSystem.robotStateModel) 

 

    def initPerceptionDrivers(self, robotSystem): 

 

        from director import perception 

        from director import robotstate 

 

        multisenseDriver, mapServerSource = perception.init(robotSystem.view) 

 

        useNeckDriver = hasattr(robotSystem.ikPlanner, 'neckPitchJoint') 

        if useNeckDriver: 

            neckPitchJoint = robotSystem.ikPlanner.neckPitchJoint 

            neckPitchIndex = robotstate.getDrakePoseJointNames().index(neckPitchJoint) 

 

            def getNeckPitch(): 

                return robotSystem.robotStateJointController.q[neckPitchIndex] 

 

            neckDriver = perception.NeckDriver(robotSystem.view, getNeckPitch) 

        else: 

            neckDriver = None 

 

 

        spindleJoint = 'hokuyo_joint' 

 

        def getSpindleAngleFunction(): 

            msg = robotSystem.robotStateJointController.lastRobotStateMessage 

            if msg and spindleJoint in msg.joint_name: 

                index = msg.joint_name.index(spindleJoint) 

                return (float(msg.utime)/(1e6), msg.joint_position[index]) 

            else: 

                return (0, 0) 

 

        spindleMonitor = perception.SpindleMonitor(getSpindleAngleFunction) 

        robotSystem.robotStateModel.connectModelChanged(spindleMonitor.onRobotStateChanged) 

 

        return FieldContainer(multisenseDriver=multisenseDriver, 

                                mapServerSource=mapServerSource, 

                                neckDriver=neckDriver, 

                                spindleMonitor=spindleMonitor) 

 

    def initHandDrivers(self, robotSystem): 

 

        from director import handdriver 

 

        rHandDriver = handdriver.RobotiqHandDriver(side='right') 

        lHandDriver = handdriver.RobotiqHandDriver(side='left') 

        return FieldContainer(rHandDriver=rHandDriver, lHandDriver=lHandDriver) 

 

    def initFootsteps(self, robotSystem): 

 

        from director import footstepsdriver 

        footstepsDriver = footstepsdriver.FootstepsDriver(robotSystem.robotStateJointController) 

        return FieldContainer(footstepsDriver=footstepsDriver) 

 

    def initRaycastDriver(self, robotSystem): 

 

        from director import raycastdriver 

        raycastDriver = raycastdriver.RaycastDriver() 

        return FieldContainer(raycastDriver=raycastDriver) 

 

    def initIRISDriver(self, robotSystem): 

 

        from director import irisdriver 

 

        irisDriver = irisdriver.IRISDriver(robotSystem.robotStateJointController, robotSystem.footstepsDriver.params) 

        return FieldContainer(irisDriver=irisDriver) 

 

    def initAtlasDriver(self, robotSystem): 

 

        from director import atlasdriver 

 

        atlasDriver = atlasdriver.init(None) 

        return FieldContainer(atlasDriver=atlasDriver) 

 

    def initPlanning(self, robotSystem): 

 

        from director import objectmodel as om 

        from director import planningutils 

        from director import roboturdf 

        from director import ikplanner 

 

 

        directorConfig = robotSystem.directorConfig 

 

        ikRobotModel, ikJointController = roboturdf.loadRobotModel('ik model', urdfFile=directorConfig['urdfConfig']['ik'], parent=None) 

        om.removeFromObjectModel(ikRobotModel) 

        ikJointController.addPose('q_end', ikJointController.getPose('q_nom')) 

        ikJointController.addPose('q_start', ikJointController.getPose('q_nom')) 

 

        handFactory = roboturdf.HandFactory(robotSystem.robotStateModel) 

        handModels = [] 

 

        for side in ['left', 'right']: 

            if side in handFactory.defaultHandTypes: 

                handModels.append(handFactory.getLoader(side)) 

 

        ikPlanner = ikplanner.IKPlanner(ikRobotModel, ikJointController, handModels) 

 

        planningUtils = planningutils.PlanningUtils(robotSystem.robotStateModel, robotSystem.robotStateJointController) 

 

        return FieldContainer( 

            ikRobotModel=ikRobotModel, 

            ikJointController=ikJointController, 

            handFactory=handFactory, 

            handModels=handModels, 

            ikPlanner=ikPlanner, 

            planningUtils=planningUtils 

            ) 

 

    def initConvexHullModel(self, robotSystem): 

 

        from director import roboturdf 

 

        directorConfig = robotSystem.directorConfig 

        chullRobotModel, chullJointController = roboturdf.loadRobotModel('convex hull model', view, urdfFile=directorConfig['urdfConfig']['chull'], parent='planning', color=roboturdf.getRobotOrangeColor(), colorMode=directorConfig['colorMode'], visible=False) 

 

        robotSystem.playbackJointController.models.append(chullRobotModel) 

 

        return FieldContainer( 

            chullRobotModel=chullRobotModel, 

            chullJointController=chullJointController 

            ) 

 

    def initPlayback(self, robotSystem): 

 

        from director import roboturdf 

        from director import planplayback 

        from director import playbackpanel 

        from director import robotplanlistener 

 

        directorConfig = robotSystem.directorConfig 

 

        manipPlanner = robotplanlistener.ManipulationPlanDriver(robotSystem.ikPlanner) 

 

        playbackRobotModel, playbackJointController = roboturdf.loadRobotModel('playback model', robotSystem.view, urdfFile=directorConfig['urdfConfig']['playback'], parent='planning', color=roboturdf.getRobotOrangeColor(), visible=False, colorMode=directorConfig['colorMode']) 

 

        planPlayback = planplayback.PlanPlayback() 

 

        playbackPanel = playbackpanel.PlaybackPanel(planPlayback, playbackRobotModel, playbackJointController, 

                                          robotSystem.robotStateModel, robotSystem.robotStateJointController, manipPlanner) 

 

        manipPlanner.connectPlanReceived(playbackPanel.setPlan) 

 

 

        return FieldContainer( 

            playbackRobotModel=playbackRobotModel, 

            playbackJointController=playbackJointController, 

            planPlayback=planPlayback, 

            manipPlanner=manipPlanner, 

            playbackPanel=playbackPanel 

            ) 

 

    def initTeleop(self, robotSystem): 

 

        from director import roboturdf 

        from director import teleoppanel 

 

        directorConfig = robotSystem.directorConfig 

 

        teleopRobotModel, teleopJointController = roboturdf.loadRobotModel('teleop model', robotSystem.view, urdfFile=directorConfig['urdfConfig']['teleop'], parent='planning', color=roboturdf.getRobotBlueColor(), visible=False, colorMode=directorConfig['colorMode']) 

 

 

        teleopPanel = teleoppanel.TeleopPanel(robotSystem.robotStateModel, robotSystem.robotStateJointController, teleopRobotModel, teleopJointController, 

                          robotSystem.ikPlanner, robotSystem.manipPlanner, robotSystem.affordanceManager, robotSystem.playbackPanel.setPlan, robotSystem.playbackPanel.hidePlan, robotSystem.planningUtils) 

 

        return FieldContainer( 

            teleopRobotModel=teleopRobotModel, 

            teleopJointController=teleopJointController, 

            teleopPanel=teleopPanel, 

            ) 

 

    def initFootstepsPlayback(self, robotSystem): 

        robotSystem.footstepsDriver.walkingPlanCallback = robotSystem.playbackPanel.setPlan 

 

    def initAffordances(self, robotSystem): 

 

        from director import affordancemanager 

        from director import affordanceitems 

 

        affordanceManager = affordancemanager.AffordanceObjectModelManager(robotSystem.view) 

        affordanceitems.MeshAffordanceItem.getMeshManager() 

 

        if affordancemanager.lcmobjectcollection.USE_LCM: 

            affordanceitems.MeshAffordanceItem.getMeshManager().collection.sendEchoRequest() 

            affordanceManager.collection.sendEchoRequest() 

 

        return FieldContainer( 

            affordanceManager=affordanceManager, 

            ) 

 

    def initSegmentationAffordances(self, robotSystem): 

 

        from director import segmentation 

        segmentation.affordanceManager = robotSystem.affordanceManager 

 

    def initPlannerPublisher(self, robotSystem): 

 

        from director import plannerPublisher 

        from director import pydrakeik 

        from director import matlabik 

 

        dummyPlannerPub = plannerPublisher.DummyPlannerPublisher(robotSystem.ikPlanner, robotSystem.affordanceManager) 

        pyDrakePlannerPub = pydrakeik.PyDrakePlannerPublisher(robotSystem.ikPlanner, robotSystem.affordanceManager) 

        exoticaPlannerPub = plannerPublisher.ExoticaPlannerPublisher(robotSystem.ikPlanner, robotSystem.affordanceManager) 

        matlabPlannerPub = plannerPublisher.MatlabDrakePlannerPublisher(robotSystem.ikPlanner, robotSystem.affordanceManager) 

 

        robotSystem.ikPlanner.addPublisher('dummy', dummyPlannerPub) 

        robotSystem.ikPlanner.addPublisher('pydrake', pyDrakePlannerPub) 

        robotSystem.ikPlanner.addPublisher('matlabdrake', matlabPlannerPub) 

        robotSystem.ikPlanner.addPublisher('exotica', exoticaPlannerPub) 

 

        directorConfig = robotSystem.directorConfig 

        if 'planningMode' in directorConfig: 

            robotSystem.ikPlanner.planningMode = directorConfig['planningMode'] 

        else: 

            robotSystem.ikPlanner.planningMode = 'matlabdrake' 

 

 

        linkNameArgs = ['','',''] 

        if 'leftFootLink' in directorConfig: 

            linkNameArgs = [directorConfig['leftFootLink'], directorConfig['rightFootLink'], directorConfig['pelvisLink']] 

 

        matlabIkServer = matlabik.AsyncIKCommunicator(directorConfig['urdfConfig']['ik'], directorConfig['fixedPointFile'], *linkNameArgs) 

 

        def startIkServer(): 

            matlabIkServer.startServerAsync() 

            matlabIkServer.comm.writeCommandsToLogFile = True 

 

        matlabIkServer.handModels = robotSystem.ikPlanner.handModels 

        matlabPlannerPub.ikServer = matlabIkServer 

 

        robotSystem.ikPlanner.ikServer = matlabIkServer 

 

        return FieldContainer( 

            ikServer=matlabIkServer, 

            startIkServer=startIkServer 

            ) 

 

    def initRobotLinkSelector(self, robotSystem): 

 

        from director import robotlinkselector 

        robotLinkSelector = robotlinkselector.RobotLinkSelector() 

        robotSystem.viewBehaviors.addHandler( 

            robotSystem.viewBehaviors.LEFT_DOUBLE_CLICK_EVENT, 

            robotLinkSelector.onLeftDoubleClick) 

        return FieldContainer(robotLinkSelector=robotLinkSelector) 

 

    def initViewBehaviors(self, robotSystem): 

 

        from director import applogic 

        from director import robotviewbehaviors 

 

        viewBehaviors = robotviewbehaviors.RobotViewBehaviors(robotSystem.view, robotSystem) 

        applogic.resetCamera(viewDirection=[-1,0,0], view=robotSystem.view) 

        return FieldContainer(viewBehaviors=viewBehaviors) 

 

 

 

def create(view=None, globalsDict=None, options=None): 

    ''' 

    Convenience function for initializing a robotSystem 

    with the default options and populating a globals() 

    dictionary with all the constructed objects. 

    ''' 

 

    from director import applogic 

 

    view = view or applogic.getCurrentRenderView() 

 

    factory = RobotSystemFactory() 

    options = options or factory.getDefaultOptions() 

    robotSystem = factory.construct(options, view=view) 

 

    if globalsDict is not None: 

        globalsDict.update(dict(robotSystem)) 

 

    return robotSystem