[PYTHON] Let's make a cycle computer with Raspberry Pi Zero (W, WH)

github repository is here https://github.com/hishizuka/pizero_bikecomputer

(2020/4/30) We reviewed the article structure and changed the "latest update" to the original article "[A story that I tried to make a cycle computer with a sword, but it was relatively successful](https: // qiita) .com / hisi / items / 8f77fd4655027fac7c7d) ”.

This is the 22nd day of Road Bike Advent Calendar 2019. It's a very unique post, but thank you.

The author has been working on building a cycle computer from scratch for several years. Since I reached a certain shape in the spring of 2019, I released "A story that I tried to make a cycle computer with a gachi and it was relatively good" did. ~~ Actually, it was an application for Maker Faire Tokyo 2019, so I wrote it with a lot of poem ingredients, but I was brilliantly rejected (laughs) ~~

Thankfully, this post itself received a great deal of feedback, so this time I took the opportunity to transcribe the sequel.

This time, the recipe (program + how to assemble) will be released, so please try it by actually moving your hands! We hope that you will feel the new enjoyment of being able to create your own device from a small single computer.

Since the volume of the article has become quite large, the explanation of the program will be posted at a later date, and daily detailed updates will be posted on Twitter. Thank you for your cooperation.

(2020/4/29) The description of "Most recent update" is described in the first article "[Story that I tried to make a cycle computer with Gachi, but it was relatively successful](https://qiita.com/hishi/ I merged it into items / 8f77fd4655027fac7c7d) "and deleted it from this article.

Introduction

I am making a cycle computer for sports bicycles with Raspberry Pi Zero W. For the functions and hardware configuration being implemented, please read "The story that I tried to make a cycle computer with a gut, but it was relatively successful" Please give me.

It looks like this now. It is waterproof to some extent by substituting Topeak's smartphone case. title-02.png title-01.png

Since there is no outer case, the buttons are a little difficult to press, but they can be used normally. In 2019, we got data of over 2,500km (= roughly half a day 50km x 4 weeks x 12 months somewhere on Saturdays and Sundays). (I miss the time when I was able to run a total of 200km or more on Saturdays and Sundays without any restrictions ...)

How to make

Well then, wait, the recipe will be unveiled. It's quite long, so please use it when you have time to spare.

Confirmation of startup on PC

First, try starting the software on your PC.

Things necessary

--python (version3 series) execution environment --pip3 command --Python programming skills, skills to install required packages --git command

Until startup

Bring it from the repository with the git command.

$ git clone https://github.com/hishizuka/pizero_bikecomputer.git

Make a copy of the initialization file.

$ cd pizero_bikecomputer
$ cp install/setting-orig.conf setting.conf
$ cp layouts/layout-cycling.yaml layout.yaml

Install the required packages. PyQtGraph has been partially modified, so please install the one in my repository.

$ pip3 install PyQt5 numpy oyaml pillow
$ pip3 install git+https://github.com/hishizuka/pyqtgraph.git

Check if it can be started.

$ python3 pizero_bikecomputer.py

app-01.png

With the --demo option, the numbers may or may not appear as appropriate.

The necessary directories and setting files (setting.conf) will be created at the first startup, so close them once.

How to use

The buttons at the bottom of the screen are as you can see, but the following functions are assigned in order from the left. (Actually, both short press and long press can be changed)

--Left (←) button: Move between screens --Wrap button: Wrap. When stopped, press and hold to reset and output the fit file (changeable) under the log folder. --Menu button: See below --Start / Stop button: Transition between start and stop states. Press and hold to end the program. --Right (→) button: Move between screens

Next is the menu screen. As of December 2019, I haven't made it yet, so don't expect it (laughs) app-02.png

As you can imagine,

One of the major features of this program is that you can develop a program that draws the screen directly on the actual machine on a PC without writing it. The barometric pressure sensor and GPS values can only be confirmed on the actual machine, but it is much more efficient to write the screen and main logic on a PC.

Now that you've reached this point, let's display it on the actual desktop!

Raspberry Pi Zero + Pi TFT version

It's easy to get a total of around 10,000 yen, which was introduced in "A story that I made a cycle computer with a sledgehammer and it was relatively successful". Let's actually make a cycle computer with various configurations.

pitft-01.png

Things necessary

hardware

There are quite a lot, so please do your best to IYH or buy them in Akihabara!

