-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot.py
More file actions
54 lines (42 loc) · 1.63 KB
/
Copy pathrobot.py
File metadata and controls
54 lines (42 loc) · 1.63 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
52
53
54
from motor import MOTOR
from sensor import SENSOR
import pybullet as p
import pyrosim.pyrosim as pyrosim
from pyrosim.neuralNetwork import NEURAL_NETWORK
import numpy as np
class ROBOT:
def __init__(self, p, s, m):
self.motors = {}
self.motorValues = np.linspace(-np.pi, np.pi, 10000)
self.id = p.loadURDF("body.urdf")
self.nn = NEURAL_NETWORK("brain.nndf")
pyrosim.Prepare_To_Simulate("body.urdf")
self.Prepare_To_Sense()
self.Prerpare_To_Act()
def Prepare_To_Sense(self):
self.sensors = {}
self.sensorValues = {}
for linkName in pyrosim.linkNamesToIndices:
self.sensors[linkName] = SENSOR(linkName)
self.sensorValues[linkName] = np.zeros(10000)
def Prerpare_To_Act(self):
self.motors = {}
for jointName in pyrosim.jointNamesToIndices:
self.motors[jointName] = MOTOR(jointName, self.id)
def Sense(self, i):
for sensor in self.sensors:
self.sensorValues[sensor][i] = self.sensors[sensor].Sense()
def Think(self, i):
self.nn.Update()
def Act(self, i):
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)
self.motors[jointName].Set_Value(desiredAngle)
def Get_Fitness(self, onlyStraight):
pos = p.getLinkState(self.id,0)[0]
if onlyStraight:
return pos[0]
else:
return (((pos[0]-1) ** 2) + ((pos[1]+0.5) ** 2)) ** 0.5