Skip to content

Commit

Permalink
zero: introduce adaptor for Radxa Zero (#1128)
Browse files Browse the repository at this point in the history
  • Loading branch information
gen2thomas authored Jan 23, 2025
1 parent c986109 commit cda3dbc
Show file tree
Hide file tree
Showing 10 changed files with 870 additions and 12 deletions.
60 changes: 60 additions & 0 deletions examples/zero_analog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//go:build example
// +build example

//
// Do not build by default.

package main

import (
"fmt"
"log"
"time"

"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/aio"
"gobot.io/x/gobot/v2/platforms/radxa/zero"
)

// Wiring:
// PWR : 1, 17 (+3.3V, VCC), 2, 4 (+5V), 6, 9, 14, 20, 25, 30, 34, 39 (GND)
// ADC (max. 1.8V): header pin 15 is input for channel 1, pin 26 is input for channel 2
func main() {
const (
inPin0 = "15_mean"
inPin1 = "26"
inVoltageScale = 0.439453125 // see README.md of the platform
)

scaler := aio.AnalogSensorLinearScaler(0, 4095, 0, 1.8)

adaptor := zero.NewAdaptor()
ana0 := aio.NewAnalogSensorDriver(adaptor, inPin0, aio.WithSensorScaler(scaler))
ana1 := aio.NewAnalogSensorDriver(adaptor, inPin1)

work := func() {
gobot.Every(500*time.Millisecond, func() {
v0, err := ana0.Read()
if err != nil {
log.Println(err)
}

v1, err := ana1.Read()
if err != nil {
log.Println(err)
}

fmt.Printf("%s: %1.3f V, %s: %2.0f (%4.0f mV)\n", inPin0, v0, inPin1, v1, v1*inVoltageScale)
})
}

robot := gobot.NewRobot("adcBot",
[]gobot.Connection{adaptor},
[]gobot.Device{ana0, ana1},
work,
)

if err := robot.Start(); err != nil {
panic(err)
}
}
80 changes: 80 additions & 0 deletions examples/zero_direct_pin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//go:build example
// +build example

//
// Do not build by default.

package main

import (
"fmt"
"time"

"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/platforms/adaptors"
"gobot.io/x/gobot/v2/platforms/radxa/zero"
)

// Wiring
// PWR : 1, 17 (+3.3V, VCC), 2, 4 (+5V), 6, 9, 14, 20, 25, 30, 34, 39 (GND)
// GPIO : header pin 24 is input, pin 32 used as normal output, pin 36 used as inverted output
// Button: the input pin is wired with a button to GND, the internal pull up resistor is used
// LED's: the output pins are wired to the cathode of the LED, the anode is wired with a resistor (70-130Ohm for 20mA)
// to VCC
// Expected behavior: always one LED is on, the other in opposite state, if button is pressed the state changes
func main() {
const (
inPinNum = "24"
outPinNum = "32"
outPinInvertedNum = "36"
)
// note: WithGpiosOpenDrain() is optional, if using WithGpiosOpenSource() the LED's will not light up
board := zero.NewAdaptor(adaptors.WithGpiosActiveLow(outPinInvertedNum),
adaptors.WithGpiosOpenDrain(outPinNum, outPinInvertedNum), adaptors.WithGpiosPullUp(inPinNum))

inPin := gpio.NewDirectPinDriver(board, inPinNum)
outPin := gpio.NewDirectPinDriver(board, outPinNum)
outPinInverted := gpio.NewDirectPinDriver(board, outPinInvertedNum)

work := func() {
level := byte(1)

gobot.Every(500*time.Millisecond, func() {
read, err := inPin.DigitalRead()
fmt.Printf("pin %s state is %d\n", inPinNum, read)
if err != nil {
fmt.Println(err)
if level == 1 {
level = 0
} else {
level = 1
}
} else {
level = byte(read)
}

err = outPin.DigitalWrite(level)
fmt.Printf("pin %s is now %d\n", outPinNum, level)
if err != nil {
fmt.Println(err)
}

err = outPinInverted.DigitalWrite(level)
fmt.Printf("pin %s is now not %d\n", outPinInvertedNum, level)
if err != nil {
fmt.Println(err)
}
})
}

robot := gobot.NewRobot("pinBot",
[]gobot.Connection{board},
[]gobot.Device{inPin, outPin, outPinInverted},
work,
)

if err := robot.Start(); err != nil {
panic(err)
}
}
15 changes: 8 additions & 7 deletions examples/tinkerboard_servo.go → examples/zero_servo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,26 @@ import (
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/platforms/adaptors"
"gobot.io/x/gobot/v2/platforms/asus/tinkerboard"
"gobot.io/x/gobot/v2/platforms/radxa/zero"
)

// Wiring
// PWR Tinkerboard: 1 (+3.3V, VCC), 2(+5V), 6, 9, 14, 20 (GND)
// PWM Tinkerboard: header pin 33 (PWM2) or pin 32 (PWM3)
// PWR: 1, 17 (+3.3V, VCC), 2, 4 (+5V), 6, 9, 14, 20, 25, 30, 34, 39 (GND)
// PWM: header pin 18 (PWM_C), 40 (PWMAO_A)
// Servo SG90: red (+5V), brown (GND), orange (PWM)
func main() {
const (
pwmPin = "32"
pwmPin = "18"
wait = 3 * time.Second

fiftyHzNanos = 20 * 1000 * 1000 // 50Hz = 0.02 sec = 20 ms
)
// usually a frequency of 50Hz is used for servos, most servos have 0.5 ms..2.5 ms for 0-180°,
// however the mapping can be changed with options:
adaptor := tinkerboard.NewAdaptor(
adaptor := zero.NewAdaptor(
adaptors.WithPWMDefaultPeriodForPin(pwmPin, fiftyHzNanos),
adaptors.WithPWMServoDutyCycleRangeForPin(pwmPin, time.Millisecond, 2*time.Millisecond),
adaptors.WithPWMServoAngleRangeForPin(pwmPin, 0, 270),
adaptors.WithPWMServoDutyCycleRangeForPin(pwmPin, 500*time.Microsecond, 2500*time.Microsecond),
adaptors.WithPWMServoAngleRangeForPin(pwmPin, 0, 180),
)
servo := gpio.NewServoDriver(adaptor, pwmPin)

Expand Down
10 changes: 5 additions & 5 deletions examples/tinkerboard_yl40.go → examples/zero_yl40.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ import (

"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/i2c"
"gobot.io/x/gobot/v2/platforms/asus/tinkerboard"
"gobot.io/x/gobot/v2/platforms/radxa/zero"
)

func main() {
// Wiring
// PWR Tinkerboard: 1 (+3.3V, VCC), 6, 9, 14, 20 (GND)
// I2C1 Tinkerboard: 3 (SDA), 5 (SCL)
// PWR : 1, 17 (+3.3V, VCC), 6, 9, 14, 20, 25, 30, 34, 39 (GND)
// I2C3: 3 (SDA), 5 (SCL), I2C1: 24/16 (SDA), 23/13 (SCL), I2C4: 7 (SDA), 11 (SCL)
// YL-40 module: wire AOUT --> AIN2 for this example
//
// Note: temperature measurement is often buggy, because sensor is not properly grounded
// fix it by soldering a small bridge to the adjacent ground pin of brightness sensor
board := tinkerboard.NewAdaptor()
yl := i2c.NewYL40Driver(board, i2c.WithBus(1))
board := zero.NewAdaptor()
yl := i2c.NewYL40Driver(board, i2c.WithBus(3))

work := func() {
// the LED light is visible above ~1.7V
Expand Down
13 changes: 13 additions & 0 deletions platforms/radxa/zero/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (c) 2025 The Hybrid Group

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Loading

0 comments on commit cda3dbc

Please sign in to comment.