--Raspberry Pi Zero (W, WH) (around 1,000 yen) --Please refer to my article "Raspberry Pi Zero (W, WH) Setup" etc. --TFT LCD module with touch screen --PiTFT 2.4 inch recommended (less than 5,000 yen) --Also, please use "Use PiTFT with Raspberry Pi Zero (W, WH)" for setup. --Assuming that the X Window can be displayed --GPS module (2,000 yen ~, option) -Akizuki Denshi Module is probably the best introduction --Assuming that it is available on GPSd. Setup will be described later. --ANT + USB dongle (around 1,000 yen, optional) --Please procure as a set with micro USB conversion connector --I2C sensor (optional) -Enviro pHAT (around 2,000 yen) is appropriate, but production will be stopped. --It is widely supported for I2C sensors etc. via CircuitPython which is a library provided by adafruit, but it can be used even if it is not a module made by adafruit if the chip and address are the same. --The barometric pressure sensor is compatible with BMP280 / BMP38X / LPS33HW / LPS35HW, the acceleration / gyro / geomagnetic sensor is compatible with LSM303 / LSM6DS / LSM9DS1 / LIS3MDL / BMX160, and the illuminance sensor is compatible with TCS3472 / VCNL4040. The button is button_shim, the power supply is PiJuice, PiJuice Zero is supported. --Setup will be described later. --Exterior related --Topeak's SmartPhone Dry Bag. Recommended per 5 inches. --Slim USB micro cable. The L-shaped type is even better.

skill

--Skills to handle soldering irons ――Speaking of bicycles, it's probably okay if you have the dexterity to change the wires. --Skills that allow you to check the operation of individual parts by referring to the seller's HP (skip appropriately in this text)

Other

--Unbreakable heart --Financial power to buy several sets in case parts are damaged ――Since it is about 1 or 2 of GP5000 (a high-class road bike tire) that everyone loves the above set ...

assembly

The main material is like this. assembly-01.png

--Upper left: PiTFT 2.4 inch --Middle left: Raspberry Pi Zero WH --Lower left: Enviro pHAT --Middle right: GPS module (not introduced above, just in case) --Lower right: ANT + USB dongle and micro USB conversion connector

I will assemble these.

If you find it difficult, we recommend that you first check that the PiTFT works, then check the GPS module, Enviro pHAT, and ANT + USB dongle one by one, and then step by step.

Also, at first, it's better to connect the GPS module to the Enviro pHAT using a jumper wire that is easy to remove from the GPIO pin inside the PiTFT. If you want to make it as compact as possible like the author, remove the extra pins as much as possible and perform complicated and strange aerial wiring, but please note that this kind of magic modification takes time and effort.

assembly-02.png Install the Enviro pHAT and ANT + USB dongle. The Enviro pHAT has a sensor on the printed side, so I think it's better to point it outwards. (If it is inside, it will pick up more heat from the CPU.) Also, insert the ANT + USB dongle into the inside micro USB terminal.

assembly-03.png The Enviro pHAT is directly wired where it is needed. The famous pHAT on the market can tell you which pin you are using on the Raspberry Pi Pinout site.

assembly-04.png Connect a PiTFT 2.4 inch.

assembly.png The PiTFT 2.4 inch has GPIO pins coming out from the side, so you can connect with jumper wires.

It will look like this when it is completely made.

pitft-02.png Each part is fixed with screws. Since the PiTFT button is a type that is pushed from above, if it is not fixed, it will sometimes be pushed too much and the connection with the main body will be cut off, which makes me feel very sad. In addition, the ANT + USB dongle and power cable are fixed with tape to prevent vibration. I haven't had any problems since I arrived at this method. If there is a gap between the connector and the board, insert a suitable cushioning material and fix it with tape.

I want to make my prototype as thin as possible, so I've removed all the outwardly exposed GPIO pins in the PiTFT. (There are two vertical columns between the ANT + dongle and GPS in the lower right of the center of the above photo)

pitft-03.png Moreover, it is soldered directly to Raspberry Pi Zero WH without using a pin socket on the PiTFT side.

Until the first startup

It is assumed that the following is done.

--Raspbian Buster is installed (not Lite) --User is pi --Somehow accessible from PC (via USB or Wifi) --Wifi is even better if you can connect your workplace to the tethering of your smartphone. The latter is insurance when you want to check on the go. --PiTFT is connected and X Window can be displayed.

If you can do it so far, the goal is close.

First, log in with SSH and change some settings with raspi-config.

$ sudo raspi-config

When you reach this point, reboot (`` `sudo reboot```) once.

Next is the update festival.

$ sudo apt update
$ sudo apt upgrade
$ sudo rpi-update
$ sudo apt-get remove piwiz
$ sudo reboot

