示例#1
0
 def _maybe_init(self):
   """
   Setup for simulation.
   """
   if not self._initialized:
     assert is_number(self.init_alpha), 'init_alpha must be a number'
     self.init_alpha = clip(self.init_alpha, 0, 1)
     self._resistor_1 = Resistor(self.n_top, self.n_middle, self.i_top_middle,
         (1 - self.init_alpha) * self.r)
     self._resistor_2 = Resistor(self.n_middle, self.n_bottom,
         self.i_middle_bottom, self.init_alpha * self.r)
     self._initialized = True
示例#2
0
 def step(self, current_solution):
   assert self.motor_plus in current_solution, ('%s not in current solution' %
       self.n1)
   assert self.motor_minus in current_solution, ('%s not in current solution'
       % self.n2)
   current_angle = self.angle_samples[-1]
   current_speed = self.speed_samples[-1]
   voltage_accross = current_solution[self.motor_plus] - current_solution[
       self.motor_minus]
   torque = self.Kt * (voltage_accross - self.Kb * current_speed) / self.r
   acceleration = (torque - self.B * current_speed) / self.J
   new_speed = current_speed + T * acceleration
   new_angle = current_angle + T * new_speed
   if not in_bounds(new_angle, self.angle_min, self.angle_max):
     # if angle has not really changed, then force speed to be 0
     if current_angle in (self.angle_min, self.angle_max):
       new_speed = 0
     new_angle = clip(new_angle, self.angle_min, self.angle_max)
   self.angle_samples.append(new_angle)
   self.speed_samples.append(new_speed)
   Component.step(self, current_solution)