专栏名称: 狗厂
目录
相关文章推荐
51好读  ›  专栏  ›  狗厂

【英】用Go实现树莓派控制交通灯功能

狗厂  · 掘金  ·  · 2018-05-29 03:06

正文


Raspberry Pi Coding in Go: Traffic Lights

I’ve been learning the Go programming language recently. As an exercise, I decided to revisit a past article that looked at working with traffic lights on the Raspberry Pi in Python in order to rewrite it in Go. To make this a standalone guide, there will be some re-use of content from the prior article here.

Shopping List

To try this out, you will need the following (links here mostly go to Adafruit , UK customers may want to consider Pimoroni as a UK based alternative, Amazon has most if not all of this stuff too):

  • A Raspberry Pi (I’ll use the Pi 3 Model B here, but any model with GPIO pins will work — if you want to use the Pi Zero you’ll need to solder some headers onto it). I’m going to assume you have a Pi 2 or 3 with 40 pins
  • A power supply for your Pi
  • Some sort of case is probably a good idea to protect the Pi (but you’ll need to leave the lid off to expose the GPIO pins to connect your lights to)
  • A Micro SD card to install your operating system on (or get one with the OS pre-installed ). If you want to install the operating system yourself, you’ll need a Mac, PC, Linux machine with an SD card reader
  • A set of traffic lights from Low Voltage Labs (the two pack is good value)
  • Any USB keyboard to type on the Pi
  • Any HDMI display to show output from the Pi

Attaching the Traffic Lights

The Low Voltage Labs traffic lights connect to the Pi using four pins. One of these needs to be ground, the other three being actual GPIO pins used to control each of the individual LEDs.

Before powering up the Pi, attach the traffic lights so that the pins connect to the GPIO pins highlighted in red:

When you’re done it’s going to look something like this… (an easy way to make sure you have it right is to locate the lights on the left hand row of pins as you look at the Pi with the USB ports to the bottom, then count 8 pins up and attach the lights there).

Don’t turn the Pi on yet, you’ll need to prepare an operating system image for it first…

Operating System Setup

Install the Raspbian Stretch Lite OS which can be downloaded from the official Raspberry Pi site . You can also find an excellent installation guide there should you need help. I found the free Etcher tool from Resin.io helpful when transferring the operating system image to the micro SD card.

Once you’ve got the operating system installed make sure you can login, and have a working wired or wifi internet connection configured.

Now you can go ahead and start turning lights on and off!

Installing Go

Go code can be compiled and distributed as a binary, it can also be cross compiled (where the compiler generates a binary to run on a different operating system / processor architecture than the one it was built on). We’ll look at both options here so will need to install the Go distribution on the Pi as it’s not included with Raspbian Lite.

Go installation is a simple matter of downloading the distribution ( check here for latest ARM v6 version that the Pi uses), then expanding it into /usr/local:

$ wget https://storage.googleapis.com/golang/go1.10.1.linux-armv6l.tar.gz
$ sudo tar -C /usr/local -xvf go1.10.1.linux-armv6l.tar.gz

Amend your PATH by editing ~/.profile and adding the following at the bottom:

PATH=$PATH:/usr/local/go/bin

Having saved your profile, source it to get the new value for path in your current terminal session:







请到「今天看啥」查看全文