robotThree.py
1    import constants as c 
2    import random 
3     
4    class ROBOT: 
5        def __init__(self, sim, wts): 
6     
7            self.send_objects(sim) 
8            self.send_joints(sim) 
9            self.send_sensors(sim) 
10           self.send_neurons(sim) 
11           self.send_synapses(sim, wts) 
12    
13           del self.O 
14           del self.J 
15           del self.S 
16           del self.SN 
17           del self.MN 
18    
19       def send_objects(self, sim): 
20    
21           # send in an object 
22           self.O0 = sim.send_box(x=0, y=0, z=c.L + c.R, length=2.5*c.L, width=c.L, height=c.R * 2, r=.5, g=.5, b=.5) 
23    
24    
25    
26           # sends a cylinder into environment 
27           self.O1 = sim.send_cylinder(x=c.L/1.4, y=c.L-c.R, z=c.L + c.R, length=c.L/2, radius=c.R, r=1, g=0, b=0, r1=1, r2=0, r3=0) 
28    
29           # sends a cylinder into environment 
30           self.O2 = sim.send_cylinder(x=c.L/1.4, y=-c.L+c.R, z=c.L + c.R, length=c.L/2, radius=c.R, r=0, g=0, b=1, r1=1, r2=0, r3=0) 
31    
32           # sends a cylinder into environment 
33           self.O3 = sim.send_cylinder(x=-c.L/1.4, y=c.L-c.R, z=c.L + c.R, length=c.L/2, radius=c.R, r=0, g=1, b=0, r1=1, r2=0, r3=0) 
34    
35           # sends a cylinder into environment 
36           self.O4 = sim.send_cylinder(x=-c.L/1.4, y=-c.L+c.R, z=c.L + c.R, length=c.L/2, radius=c.R, r=1, g=0, b=1,  r1=1, r2=0, r3=0) 
37    
38           # sends a cylinder into environment 
39           self.O5 = sim.send_cylinder(x=c.L, y=c.L-c.R, z=(c.L)/2 + c.R, length=c.L, radius=c.R, r=1, g=0, b=0, r1=0, r2=0, r3=1) 
40    
41           # sends a cylinder into environment 
42           self.O6 = sim.send_cylinder(x=c.L, y=-c.L+c.R, z=(c.L)/2 + c.R, length=c.L, radius=c.R, r=0, g=0, b=1, r1=0, r2=0, r3=1) 
43    
44           # sends a cylinder into environment 
45           self.O7 = sim.send_cylinder(x=-c.L, y=c.L-c.R, z=(c.L)/2 + c.R, length=c.L, radius=c.R, r=0, g=1, b=0, r1=0, r2=0, r3=1) 
46    
47           # sends a cylinder into environment 
48           self.O8 = sim.send_cylinder(x=-c.L, y=-c.L+c.R, z=(c.L)/2 + c.R, length=c.L, radius=c.R, r=1, g=0, b=1, r1=0, r2=0, r3=1) 
49    
50    
51    
52           # sends a cylinder into environment 
53           self.O9 = sim.send_cylinder(x=c.L/1.4, y=0, z=c.L + c.R, length=c.L / 2, radius=c.R, r=1, g=1, b=0, 
54                                       r1=1, r2=0, r3=0) 
55    
56           # sends a cylinder into environment 
57           self.O10 = sim.send_cylinder(x=-c.L/1.4, y=0, z=c.L + c.R, length=c.L / 2, radius=c.R, r=0, g=0, b=0, 
58                                       r1=1, r2=0, r3=0) 
59    
60           # sends a cylinder into environment 
61           self.O11 = sim.send_cylinder(x=-c.L, y=0, z=(c.L) / 2 + c.R, length=c.L, radius=c.R, r=0, g=0, b=0, r1=0, 
62                                       r2=0, r3=1) 
63    
64           # sends a cylinder into environment 
65           self.O12 = sim.send_cylinder(x=c.L, y=0, z=(c.L) / 2 + c.R, length=c.L, radius=c.R, r=1, g=1, b=0, 
66                                       r1=0, r2=0, r3=1) 
67    
68       def send_joints(self, sim): 
69    
70    
71           # creates a joint between the two cylinders 
72           self.J0 = sim.send_hinge_joint(first_body_id=self.O0, second_body_id=self.O1, 
73                                          x=c.L/1.4, y=c.L-c.R, z=c.L+c.R, 
74                                          n1=0, n2=0, n3=-1) 
75    
76           # creates a joint between the two cylinders 
77           self.J1 = sim.send_hinge_joint(first_body_id=self.O1, second_body_id=self.O5, 
78                                          x=c.L, y=c.L-c.R, z=c.L+c.R, 
79                                          n1=0, n2=-1, n3=0) 
80    
81           # creates a joint between the two cylinders 
82           self.J2 = sim.send_hinge_joint(first_body_id=self.O0, second_body_id=self.O2, 
83                                          x=c.L/1.4, y=-c.L+c.R, z=c.L+c.R, 
84                                          n1=0, n2=0, n3=-1) 
85    
86           # creates a joint between the two cylinders 
87           self.J3 = sim.send_hinge_joint(first_body_id=self.O2, second_body_id=self.O6, 
88                                          x=c.L, y=-c.L+c.R, z=c.L+c.R, 
89                                          n1=0, n2=-1, n3=0) 
90    
91           # creates a joint between the two cylinders (PROBLEM WITH GREEN LEG JOINT TO BODY) 
92           self.J4 = sim.send_hinge_joint(first_body_id=self.O0, second_body_id=self.O3, 
93                                          x=-c.L/1.4, y=c.L+c.R, z=c.L+c.R, 
94                                          n1=0, n2=0, n3=-1) 
95    
96           # creates a joint between the two cylinders 
97           self.J5 = sim.send_hinge_joint(first_body_id=self.O3, second_body_id=self.O7, 
98                                          x=-c.L, y=c.L-c.R, z=c.L+c.R, 
99                                          n1=0, n2=-1, n3=0) 
100   
101          # creates a joint between the two cylinders 
102          self.J6 = sim.send_hinge_joint(first_body_id=self.O0, second_body_id=self.O4, 
103                                         x=-c.L/1.4, y=-c.L+c.R, z=c.L+c.R, 
104                                         n1=0, n2=0, n3=-1) 
105   
106          # creates a joint between the two cylinders 
107          self.J7 = sim.send_hinge_joint(first_body_id=self.O4, second_body_id=self.O8, 
108                                         x=-c.L, y=-c.L+c.R, z=c.L+c.R, 
109                                         n1=0, n2=-1, n3=0) 
110   
111   
112   
113          # creates a joint between the two cylinders 
114          self.J8 = sim.send_hinge_joint(first_body_id=self.O0, second_body_id=self.O9, 
115                                         x=c.L/1.4, y=0, z=c.L+c.R, 
116                                         n1=0, n2=0, n3=-1) 
117   
118          # creates a joint between the two cylinders 
119          self.J11 = sim.send_hinge_joint(first_body_id=self.O9, second_body_id=self.O12, 
120                                          x=c.L, y=0, z=c.L + c.R, 
121                                          n1=0, n2=-1, n3=0) 
122   
123   
124          # creates a joint between the two cylinders 
125          self.J10 = sim.send_hinge_joint(first_body_id=self.O0, second_body_id=self.O10, 
126                                         x=-c.L/1.4, y=0, z=c.L+c.R, 
127                                         n1=0, n2=0, n3=-1) 
128   
129   
130          # creates a joint between the two cylinders 
131          self.J9 = sim.send_hinge_joint(first_body_id=self.O10, second_body_id=self.O11, 
132                                         x=-c.L, y=0, z=c.L+c.R, 
133                                         n1=0, n2=-1, n3=0) 
134   
135   
136   
137   
138   
139      def send_sensors(self, sim): 
140   
141          # add a sensor to the white cylinder 
142          self.T0 = sim.send_touch_sensor(body_id=self.O5) 
143   
144          # add a sensor to the red cylinder 
145          self.T1 = sim.send_touch_sensor(body_id=self.O6) 
146   
147          # adds a proprioceptive sensor to joint 
148          self.T2 = sim.send_touch_sensor(body_id=self.O7) 
149   
150          # adds a ray sensor to the end of the red cylinder: 
151          self.T3 = sim.send_touch_sensor(body_id=self.O8) 
152   
153          # adds a proprioceptive sensor to joint 
154          self.T4 = sim.send_touch_sensor(body_id=self.O11) 
155   
156          # adds a ray sensor to the end of the red cylinder: 
157          self.T5 = sim.send_touch_sensor(body_id=self.O12) 
158   
159          # adds position sensor to robot 
160          self.L4 = sim.send_light_sensor(body_id=self.O0) 
161   
162      def send_neurons(self, sim): 
163   
164          self.O = {} 
165          self.O[0] = self.O0 
166          self.O[1] = self.O1 
167          self.O[2] = self.O2 
168          self.O[3] = self.O3 
169          self.O[4] = self.O4 
170          self.O[5] = self.O5 
171          self.O[6] = self.O6 
172          self.O[7] = self.O7 
173          self.O[8] = self.O8 
174          self.O[9] = self.O9 
175          self.O[10] = self.O10 
176          self.O[11] = self.O11 
177          self.O[12] = self.O12 
178   
179   
180   
181          self.J = {} 
182          self.J[0] = self.J0 
183          self.J[1] = self.J1 
184          self.J[2] = self.J2 
185          self.J[3] = self.J3 
186          self.J[4] = self.J4 
187          self.J[5] = self.J5 
188          self.J[6] = self.J6 
189          self.J[7] = self.J7 
190          self.J[8] = self.J8 
191          self.J[9] = self.J9 
192          self.J[10] = self.J10 
193          self.J[11] = self.J11 
194   
195   
196          self.S = {} 
197          self.S[0] = self.T0 
198          self.S[1] = self.T1 
199          self.S[2] = self.T2 
200          self.S[3] = self.T3 
201          self.S[4] = self.T4 
202          self.S[5] = self.T5 
203          self.S[6] = self.L4 
204   
205          self.SN = {} 
206          self.MN = {} 
207   
208          for s in self.S: 
209              self.SN[s] = sim.send_sensor_neuron(sensor_id=self.S[s]) 
210   
211          for j in self.J: 
212              self.MN[j] = sim.send_motor_neuron(joint_id=self.J[j], tau=1) 
213   
214   
215      def send_synapses(self, sim, wts): 
216   
217          for j in self.SN: 
218              for i in self.MN: 
219                  sim.send_synapse(source_neuron_id=self.SN[j], target_neuron_id=self.MN[i], weight=wts[j, i]) 
220   
221          # for loop that creates synapses between each sensor neuron and the motor neuron MN2 
222          #for s in self.sensorNeurons: 
223           #   for m in self.motorNeurons: 
224            #      sim.send_synapse(source_neuron_id=self.sensorNeurons[s], target_neuron_id=self.motorNeurons[m], weight=wts[s]) 
225   
226   
227