Skip to content

Commit 8e3c553

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

5 files changed

Lines changed: 104 additions & 235 deletions

File tree

deploy/Manifest.toml

Lines changed: 19 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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)