ev3.py 1009 B

123456789101112131415161718192021222324252627282930313233343536
  1. """Set of simple class files for Digital Twin.
  2. These can (and should) be expanded for a complete library.
  3. """
  4. from CBD.CBD import BaseBlock
  5. from pybricks.ev3devices import Motor, ColorSensor
  6. from pybricks.parameters import Port
  7. _PI = 3.14159265359
  8. def _rad2deg(radians):
  9. return radians * 180 / _PI
  10. class MotorActuatorBlock(BaseBlock):
  11. def __init__(self, block_name, port_name):
  12. super().__init__(block_name, input_ports=["IN1"], output_ports=[])
  13. self.port = getattr(Port, port_name)
  14. self.motor = Motor(self.port)
  15. def compute(self, curIteration):
  16. self.motor.run(_rad2deg(self.getInputSignal(curIteration, "IN1").value))
  17. class ReflectionSensorBlock(BaseBlock):
  18. def __init__(self, block_name, port_name):
  19. super().__init__(block_name, input_ports=[], output_ports=["OUT1"])
  20. self.port = getattr(Port, "S" + port_name)
  21. self.sensor = ColorSensor(self.port)
  22. def compute(self, curIteration):
  23. ref = self.sensor.reflection()
  24. if ref is None:
  25. ref = 0.0
  26. self.appendToSignal(ref, "OUT1")