-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot.py
More file actions
51 lines (44 loc) · 1.79 KB
/
robot.py
File metadata and controls
51 lines (44 loc) · 1.79 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
import pybullet as p
import pyrosim.pyrosim as pyrosim
from pyrosim.neuralNetwork import NEURAL_NETWORK
from sensor import SENSOR
from motor import MOTOR
import os
import constants as c
class ROBOT:
def __init__(self, solutionID):
self.myID = solutionID
self.robot = p.loadURDF("body.urdf")
pyrosim.Prepare_To_Simulate("body.urdf")
self.Prepare_To_Sense()
self.Prepare_To_Act()
self.nn = NEURAL_NETWORK("brain" + str(solutionID) + ".nndf")
os.system("del brain" + str(solutionID) + ".nndf")
def Prepare_To_Sense(self):
self.sensors = {}
for linkName in pyrosim.linkNamesToIndices:
self.sensors[linkName] = SENSOR(linkName)
def Sense(self, t):
for i in self.sensors:
self.sensors[i].Get_Value(t)
def Prepare_To_Act(self):
self.motors = {}
for jointName in pyrosim.jointNamesToIndices:
self.motors[jointName] = MOTOR(jointName)
def Act(self, t):
for neuronName in self.nn.Get_Neuron_Names():
if self.nn.Is_Motor_Neuron(neuronName):
jointName = self.nn.Get_Motor_Neurons_Joint(neuronName)
desiredAngle = self.nn.Get_Value_Of(neuronName) * c.motorJointRange
for i in self.motors:
self.motors[i].Set_Value(self.robot, desiredAngle)
def Think(self):
self.nn.Update()
def Get_Fitness(self, solutionID):
stateOfLinkZero = p.getLinkState(self.robot, 0)
positionOfLinkZero = stateOfLinkZero[0]
xCoordinateOfLinkZero = positionOfLinkZero[0]
f = open("tmp" + str(self.myID) + ".txt", "w")
f.write(str(xCoordinateOfLinkZero))
f.close()
os.system("rename tmp" + str(self.myID) + ".txt fitness" + str(self.myID) + ".txt")