@@ -25,14 +25,6 @@ $ python3 -m venv hat_env
25
25
$ source hat_env/bin/activate
26
26
```
27
27
28
- You may need to install the I2C development library:
29
-
30
- ```
31
- $ sudo apt install libi2c-dev
32
- ```
33
-
34
- and you will certainly need to turn I2C on in your boot configuration file.
35
-
36
28
Now use the setup.py script to build and install the module:
37
29
38
30
```
@@ -44,12 +36,8 @@ Now use the setup.py script to build and install the module:
44
36
You should now be able to "import hub" in a Python3 script and have
45
37
the module available to you.
46
38
47
- Optionally the code may be compiled with "DEBUG\_ I2C=1" to enable logging
48
- of the I2C traffic on the hub.
49
-
50
- The code may be compiled with "USE\_ DUMMY\_ I2C=1" to use an ethernet local
51
- loopback as a fake I2C for test purposes.
52
-
39
+ Optionally the code may be compiled with "DEBUG\_ UART=1" to enable logging
40
+ of the UART traffic on the hub.
53
41
54
42
Documentation
55
43
-------------
@@ -71,32 +59,37 @@ Usage
71
59
72
60
See the detailed documentation for the Python objects available.
73
61
74
- To control a motor attached to port A:
75
-
76
62
``` python
77
- from build_hat import BuildHAT
78
-
79
- bh = BuildHAT()
80
- bh.port.A.motor.run_for_time(1000 , 127 ) # run for 1000ms at maximum clockwise speed
81
- bh.port.A.motor.run_for_time(1000 , - 127 ) # run for 1000ms at maximum anticlockwise speed
82
- bh.port.A.motor.run_for_degrees(180 , 127 ) # turn 180 degrees clockwise at maximum speed
83
- bh.port.A.motor.run_for_degrees(720 , - 127 ) # Make two rotations anticlockwise at maximum speed
84
- bh.port.A.motor.run_to_position(0 , 127 ) # Move to top dead centre at maximum speed (positioning seems to be absolute)
85
- bh.port.A.motor.run_to_position(180 , 127 ) # Move to 180 degrees forward of top dead centre at maximum speed
86
- ```
63
+ import time
64
+ from signal import pause
65
+ from buildhat import Motor
87
66
88
- To rotate motor attached to port once clockwise when a button attached to port B is pressed:
67
+ motor = Motor(' A' )
68
+ motor.set_default_speed(30 )
89
69
90
- ``` python
91
- from build_hat import BuildHAT
92
- import time
70
+ print (" Position" , motor.get_aposition())
71
+
72
+ def handle_motor (speed , pos , apos ):
73
+ print (" Motor" , speed, pos, apos)
74
+
75
+ motor.when_rotated = handle_motor
76
+
77
+ print (" Run for degrees" )
78
+ motor.run_for_degrees(360 )
79
+
80
+ print (" Run for seconds" )
81
+ motor.run_for_seconds(5 )
82
+
83
+ print (" Run for rotations" )
84
+ motor.run_for_rotations(2 )
93
85
94
- bh = BuildHAT( )
95
- while True :
96
- if bh.port.B.device.get() > 0 : # test if button is pressed
97
- bh.port.A.motor.run_for_degrees( 360 , 127 ) # turn 360 degrees clockwise at maximum speed
98
- time.sleep( 0.5 ) # Wait half a second for motor to finish turning
86
+ print ( " Start motor " )
87
+ motor.start()
88
+ time.sleep( 3 )
89
+ print ( " Stop motor " )
90
+ motor.stop()
99
91
92
+ pause()
100
93
```
101
94
102
95
Programming Bootloader
0 commit comments