Exemplo n.º 1
0
    def __init__(self, serial_port, has_sensor=False, start_heartbeat=True, baudrate=115200, timeout=0.05):
        """
        :param serial_port: Serial device to use for communication (i.e. "COM3" or "/dev/tty.usbmodem0")
        :param has_sensor: Whether or not the bldc motor is using a hall effect sensor
        :param start_heartbeat: Whether or not to automatically start the heartbeat thread that will keep commands
                                alive.
        :param baudrate: baudrate for the serial communication. Shouldn't need to change this.
        :param timeout: timeout for the serial communication
        """

        if serial is None:
            raise ImportError("Need to install pyserial in order to use the VESCMotor class.")

        self.serial_port = serial.Serial(port=serial_port, baudrate=baudrate, timeout=timeout)
        if has_sensor:
            self.serial_port.write(encode(SetRotorPositionMode(SetRotorPositionMode.DISP_POS_OFF)))

        self.heart_beat_thread = threading.Thread(target=self._heartbeat_cmd_func)
        self._stop_heartbeat = threading.Event()

        if start_heartbeat:
            self.start_heartbeat()

        # check firmware version and set GetValue fields to old values if pre version 3.xx
        version = self.get_firmware_version()
        if int(version.split('.')[0]) < 3:
            GetValues.fields = pre_v3_33_fields

        # store message info for getting values so it doesn't need to calculate it every time
        msg = GetValues()
        self._get_values_msg = encode_request(msg)
        self._get_values_msg_expected_length = msg._full_msg_size
Exemplo n.º 2
0
 def set_servo(self, new_servo_pos):
     """
     :param new_servo_pos: New servo position. valid range [0, 1]
     """
     self.write(encode(SetServoPosition(new_servo_pos)))
Exemplo n.º 3
0
 def set_duty_cycle(self, new_duty_cycle):
     """
     :param new_duty_cycle: Value of duty cycle to be set (range [-1e5, 1e5]).
     """
     self.write(encode(SetDutyCycle(new_duty_cycle)))
Exemplo n.º 4
0
 def set_current(self, new_current):
     """
     :param new_current: new current in milli-amps for the motor
     """
     self.write(encode(SetCurrent(new_current)))
Exemplo n.º 5
0
 def set_rpm(self, new_rpm):
     """
     Set the electronic RPM value (a.k.a. the RPM value of the stator)
     :param new_rpm: new rpm value
     """
     self.write(encode(SetRPM(new_rpm)))
Exemplo n.º 6
0
     :ivar pos_mode: Value of the mode
     """

    DISP_POS_OFF = 0
    DISP_POS_MODE_ENCODER = 3
    DISP_POS_MODE_PID_POS = 4
    DISP_POS_MODE_PID_POS_ERROR = 5

    id = VedderCmd.COMM_SET_DETECT
    fields = [('pos_mode', 'b')]


class SetServoPosition(metaclass=VESCMessage):
    """Sets the position of s servo connected to the VESC.

    :ivar servo_pos: Value of position (range [0, 1])
    """

    id = VedderCmd.COMM_SET_SERVO_POS
    fields = [('servo_pos', 'h', 1000)]


class Alive(metaclass=VESCMessage):
    """Heartbeat signal to keep VESC alive"""
    id = VedderCmd.COMM_ALIVE
    fields = []


# statically save this message because it does not need to be recalculated
alive_msg = encode(Alive())