At this point, there should be no apps that fill the screen on their own at startup. (Because of changing password, initial setting wizard piwiz)

The installation festival starts here. Again, perform the installation procedure performed in "Confirm startup on PC" and the procedure specific to Raspberry Pi.

Bring it from the repository with the git command.

$ cd $HOME
$ git clone https://github.com/hishizuka/pizero_bikecomputer.git
$ cd pizero_bikecomputer
$ cp layouts/layout-cycling.yaml layout.yaml

Install the required packages. Raspbian Buster seems to be included from the beginning except for cython. PyQtGraph has been partially modified, so please install the one in my repository.

$ sudo apt-get install python3-pip cython3 cmake gawk python3-numpy python3-pyqt5
$ sudo apt-get install wiringpi python3-smbus python3-rpi.gpio python3-psutil python3-pil
$ sudo pip3 install oyaml
$ sudo pip3 install git+https://github.com/hishizuka/pyqtgraph.git

(Added on April 30, 2020) Please also install the following to stabilize the sqlite3 connection.

sudo pip3 install git+https://github.com/dashawn888/sqlite3worker#egg=sqlite3worker

Install the backlight control program for PiTFT 2.4 and the shutdown program.

(PiTFT 2.Backlight control program for 4)
$ sudo cp install/usr/local/bin/disable-pitft /usr/local/bin/
$ sudo chmod 755 /usr/local/bin/disable-pitft
$ sudo cp install/usr/local/bin/enable-pitft /usr/local/bin/
$ sudo chmod 755 /usr/local/bin/enable-pitft
$ sudo cp install/etc/systemd/system/disable-pitft.service /etc/systemd/system/

(Program for shutdown)
$ sudo cp install/usr/local/bin/pizero_bikecomputer_shutdown /usr/local/bin/
$ sudo chmod 755 /usr/local/bin/pizero_bikecomputer_shutdown
$ sudo cp install/etc/systemd/system/pizero_bikecomputer_shutdown.service /etc/systemd/system/

(Reflection in service)
$ sudo systemctl daemon-reload
$ sudo systemctl enable disable-pitft.service

Check if it can be started at this point. The first command is a spell that starts on PiTFT.

$ export DISPLAY=:0.0
$ python3 pizero_bikecomputer.py -f

An example of the execution log is shown below. Since Enviro pHAT is connected, the message "True (existence)" is displayed around I2C at the beginning. The first startup will take some time as some compilation for cython will run.

Loading original modules...
	config : 2.125001 sec
  GPS :  False
  ANT :  False
  RPi GPIO :  True
  I2C( ENVIRO ) :  True
  I2C( LSM303D ) :  True
  I2C( BMP280 ) :  True
  SPI :  True
/usr/lib/python3/dist-packages/Cython/Compiler/Main.py:367: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /home/pi/pizero_bikecomputer/modules/logger/cython/crc16_c.pyx
  tree = Parsing.p_module(s, pxd, full_module_name)
	logger_core : 26.252219 sec
	gui_pyqt : 3.164116 sec
Loading modules... done : 31.548674 sec
/proc/device-tree/hat/product : Adafruit PiTFT HAT - 2.4 inch Resistive Touch
/proc/device-tree/hat/vendor : Adafruit Industries
	logger_core : init...
  PiTFT 2.8(r) :  True
	GPS/ANT+/I2C/GPIO/integrate/start: ['0.000380', '0.001742', '0.239424', '0.000563', '0.000490', '0.493737']
	logger_core : setup ambient...: done 9.1e-05 sec
	logger_core : loading course...
	logger_core : loading course...: done 0.011168 sec
	config/logger: ['0.073537', '0.870578']
running in PyQt...
libEGL warning: DRI2: failed to authenticate
calibrated position
Qt version: 5.11.3
['rfkill', '--json']
	pyqt_graph : get_track(init) :  0.005978 sec
  gui_pyqt:
    base          : 1.5448
    widget        : 2.8341
    button        : 0.0854

test-01.png

Thank you very much.

It's very difficult to use if you start it from the terminal every time, so I will make it easy to start from the screen later. Exit with ctrl -c from the terminal.

Additional installation

If you come this far, it's time to take a break!

When using GPS

Install the GPSd service and the package used by python.

$ sudo apt-get install python3-tz gpsd gpsd-clients python3-pyproj
$ sudo pip3 install gps3 timezonefinder
$ sudo cp install/etc/default/gpsd /etc/default/gpsd
$ sudo systemctl restart gpsd

