Skip to content

Commit 8e3c553

Browse files
committed
WiringPi first pass
1 parent 724dc47 commit 8e3c553

File tree

5 files changed

+104
-235
lines changed

5 files changed

+104
-235
lines changed

deploy/Manifest.toml

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
julia_version = "1.12.4"
44
manifest_format = "2.0"
5-
project_hash = "ffb87b643daea33674fe3b248d832b13237c53d3"
5+
project_hash = "1f4210cd068465307a9c95f53a8d8f3e44b6baa9"
66

77
[[deps.ArgTools]]
88
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"
@@ -35,6 +35,12 @@ version = "1.7.0"
3535
uuid = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee"
3636
version = "1.11.0"
3737

38+
[[deps.JLLWrappers]]
39+
deps = ["Artifacts", "Preferences"]
40+
git-tree-sha1 = "0533e564aae234aff59ab625543145446d8b6ec2"
41+
uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210"
42+
version = "1.7.1"
43+
3844
[[deps.JuliaSyntaxHighlighting]]
3945
deps = ["StyledStrings"]
4046
uuid = "ac6e5ff7-fb65-4e79-a425-ec3bc9c03011"
@@ -101,12 +107,6 @@ deps = ["Artifacts", "Libdl"]
101107
uuid = "458c3c95-2e84-50aa-8efc-19380b2a3a95"
102108
version = "3.5.4+0"
103109

104-
[[deps.PiGPIO]]
105-
deps = ["Sockets"]
106-
path = "dev/PiGPIO"
107-
uuid = "bb151fc1-c6dc-5496-8ed6-07f94907e623"
108-
version = "0.2.1"
109-
110110
[[deps.Pkg]]
111111
deps = ["Artifacts", "Dates", "Downloads", "FileWatching", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "Random", "SHA", "TOML", "Tar", "UUIDs", "p7zip_jll"]
112112
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
@@ -144,10 +144,6 @@ version = "1.11.0"
144144
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
145145
version = "0.7.0"
146146

147-
[[deps.Sockets]]
148-
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"
149-
version = "1.11.0"
150-
151147
[[deps.StaticArrays]]
152148
deps = ["LinearAlgebra", "PrecompileTools", "Random", "StaticArraysCore"]
153149
git-tree-sha1 = "eee1b9ad8b29ef0d936e3ec9838c7ec089620308"
@@ -202,6 +198,18 @@ version = "1.11.0"
202198
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
203199
version = "1.11.0"
204200

201+
[[deps.WiringPi]]
202+
deps = ["WiringPi_jll"]
203+
git-tree-sha1 = "9c9733c7f8ca73698ce601fc2df625be5270f334"
204+
uuid = "56b06491-0088-44d9-858f-9bc194f2c904"
205+
version = "0.2.2"
206+
207+
[[deps.WiringPi_jll]]
208+
deps = ["Artifacts", "JLLWrappers", "Libdl"]
209+
git-tree-sha1 = "99a7679f9e8618aca73b022548c032eecdac42e1"
210+
uuid = "39e6258f-44e5-5edf-b49b-51a5e5bce6ab"
211+
version = "3.16.0+0"
212+
205213
[[deps.Zlib_jll]]
206214
deps = ["Libdl"]
207215
uuid = "83775a58-1f1d-513f-b197-d71354ab007a"

deploy/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[deps]
2-
PiGPIO = "bb151fc1-c6dc-5496-8ed6-07f94907e623"
32
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
43
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
54
Timers = "21f18d07-b854-4dab-86f0-c15a3821819a"
5+
WiringPi = "56b06491-0088-44d9-858f-9bc194f2c904"

deploy/src/encoder.jl

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
using PiGPIO
1+
using WiringPi
22

33
#=============================================================================
44
Encoder Struct and Basic Operations
55
=============================================================================#
66

77
mutable struct Encoder
8-
p::Pi
98
pin::Int
109
count::Int
1110
state::Bool
1211
end
1312

14-
function Encoder(p::Pi, pin::Int)
15-
return Encoder(p, pin, 0, PiGPIO.read(p, pin) == 1)
13+
function Encoder(pin::Int)
14+
return Encoder(pin, 0, digitalRead(pin) == HIGH)
1615
end
1716

