|
| 1 | +import vrep |
| 2 | + |
| 3 | +class Tank: |
| 4 | + def __init__(self, ID): |
| 5 | + self.clientID = ID |
| 6 | + # get handles to robot drivers |
| 7 | + err_code, self.left_front_handle = vrep.simxGetObjectHandle(self.clientID,'left_front', vrep.simx_opmode_blocking) |
| 8 | + err_code, self.left_back_handle = vrep.simxGetObjectHandle(self.clientID,'left_back', vrep.simx_opmode_blocking) |
| 9 | + err_code, self.right_back_handle = vrep.simxGetObjectHandle(self.clientID,'right_back', vrep.simx_opmode_blocking) |
| 10 | + err_code, self.right_front_handle= vrep.simxGetObjectHandle(self.clientID,'right_front', vrep.simx_opmode_blocking) |
| 11 | + |
| 12 | + self.side_handles=[] |
| 13 | + for l in 'rl': |
| 14 | + for i in range(1,7): |
| 15 | + err_code, handle= vrep.simxGetObjectHandle(self.clientID,'sj_'+l+'_'+str(i) , vrep.simx_opmode_blocking) |
| 16 | + self.side_handles.append(handle) |
| 17 | + |
| 18 | + #initial velocity |
| 19 | + self.leftvelocity=0 |
| 20 | + self.rightvelocity=0 |
| 21 | + self.MaxVel=10 |
| 22 | + self.dVel=1 |
| 23 | + |
| 24 | + def stop(self): |
| 25 | + #set divers to stop mode |
| 26 | + force =0 |
| 27 | + err_code = vrep.simxSetJointForce(self.clientID, self.left_front_handle, force, vrep.simx_opmode_oneshot) |
| 28 | + err_code = vrep.simxSetJointForce(self.clientID, self.left_back_handle, force, vrep.simx_opmode_oneshot) |
| 29 | + err_code = vrep.simxSetJointForce(self.clientID, self.right_back_handle, force, vrep.simx_opmode_oneshot) |
| 30 | + err_code = vrep.simxSetJointForce(self.clientID, self.right_front_handle, force, vrep.simx_opmode_oneshot) |
| 31 | + |
| 32 | + force =10 |
| 33 | + for h in self.side_handles: |
| 34 | + err_code = vrep.simxSetJointForce(self.clientID, h, force, vrep.simx_opmode_oneshot) |
| 35 | + |
| 36 | + #break |
| 37 | + self.leftvelocity=10 |
| 38 | + self.rightvelocity=10 |
| 39 | + vrep.simxSetJointTargetVelocity(self.clientID,self.left_front_handle,self.leftvelocity,vrep.simx_opmode_streaming) |
| 40 | + vrep.simxSetJointTargetVelocity(self.clientID,self.left_back_handle,self.leftvelocity,vrep.simx_opmode_streaming) |
| 41 | + vrep.simxSetJointTargetVelocity(self.clientID,self.right_back_handle,self.rightvelocity,vrep.simx_opmode_streaming) |
| 42 | + vrep.simxSetJointTargetVelocity(self.clientID,self.right_front_handle,self.rightvelocity,vrep.simx_opmode_streaming) |
| 43 | + |
| 44 | + def go(self): |
| 45 | + #set divers to go mode |
| 46 | + force =10 |
| 47 | + err_code = vrep.simxSetJointForce(self.clientID, self.left_front_handle, force, vrep.simx_opmode_oneshot) |
| 48 | + err_code = vrep.simxSetJointForce(self.clientID, self.left_back_handle, force, vrep.simx_opmode_oneshot) |
| 49 | + err_code = vrep.simxSetJointForce(self.clientID, self.right_back_handle, force, vrep.simx_opmode_oneshot) |
| 50 | + err_code = vrep.simxSetJointForce(self.clientID, self.right_front_handle, force, vrep.simx_opmode_oneshot) |
| 51 | + |
| 52 | + force =0 |
| 53 | + for h in self.side_handles: |
| 54 | + err_code = vrep.simxSetJointForce(self.clientID, h, force, vrep.simx_opmode_oneshot) |
| 55 | + |
| 56 | + def setVelocity(self): |
| 57 | + #verify if the velocity is in correct range |
| 58 | + if self.leftvelocity > self.MaxVel: |
| 59 | + self.leftvelocity = self.MaxVel |
| 60 | + if self.leftvelocity < -self.MaxVel: |
| 61 | + self.leftvelocity = -self.MaxVel |
| 62 | + if self.rightvelocity > self.MaxVel: |
| 63 | + self.rightvelocity = self.MaxVel |
| 64 | + if self.rightvelocity < -self.MaxVel: |
| 65 | + self.rightvelocity = -self.MaxVel |
| 66 | + |
| 67 | + #send value of velocity to drivers |
| 68 | + #vrep.simxSetJointTargetVelocity(clientID,left_front_handle,leftvelocity,vrep.simx_opmode_streaming) |
| 69 | + vrep.simxSetJointTargetVelocity(self.clientID,self.left_back_handle,self.leftvelocity,vrep.simx_opmode_streaming) |
| 70 | + vrep.simxSetJointTargetVelocity(self.clientID,self.right_back_handle,self.rightvelocity,vrep.simx_opmode_streaming) |
| 71 | + #vrep.simxSetJointTargetVelocity(clientID,right_front_handle,rightvelocity,vrep.simx_opmode_streaming) |
| 72 | + |
| 73 | + #Move the tank forward |
| 74 | + #None - increases velocity by 1, if velocities of wheels are different they are equalized |
| 75 | + #velocity - takes values from <-10,10> and sets them as velocity for both wheels in forward direction |
| 76 | + def forward(self, velocity=None): |
| 77 | + self.go() |
| 78 | + if velocity!=None: |
| 79 | + self.leftvelocity=velocity |
| 80 | + self.rightvelocity=velocity |
| 81 | + else: |
| 82 | + self.rightvelocity=self.leftvelocity=(self.leftvelocity+self.rightvelocity)/2 |
| 83 | + self.leftvelocity+=self.dVel |
| 84 | + self.rightvelocity+=self.dVel |
| 85 | + self.setVelocity() |
| 86 | + |
| 87 | + #Move the tank backward |
| 88 | + #None - decreases velocity by 1, if velocities of wheels are different they are equalized |
| 89 | + #velocity - takes values from <-10,10> and sets them as velocity for both wheels in backward direction |
| 90 | + def backward(self, velocity=None): |
| 91 | + self.go() |
| 92 | + if velocity!=None: |
| 93 | + self.leftvelocity=-velocity |
| 94 | + self.rightvelocity=-velocity |
| 95 | + else: |
| 96 | + self.rightvelocity=self.leftvelocity=(self.leftvelocity+self.rightvelocity)/2 |
| 97 | + self.leftvelocity-=self.dVel |
| 98 | + self.rightvelocity-=self.dVel |
| 99 | + self.setVelocity() |
| 100 | + |
| 101 | + #Turns left the tank |
| 102 | + #None - increases velocity of rightwheel by 1, decreases velocity of leftwheel by 1 |
| 103 | + #velocity - takes values from <-10,10> and sets it as velocity for right wheel |
| 104 | + #in forward direction and oposite value of velocity for left wheel in backward direction |
| 105 | + def turn_left(self, velocity=None): |
| 106 | + self.go() |
| 107 | + if velocity!=None: |
| 108 | + self.leftvelocity =-velocity |
| 109 | + self.rightvelocity= velocity |
| 110 | + else: |
| 111 | + self.leftvelocity -=self.dVel |
| 112 | + self.rightvelocity+=self.dVel |
| 113 | + self.setVelocity() |
| 114 | + |
| 115 | + #Turns right the tank |
| 116 | + #None - increases velocity of leftwheel by 1, decreases velocity of rightwheel by 1 |
| 117 | + #velocity - takes values from <-10,10> and sets it as velocity for left wheel |
| 118 | + #in forward direction and oposite value of velocity for right wheel in backward direction |
| 119 | + def turn_right(self, velocity=None): |
| 120 | + self.go() |
| 121 | + if velocity!=None: |
| 122 | + self.leftvelocity = velocity |
| 123 | + self.rightvelocity=-velocity |
| 124 | + else: |
| 125 | + self.leftvelocity +=self.dVel |
| 126 | + self.rightvelocity-=self.dVel |
| 127 | + self.setVelocity() |
| 128 | + def readVelocity(self): |
| 129 | + velocity = (self.rightvelocity+self.leftvelocity)/2 |
| 130 | + return velocity |
0 commit comments