To check if GPSd is running correctly, use the `cgps``` or `gpsmon``` command to check if the value is obtained. If you leave it outdoors for a few minutes, GPS can be supplemented and information such as latitude and longitude will flow.

When using ANT +

As with PyQtGraph, openant has been partially modified, so please install the one from my repository.

$ sudo apt-get install libusb-1.0-0 python3-usb
$ sudo pip3 install git+https://github.com/hishizuka/openant.git

If you can install both GPS and ANT +, the beginning of the execution log will change to True.

Loading original modules...
	config : 2.952061 sec
  GPS :  True
  ANT :  True

Edit the beginning of the [ANT] block in setting.conf without the program (pizero_bikecomputer.py) running.

[ANT]
status = True

Settings around startup

If you come this far, it's time to take a break! (Second time with a smoky smell)

To use it as a small device rather than a desktop computer, either auto-start it or add a boot button that's easy to press.

For the time being, only perform the former automatic startup, and add buttons as needed. If there is no button, the problem is when it freezes in the middle, in which case you have to SSH in from your smartphone and manually type the restart command or restart.

Automatic startup (X Window)

It ends with the following three lines. Buster as of 2020/1 is a prerequisite. It may not be possible if the version etc. changes.

$ mkdir -p ~/.config/lxsession/LXDE-pi
$ cp /etc/xdg/lxsession/LXDE-pi/autostart ~/.config/lxsession/LXDE-pi/
$ echo "@/home/pi/pizero_bikecomputer/exec.sh" >> ~/.config/lxsession/LXDE-pi/autostart

Reference: Raspberry Pi Official Forum https://www.raspberrypi.org/forums/viewtopic.php?t=249620

It is smarter to go directly without going through X, but if you start it directly without going through X with PyQt5 installed by the apt command, the touch panel does not seem to work, so build Qt5 and PyQt5 with tslib enabled. is needed. If you really want to use the touch screen, raise 4GB of Rasberry Pi 4 for compilation. This area will be described in the sequel.

Button added

(Since it is quite long, please skip it if you do not need it)

Since it is impossible to change the settings on the small desktop screen of PiTFT, change the screen settings via VNC.

$ sudo apt install tightvncserver
$ tightvncserver :1

(When quitting tightvncserver)
$ $ tightvncserver -kill :1

After setting the login password, log in with VNC. For Windows, install the VNC viewer (RealVNC etc.). The server name is the same as the one connected by SSH etc., and the port is 5901 version.

macOS is included by default, so press "Connect to Server" (command + k) at the bottom of the Finder's Go menu and log in at vnc: //raspberrypi.local: 5901. vnc.png

vnc-01.png There is no harm in using it at normal resolution, but if you use it at 320x240, everything is superfluous, so I will erase it.

vnc-02.png The 2nd to 4th application launchers from the left are unnecessary, so right-click to remove (3rd from the top).

vnc-03.png Right-click on the top bar to enter Panel Settings.

vnc-04.png Pannel Applets erases unnecessary tray icons on the far right as well as unnecessary ones.

menu-01.png Next, there are too many menus, so delete them as well. Launch the Main Menu Editor in Preferences.

menu-02.png With Applications selected in the upper left, uncheck the check boxes of each menu in the center except Preferences. Next, create an item with the second button New Item from the top right.

menu-03.png Type in the name of the program and press the Browse button next to the Command field.

menu-05.png Select exec.sh in the folder where you placed the program (/ home / pi / pizero_bikecomputer /).

menu-06.png This completes the input. Press OK to return.

menu-07.png The new item will be placed under Preferences, so with the added item selected, press the Move Up button on the far right of the screen to place it at least above Preferences.

menu-08.png With this, the number of menus has decreased, and this program comes out first.

menu-09.png Reboot and check from the screen of the actual machine.

How to use

The hardware buttons are designed to roughly match the software screen. It is as follows in order from the left. (Short press / long press can be changed. Also, only the GPIO number of PiTFT 2.4 is supported. For other models, it needs to be changed in modules / config.py)

--Screen brightness On / Off button. --Wrap button. When stopped, press and hold to reset and output the fit file (changeable) under the log folder. --Screenshot button. Output under screenshot folder. --Start / Stop button. Transition between start and stop states. Press and hold to end the program. --Move between screens with the right button. Press and hold the menu screen

Also, the assignment is changed on the menu screen. The following assignments are made in order from the left.

Thank you very much. Now you can run outside.

Until you start running

** Course file **