1817
function step!(e::Encoder)
19-
new_state = PiGPIO.read(e.p, e.pin)
20-
if e.state && new_state == 0
18+
new_state = digitalRead(e.pin)
19+
if e.state && new_state == LOW
2120
e.count += 1
2221
e.state = false
23-
elseif !e.state && new_state == 1
22+
elseif !e.state && new_state == HIGH
2423
e.state = true
2524
end
2625
end
@@ -43,7 +42,7 @@ Send commands via the command channel, receive count via the response channel.
4342
4443
# Example
4544
```julia
46-
enc = Encoder(pi, pin)
45+
enc = Encoder(pin)
4746
tenc = ThreadedEncoder(enc, 1_000_000) # 1ms poll interval
4847
4948
# Read current count

deploy/src/main.jl

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using PiGPIO
1+
using WiringPi
22
using Timers
33
include(joinpath(@__DIR__, "mpu6000.jl"))
44
include(joinpath(@__DIR__, "encoder.jl"))
55

6-
module BalanceController
6+
module BalanceController
77
include(joinpath(@__DIR__, "balance_original.jl"))
88
end
99

@@ -16,9 +16,11 @@ const PWMB_RIGHT = 18 # both have hardware PWM
1616
const LTRANS_OE = 11 # enables the 3.3V <-> 5V level translator
1717
const STBY_PIN = 0
1818

19+
const PWM_RANGE = 1024 # Hardware PWM duty cycle range (0-1024)
20+
1921
function handle_err(ec)
2022
if ec == 0
21-
return
23+
return
2224
end
2325
println(Core.stdout, "Had error ", ec)
2426
return
@@ -34,12 +36,12 @@ end
3436
3537
Stop both motors by setting PWM to 0 and appropriate direction pins.
3638
"""
37-
function car_stop!(p::Pi)
38-
write(p, AIN1, 1) # HIGH
39-
write(p, BIN1, 0) # LOW
40-
write(p, STBY_PIN, 1) # HIGH (standby off = enabled, but PWM=0)
41-
PiGPIO.set_PWM_dutycycle(p, PWMA_LEFT, 0)
42-
PiGPIO.set_PWM_dutycycle(p, PWMB_RIGHT, 0)
39+
function car_stop!()
40+
digitalWrite(AIN1, HIGH)
41+
digitalWrite(BIN1, LOW)
42+
digitalWrite(STBY_PIN, HIGH) # standby off = enabled, but PWM=0
43+
pwmWrite(PWMA_LEFT, 0)
44+
pwmWrite(PWMB_RIGHT, 0)
4345
end
4446

4547
"""
@@ -48,51 +50,70 @@ end
4850
Apply PWM signals to motors based on computed control values.
4951
Handles direction pin setting based on PWM sign.
5052
"""
51-
function apply_motor_output!(p::Pi, pwm_left::Float32, pwm_right::Float32)
53+
function apply_motor_output!(pwm_left::Float32, pwm_right::Float32)
5254
# Left motor
5355
if pwm_left < 0
54-
write(p, AIN1, 1)
55-
PiGPIO.set_PWM_dutycycle(p, PWMA_LEFT, round(Int, -pwm_left))
56+
digitalWrite(AIN1, HIGH)
57+
pwmWrite(PWMA_LEFT, round(Int, -pwm_left))
5658
else
57-
write(p, AIN1, 0)
58-
PiGPIO.set_PWM_dutycycle(p, PWMA_LEFT, round(Int, pwm_left))
59+
digitalWrite(AIN1, LOW)
60+
pwmWrite(PWMA_LEFT, round(Int, pwm_left))
5961
end
6062

6163
# Right motor
6264
if pwm_right < 0
63-
write(p, BIN1, 1)
64-
PiGPIO.set_PWM_dutycycle(p, PWMB_RIGHT, round(Int, -pwm_right))
65+
digitalWrite(BIN1, HIGH)
66+
pwmWrite(PWMB_RIGHT, round(Int, -pwm_right))
6567
else
66-
write(p, BIN1, 0)
67-
PiGPIO.set_PWM_dutycycle(p, PWMB_RIGHT, round(Int, pwm_right))
68+
digitalWrite(BIN1, LOW)
69+
pwmWrite(PWMB_RIGHT, round(Int, pwm_right))
6870
end
6971
end
7072

7173
function (@main)(args)::Cint
7274
println(Core.stdout, "hello world!")
73-
p = Pi()
74-
println(Core.stdout, "pi startup done")
75-
PiGPIO.set_internals(p, 8, 0)
76-
println(Core.stdout, "set debug level")
77-
handle_err(PiGPIO.hardware_PWM(p, PWMA_LEFT, 1000, 0))
78-
handle_err(PiGPIO.hardware_PWM(p, PWMB_RIGHT, 1000, 0)) # use 1kHz pwm for the motor drivers
75+
76+
# Initialize WiringPi with BCM GPIO numbering
77+
wiringPiSetupGpio()
78+
println(Core.stdout, "wiringPi startup done")
79+
80+
# Setup GPIO pins as outputs
81+
pinMode(AIN1, OUTPUT)
82+
pinMode(BIN1, OUTPUT)
83+
pinMode(STBY_PIN, OUTPUT)
84+
pinMode(M1A, INPUT)
85+
pinMode(M2A, INPUT)
86+
87+
# Setup hardware PWM on motor pins
88+
pinMode(PWMA_LEFT, PWM_OUTPUT)
89+
pinMode(PWMB_RIGHT, PWM_OUTPUT)
90+
pwmSetMode(PWM_MODE_MS) # Mark-space mode for motor control
91+
pwmSetRange(PWM_RANGE)
92+
pwmSetClock(19) # ~1kHz PWM frequency (19.2MHz / 19 / 1024 ≈ 1kHz)
7993
println(Core.stdout, "pwm startup done")
80-
imu_bus = PiGPIO.i2c_open(p, 1, 0x68)
94+
95+
# Initialize I2C for IMU (address 0x68)
96+
imu_handle = wiringPiI2CSetup(0x68)
97+
if imu_handle < 0
98+
println(Core.stdout, "Failed to initialize I2C")
99+
return 1
100+
end
81101
println(Core.stdout, "i2c startup done")
82-
imu = MPU6000(p, imu_bus, GyroRange.GYRO_FS_250, AccelRange.ACCEL_F_2G)
102+
103+
imu = MPU6000(imu_handle, GyroRange.GYRO_FS_250, AccelRange.ACCEL_F_2G)
83104
wake!(imu)
84105
println(Core.stdout, "imu startup done")
85106
timu = ThreadedMPU6000(imu)
86-
tenc_1a = ThreadedEncoder(Encoder(p, M1A), 1_000)
87-
tenc_2a = ThreadedEncoder(Encoder(p, M2A), 1_000)
107+
tenc_1a = ThreadedEncoder(Encoder(M1A), 1_000)
108+
tenc_2a = ThreadedEncoder(Encoder(M2A), 1_000)
88109

89110
ctrl = BalanceController()
90111

91112
ml_update_rate_ns = 50000000
92113
imu_read_margin = 12600000
93114
println(Core.stdout, "start loop!")
94115
now = time_ns()
95-
while true
116+
while true
96117
wait_until(start + ml_update_rate_ns - imu_read_margin)
97118
put!(timu.request, true) # trigger the IMU request
98119
wait_until(start + ml_update_rate_ns)
@@ -104,10 +125,10 @@ function (@main)(args)::Cint
104125
imu_data.gyro_x, imu_data.gyro_y, imu_data.gyro_z)
105126
now = time_ns()
106127
if isnothing(command)
107-
car_stop!(p)
108-
else
128+
car_stop!()
129+
else
109130
left, right = command
110-
apply_motor_output!(p, left, right)
131+
apply_motor_output!(left, right)
111132
end
112133
end
113134
return 0
@@ -169,4 +190,4 @@ end
169190
readcb_specialized(stream_unknown_type, Int(nread), UInt(nrequested))
170191
nothing
171192
end
172-
end
193+
end

0 commit comments

Comments
 (0)