-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvesc.py
More file actions
67 lines (52 loc) · 2.78 KB
/
Copy pathvesc.py
File metadata and controls
67 lines (52 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Contains functions for defining VESC class object.
# Obtained from : https://drive.google.com/drive/folders/1SBzChXK2ebzPHgZBP_AIhVXJOekVc0r3
import time
import donkeycar as dk
class VESC:
'''
VESC Motor controler using pyvesc
This is used for most electric scateboards.
inputs: serial_port---- port used communicate with vesc. for linux should be something like /dev/ttyACM1
has_sensor=False------- default value from pyvesc
start_heartbeat=True----default value from pyvesc (I believe this sets up a heartbeat and kills speed if lost)
baudrate=115200--------- baudrate used for communication with VESC
timeout=0.05-------------time it will try before giving up on establishing connection
percent=.2--------------max percentage of the dutycycle that the motor will be set to
outputs: none
uses the pyvesc library to open communication with the VESC and sets the servo to the angle (0-1) and the duty_cycle(speed of the car) to the throttle (mapped so that percentage will be max/min speed)
Note that this depends on pyvesc, but using pip install pyvesc will create a pyvesc file that
can only set the speed, but not set the servo angle.
Instead please use:
pip install git+https://github.com/LiamBindle/PyVESC.git@master
to install the pyvesc library
'''
def __init__(self, serial_port, percent=.2, has_sensor=False, start_heartbeat=True, baudrate=115200, timeout=0.05, steering_scale=1.0, steering_offset=0.0):
try:
import pyvesc
except Exception as err:
print("\n\n\n\n", err, "\n")
print(
"please use the following command to import pyvesc so that you can also set")
print("the servo position:")
print("pip install git+https://github.com/LiamBindle/PyVESC.git@master")
print("\n\n\n")
time.sleep(1)
raise
assert percent <= 1 and percent >= - \
1, '\n\nOnly percentages are allowed for MAX_VESC_SPEED (we recommend a value of about .2) (negative values flip direction of motor)'
self.steering_scale = steering_scale
self.steering_offset = steering_offset
self.percent = percent
try:
self.v = pyvesc.VESC(serial_port, has_sensor,
start_heartbeat, baudrate, timeout)
except Exception as err:
print("\n\n\n\n", err)
print(
"\n\nto fix permission denied errors, try running the following command:")
print("sudo chmod a+rw {}".format(serial_port), "\n\n\n\n")
time.sleep(1)
raise
def run(self, angle, throttle):
self.v.set_servo((angle * self.steering_scale) + self.steering_offset)
self.v.set_duty_cycle(throttle*self.percent)