-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from LiamBindle/get-values
Get Values from VESC sensors and encoder
- Loading branch information
Showing
6 changed files
with
151 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import pyvesc | ||
from pyvesc import GetValues, SetRPM, SetCurrent, SetRotorPositionMode, GetRotorPosition | ||
import serial | ||
import time | ||
|
||
# Set your serial port here (either /dev/ttyX or COMX) | ||
serialport = 'COM3' | ||
|
||
def get_values_example(): | ||
with serial.Serial(serialport, baudrate=115200, timeout=0.05) as ser: | ||
try: | ||
# Optional: Turn on rotor position reading if an encoder is installed | ||
ser.write(pyvesc.encode(SetRotorPositionMode(SetRotorPositionMode.DISP_POS_OFF))) | ||
while True: | ||
# Set the ERPM of the VESC motor | ||
# Note: if you want to set the real RPM you can set a scalar | ||
# manually in setters.py | ||
# 12 poles and 19:1 gearbox would have a scalar of 1/228 | ||
ser.write(pyvesc.encode(SetRPM(10000))) | ||
|
||
# Request the current measurement from the vesc | ||
ser.write(pyvesc.encode_request(GetValues)) | ||
|
||
# Check if there is enough data back for a measurement | ||
if ser.in_waiting > 61: | ||
(response, consumed) = pyvesc.decode(ser.read(61)) | ||
|
||
# Print out the values | ||
try: | ||
print(response.rpm) | ||
|
||
except: | ||
# ToDo: Figure out how to isolate rotor position and other sensor data | ||
# in the incoming datastream | ||
#try: | ||
# print(response.rotor_pos) | ||
#except: | ||
# pass | ||
pass | ||
|
||
time.sleep(0.1) | ||
|
||
except KeyboardInterrupt: | ||
# Turn Off the VESC | ||
ser.write(pyvesc.encode(SetCurrent(0))) | ||
|
||
if __name__ == "__main__": | ||
get_values_example() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from pyvesc.messages.base import VESCMessage | ||
|
||
class GetValues(metaclass=VESCMessage): | ||
""" | ||
Gets internal sensor data | ||
""" | ||
id = 4 | ||
|
||
fields = [ | ||
('temp_mos1', 'h', 10), | ||
('temp_mos2', 'h', 10), | ||
('temp_mos3', 'h', 10), | ||
('temp_mos4', 'h', 10), | ||
('temp_mos5', 'h', 10), | ||
('temp_mos6', 'h', 10), | ||
('temp_pcb', 'h', 10), | ||
('current_motor', 'i', 100), | ||
('current_in', 'i', 100), | ||
('duty_now', 'h', 1000), | ||
('rpm', 'i', 1), | ||
('v_in', 'h', 10), | ||
('amp_hours', 'i', 10000), | ||
('amp_hours_charged', 'i', 10000), | ||
('watt_hours', 'i', 10000), | ||
('watt_hours_charged', 'i', 10000), | ||
('tachometer', 'i', 1), | ||
('tachometer_abs', 'i', 1), | ||
('mc_fault_code', 'c') | ||
] | ||
|
||
|
||
class GetRotorPosition(metaclass=VESCMessage): | ||
""" | ||
Gets rotor position data | ||
Note: Must be set to DISP_POS_MODE_ENCODER or DISP_POS_MODE_PID_POS (Mode 3 or Mode 4) | ||
This is set by SetRotorPositionMode (id=21) | ||
""" | ||
id = 21 | ||
|
||
fields = [ | ||
('rotor_pos', 'i', 100000) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters