yarp-devices
Loading...
Searching...
No Matches
exampleRemoteControlBoard.py
1
36
37
38
39import yarp # imports YARP
40yarp.Network.init() # connect to YARP network
41if not yarp.Network.checkNetwork(): # let's see if there was actually a reachable YARP network
42 print('[error] Please try running yarp server') # tell the user to start one with 'yarp server' if there isn't any
43 quit()
44options = yarp.Property() # create an instance of Property, a nice YARP class for storing name-value (key-value) pairs
45options.put('device','remote_controlboard') # we add a name-value pair that indicates the YARP device
46options.put('remote','/robotName/partName') # we add info on to whom we will connect
47options.put('local','/exampleRemoteControlBoard') # we add info on how we will call ourselves on the YARP network
48dd = yarp.PolyDriver(options) # create a YARP multi-use driver with the given options
49if not dd.isValid():
50 print('[error] Please launch robot side')
51 quit()
52
53pos = dd.viewIPositionControl() # make a position controller object we call 'pos'
54vel = dd.viewIVelocityControl() # make a velocity controller object we call 'vel'
55posd = dd.viewIPositionDirect() # make a direct position controller object we call 'posd'
56enc = dd.viewIEncoders() # make an encoder controller object we call 'enc'
57mode = dd.viewIControlMode() # make a operation mode controller object we call 'mode'
58ll = dd.viewIControlLimits() # make a limits controller object we call 'll'
59
60axes = enc.getAxes() # retrieve number of joints
61
62lmin = yarp.DVector(1)
63lmax = yarp.DVector(1)
64ll.getLimits(0,lmin,lmax) # retrieve limits of joint 0
65print('lmin',lmin[0],'lmax',lmax[0])
66
67# use the object to set the device to position mode (as opposed to velocity mode)(note: stops the robot)
68mode.setControlModes(yarp.IVector(axes, yarp.encode('pos')))
69
70print('positionMove(1,-35) -> moves motor 1 (start count at motor 0) to -35 degrees')
71pos.positionMove(1,-35)
72
73done = False
74while not done:
75 print('wait to reach...')
76 yarp.delay(1.0) # [s]
77 done = pos.checkMotionDone()
78
79v = yarp.DVector(axes) # create a YARP vector of doubles the size of the number of elements read by enc, call it 'v'
80enc.getEncoders(v) # read the encoder values and put them into 'v'
81print('v[1] is: ' + str(v[1])) # print element 1 of 'v', note that motors and encoders start at 0
82
83targets = list(range(0,10+5*axes,5))
84print('positionMove(...) -> [multiple axes] moves motor 0 to 10 degrees, motor 1 to 15 degrees and so on')
85pos.positionMove(yarp.DVector(targets))
86
87done = False
88while not done:
89 print('wait to reach...')
90 yarp.delay(1.0) # [s]
91 done = pos.checkMotionDone()
92
93# use the object to set the device to velocity mode (as opposed to position mode)
94mode.setControlModes(yarp.IVector(axes, yarp.encode('vel')))
95
96print('velocityMove(0,10) -> moves motor 0 (start count at motor 0) at 10 degrees per second')
97vel.velocityMove(0,10)
98
99print('delay(5)')
100yarp.delay(5)
101
102vel.velocityMove(0,0) # stop the robot
103
104mode.setControlModes(yarp.IVector(axes, yarp.encode('posd')))
105home = [0] * axes
106print('positionMove(home) -> [multiple axes] moves all to 0 immediately')
107posd.setPositions(yarp.DVector(home))
108
109dd.close()
110
111yarp.Network.fini() # disconnect from the YARP network