def writeData(self): #Get the throttle value and write it to the vesc. # Throttle duty cycle value range for vesc: -100,000 to 100,000 # Input throttle: 0 to 100 # Only write a value if we are in endurance mode if (self.cache.getNumerical('boatConfig',0) == 0): if (self.cache.getNumerical('throttleMode',0) == 0): throttle = ((self.cache.getNumerical('throttle',0)/255.0) * 100.0 * 1000.0) throttleMessage = pyvesc.SetDutyCycle(int(throttle)) self.port.write(pyvesc.encode(throttleMessage)) else: # Goal current is the MOTOR CURRENT, # To convert to motor current from battery current: MOTOR CURRENT = BATTERY CURRENT / DUTY CYCLE batteryCurrent = (self.cache.getNumerical('throttleCurrentTarget',0)) dutyCycle = (self.cache.getNumerical('controllerDutyCycle',0) / 10.0) if dutyCycle > 0: motorCurrent = (batteryCurrent / dutyCycle) * -1.0 * 1000.0 else: motorCurrent = 0 throttleMessage = pyvesc.SetCurrent(int(motorCurrent)) self.port.write(pyvesc.encode(throttleMessage)) else: self.port.write(pyvesc.encode(pyvesc.SetDutyCycle(0))) self.port.write(pyvesc.encode_request(pyvesc.GetValues))
def simple_example(): # lets make a SetDuty message my_msg = pyvesc.SetDutyCycle(255) # now lets encode it to make get a byte string back packet = pyvesc.encode(my_msg) # now lets create a buffer with some garbage in it and put our packet in the middle buffer = b'\x23\x82\x02' + packet + b'\x38\x23\x12\x01' # now lets parse our message which is hidden in the buffer msg, consumed = pyvesc.decode(buffer) # update the buffer buffer = buffer[consumed:] # check that the message we parsed is equivalent to my_msg assert my_msg.duty_cycle == msg.duty_cycle print("Success!")
#request data conn.write(pyvesc.encode_request(pyvesc.GetValues)) while conn.inWaiting() < 70: pass _buffer= conn.read(70) (vescMessage,consumed) = pyvesc.decode(_buffer) if(vescMessage): print("VESC Data:") print(vescMessage.temp_fet_filtered) print(vescMessage.temp_motor_filtered) print(vescMessage.avg_motor_current) print(vescMessage.avg_input_current) print(vescMessage.duty_cycle) print(vescMessage.rpm) print(vescMessage.v_in) print(vescMessage.amp_hours) print(vescMessage.watt_hours) print(vescMessage.tachometer) print(vescMessage.tachometer_abs) print(vescMessage.mc_fault_code) print(vescMessage.temp_mos1) print(vescMessage.temp_mos2) print(vescMessage.temp_mos3) throt_msg = pyvesc.SetDutyCycle(10000) conn.write(pyvesc.encode(throt_msg))
def changeDuty(num): ser.write(pyvesc.encode(pyvesc.SetDutyCycle(num))) time.sleep(SLEEP_TIME)
def dutyPackage(voltage) -> bytes: slowDutyCycleF = pyvesc.SetDutyCycle( voltage) # prints value of my_msg.duty_cycle sendSDCF = pyvesc.encode(slowDutyCycleF) return sendSDCF