Skip to content

Commit

Permalink
Merge branch 'main' into rsdk-9474
Browse files Browse the repository at this point in the history
  • Loading branch information
martha-johnston authored Jan 2, 2025
2 parents 9604429 + 1a86d71 commit 9f8bb0d
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 59 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
go.work
go.work.sum

bin/
bin/
.codegpt
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ $(TOOL_BIN)/golangci-lint:
.PHONY: lint
lint: $(TOOL_BIN)/golangci-lint
go mod tidy
$(TOOL_BIN)/golangci-lint run -v --fix --config=./etc/.golangci.yaml --timeout 3m
$(TOOL_BIN)/golangci-lint run -v --fix --config=./etc/.golangci.yaml --timeout 5m

.PHONY: docker-all
docker-all: docker-build-64 docker-build-32
Expand Down
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

This module implements the [`rdk:component:board` API](https://docs.viam.com/components/board/#api) and [`rdk:component:servo` API](https://docs.viam.com/components/servo/#api)

Two models are provided in this module:
* `viam:raspberry-pi:rpi` - Configure a Raspberry Pi 4, 3 and Zero 2 W, board to access GPIO functionality: input, output, PWM, power, serial interfaces, etc.
* `viam:raspberry-pi:rpi-servo` - Configure a servo controlled by the GPIO pins on the board.
The following models are provided in this module:
* `viam:raspberry-pi:rpi5` - Configure a Raspberry Pi 5 board to access GPIO functionality: input, output, PWM, power, serial interfaces, etc.
* `viam:raspberry-pi:rpi4` - Configure a Raspberry Pi 4 board
* `viam:raspberry-pi:rpi3` - Configure a Raspberry Pi 3 board
* `viam:raspberry-pi:rpi2` - Configure a Raspberry Pi 2 board
* `viam:raspberry-pi:rpi1` - Configure a Raspberry Pi 1 board
* `viam:raspberry-pi:rpi0` - Configure a Raspberry Pi 0 board
* `viam:raspberry-pi:rpi0_2` - Configure a Raspberry Pi 0 2 W board
* `viam:raspberry-pi:rpi-servo` - Configure a servo controlled by the GPIO pins on the board. Note this model is not supported on the rpi5 board.

## Requirements

Expand All @@ -22,7 +28,7 @@ Fill in the attributes as applicable to your board:
"components": [
{
"name": "<your-pi-board-name>",
"model": "viam:raspberry-pi:rpi",
"model": "viam:raspberry-pi:<rpi-model>",
"type": "board",
"namespace": "rdk",
"attributes": {
Expand Down Expand Up @@ -59,20 +65,20 @@ Fill in the attributes as applicable to your board:

### Attributes

The following attributes are available for `viam:raspberry-pi:rpi` board:
The following attributes are available for `viam:raspberry-pi:<raspberry-pi-model>` board:

| Name | Type | Required? | Description |
| ---- | ---- | --------- | ----------- |
| `analogs` | object | Optional | Attributes of any pins that can be used as analog-to-digital converter (ADC) inputs. See [configuration info](#analogs). |
| `pins` | object | Optional | Any pin's pin number and name. Used to configure gpios and interrupts. See [configuration info](#pins). |
| `pins` | object | Optional | Any pin's pin number and name. Used to configure gpios and interrupts and setting the pull state for a pin. See [configuration info](#pins). |

#### `analogs`

An [analog-to-digital converter](https://www.electronics-tutorials.ws/combination/analogue-to-digital-converter.html) (ADC) takes a continuous voltage input (analog signal) and converts it to an discrete integer output (digital signal).

ADCs are useful when building a robot, as they enable your board to read the analog signal output by most types of [sensors](https://docs.viam.com/components/sensor/) and other hardware components.

To integrate an ADC into your machine, you must first physically connect the pins on your ADC to your board.
To integrate an ADC into your machine, you must first physically connect the pins on your ADC to your board. The Pi 5 board does not currently support the use of analogs.

Then, integrate `analogs` into the `attributes` of your board by following the **Config Builder** instructions or by adding the following to your board’s JSON configuration:

Expand Down
58 changes: 11 additions & 47 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
package main

import (
"context"

"go.viam.com/rdk/components/board"
"go.viam.com/rdk/components/servo"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/module"
"go.viam.com/utils"
"go.viam.com/rdk/resource"
"raspberry-pi/pi5"
"raspberry-pi/rpi"
rpiservo "raspberry-pi/rpi-servo"
Expand All @@ -25,47 +22,14 @@ import (
// }

func main() {
utils.ContextualMain(mainWithArgs, module.NewLoggerFromArgs("raspberry-pi"))
}

func mainWithArgs(ctx context.Context, args []string, logger logging.Logger) error {
module, err := module.NewModuleFromArgs(ctx)
if err != nil {
return err
}

if err = module.AddModelFromRegistry(ctx, board.API, pi5.Model); err != nil {
return err
}
if err = module.AddModelFromRegistry(ctx, board.API, rpi.ModelPi4); err != nil {
return err
}
if err = module.AddModelFromRegistry(ctx, board.API, rpi.ModelPi3); err != nil {
return err
}
if err = module.AddModelFromRegistry(ctx, board.API, rpi.ModelPi2); err != nil {
return err
}
if err = module.AddModelFromRegistry(ctx, board.API, rpi.ModelPi1); err != nil {
return err
}
if err = module.AddModelFromRegistry(ctx, board.API, rpi.ModelPi0_2); err != nil {
return err
}
if err = module.AddModelFromRegistry(ctx, board.API, rpi.ModelPi0); err != nil {
return err
}

if err = module.AddModelFromRegistry(ctx, servo.API, rpiservo.Model); err != nil {
return err
}

err = module.Start(ctx)
defer module.Close(ctx)
if err != nil {
return err
}

<-ctx.Done()
return nil
module.ModularMain(
resource.APIModel{board.API, pi5.Model},
resource.APIModel{board.API, rpi.ModelPi},
resource.APIModel{board.API, rpi.ModelPi4},
resource.APIModel{board.API, rpi.ModelPi3},
resource.APIModel{board.API, rpi.ModelPi2},
resource.APIModel{board.API, rpi.ModelPi1},
resource.APIModel{board.API, rpi.ModelPi0_2},
resource.APIModel{board.API, rpi.ModelPi0},
resource.APIModel{servo.API, rpiservo.Model})
}
28 changes: 28 additions & 0 deletions meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,34 @@
"api": "rdk:component:board",
"model": "viam:raspberry-pi:rpi"
},
{
"api": "rdk:component:board",
"model": "viam:raspberry-pi:rpi5"
},
{
"api": "rdk:component:board",
"model": "viam:raspberry-pi:rpi4"
},
{
"api": "rdk:component:board",
"model": "viam:raspberry-pi:rpi3"
},
{
"api": "rdk:component:board",
"model": "viam:raspberry-pi:rpi2"
},
{
"api": "rdk:component:board",
"model": "viam:raspberry-pi:rpi1"
},
{
"api": "rdk:component:board",
"model": "viam:raspberry-pi:rpi0"
},
{
"api": "rdk:component:board",
"model": "viam:raspberry-pi:rpi0_2"
},
{
"api": "rdk:component:servo",
"model": "viam:raspberry-pi:rpi-servo"
Expand Down
5 changes: 3 additions & 2 deletions pi5/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"sync"
"time"

rpiutils "raspberry-pi/utils"

"github.com/pkg/errors"
"github.com/viam-modules/pinctrl/pinctrl"
"go.uber.org/multierr"
Expand All @@ -21,7 +23,6 @@ import (
"go.viam.com/rdk/logging"
"go.viam.com/rdk/resource"
"go.viam.com/utils"
rpiutils "raspberry-pi/utils"
)

// Model is the model for a Raspberry Pi 5.
Expand Down Expand Up @@ -90,7 +91,7 @@ func newBoard(
if err != nil {
logging.Global().Errorw("Cannot determine raspberry pi model", "error", err)
}
isPi5 := strings.Contains(string(piModel), "5")
isPi5 := strings.Contains(string(piModel), "Raspberry Pi 5")
// ensure that we are a pi5 when not running tests
if !isPi5 && !testingMode {
return nil, rpiutils.WrongModelErr(conf.Name)
Expand Down
9 changes: 8 additions & 1 deletion rpi/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (

// Model represents a raspberry pi board model.
var (
ModelPi = rpiutils.RaspiFamily.WithModel("rpi") // Raspberry Pi Generic model
ModelPi4 = rpiutils.RaspiFamily.WithModel("rpi4") // Raspberry Pi 4 model
ModelPi3 = rpiutils.RaspiFamily.WithModel("rpi3") // Raspberry Pi 3 model
ModelPi2 = rpiutils.RaspiFamily.WithModel("rpi2") // Raspberry Pi 2 model
Expand All @@ -56,6 +57,12 @@ var (

// init registers a pi board based on pigpio.
func init() {
resource.RegisterComponent(
board.API,
ModelPi,
resource.Registration[board.Board, *rpiutils.Config]{
Constructor: newPigpio,
})
resource.RegisterComponent(
board.API,
ModelPi4,
Expand Down Expand Up @@ -140,7 +147,7 @@ func newPigpio(
if err != nil {
logging.Global().Errorw("Cannot determine raspberry pi model", "error", err)
}
isPi5 := strings.Contains(string(piModel), "5")
isPi5 := strings.Contains(string(piModel), "Raspberry Pi 5")
if isPi5 {
return nil, rpiutils.WrongModelErr(conf.Name)
}
Expand Down

0 comments on commit 9f8bb0d

Please sign in to comment.