If you want to use it, place the TCX format course.tcx file under the course folder. The file name is fixed now. If there is a file at startup, it will be read.

** Mounting on a road bike ** Attach the case to the handlebar and connect it to the battery in the top tube bag. There is no particular switch, so it will start the moment you connect it. Just in case. bike-01.png

After starting It takes a few minutes to supplement GPS, so wait in a place where the sky is open as much as possible. In the meantime, ANT + pairing and altitude correction if necessary.

Press the start button to start recording, press the lap button to measure the section, press the start button again to end recording, and then press and hold the lap button to reset and write the recording. Please retrieve the recorded file via SFTP. I will add the method of uploading to STRAVA in the near future.

in conclusion

end-01.png

What did you think?

We would appreciate it if you could let us know your impressions in the comments section or on Twitter. Especially for those who have tried it, I would like to use it as a reference.

This hobby project is suspicious if it is still at the starting point because we have not implemented many high-impact functions that are not available on the market, but we plan to continue working on it on a daily basis, so if you are interested, please join us.

The documentation hasn't caught up at all, and I'd like to explain how to start it directly without going through X, the MIP color reflective LCD version, and the program, but I'll take this opportunity again.

Recommended Posts

Let's make a cycle computer with Raspberry Pi Zero (W, WH)
Make an umbrella reminder with Raspberry Pi Zero W
Make a wash-drying timer with a Raspberry Pi
Play with the Raspberry Pi Zero WH camera module Part 1
Let's make a breakout with wxPython
Let's make a graph with python! !!
Let's make a supercomputer with xCAT
Using a webcam with Raspberry Pi
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
Make a Kanji display compass with Raspberry Pi and Sense Hat
Let's make an IoT shirt with Lambda, Kinesis, Raspberry Pi [Part 1]
Let's make a shiritori game with Python
Build a Tensorflow environment with Raspberry Pi [2020]
Let's make a voice slowly with Python
Let's make a simple language with PLY 1
Let's make a web framework with Python! (1)
Operate an oscilloscope with a Raspberry Pi
Let's make a tic-tac-toe AI with Pylearn 2
Let's make a Twitter Bot with Python!
Create a car meter with raspberry pi
Let's make a web framework with Python! (2)
Make a wireless LAN Ethernet converter and simple router with Raspberry Pi
[Python + PHP] Make a temperature / humidity / barometric pressure monitor with Raspberry Pi
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
Discord bot with python raspberry pi zero with [Notes]
Let's replace UWSC with Python (5) Let's make a Robot
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
[Let's play with Python] Make a household account book
Let's make a simple game with Python 3 and iPhone
Control brushless motors with GPIOs on Raspberry Pi Zero
Let's do Raspberry Pi?
Let's make dependency management with pip a little easier
Let's make a Mac app with Tkinter and py2app
Let's make a spherical grid with Rhinoceros / Grasshopper / GHPython
DigitalSignage with Raspberry Pi
[Super easy] Let's make a LINE BOT with Python.
A memorandum when making a surveillance camera with Raspberry Pi
Let's operate GPIO of Raspberry Pi with Python CGI
Make a note of what you want to do in the future with Raspberry Pi
Easily make a TweetBot that notifies you of temperature and humidity with Raspberry Pi + DHT11.
I tried to make a motion detection surveillance camera with OpenCV using a WEB camera with Raspberry Pi
Let's make a websocket client with Python. (Access token authentication)
Create a web surveillance camera with Raspberry Pi and OpenCV
Make an air conditioner integrated PC "airpi" with Raspberry Pi 3!
I made a resource monitor for Raspberry Pi with a spreadsheet
getrpimodel: Recognize Raspberry Pi model (A, B, B +, B2, B3, etc) with python
Check! Let's develop Raspberry Pi Zero remotely-Try using Arafruit WebIDE
I made a surveillance camera with my first Raspberry PI.
Creating a temperature / humidity monitor with Raspberry Pi (pigpio version)
Let's make a diagram that can be clicked with IPython
Let's make a Discord Bot.
Mutter plants with Raspberry Pi
Let's make Othello with wxPython
Let's make dice with tkinter
Make a fortune with Python
Let's make a rock-paper-scissors game
Make a fire with kdeplot
How to upload a file to Cloud Storage using Python [Make a fixed point camera with Raspberry PI # 1]
Control the motor with a motor driver using python on Raspberry Pi 3!
Let's access your Raspberry Pi from outside your home with VPN (WireGuard)
Let's make a WEB application for phone book with flask Part 1