Peter1999 profile picture Peter1999 2 years ago 3
Blog

RC Viper ACR - Basic of RPI, Alpha Version

RC Viper ACR - Basic of RPI, Alpha Version - Blog

The second part of RC Viper ACR Technologies & Systems is focused on basics of RPI, communication between RPI and the client, and shows real codes from the Alpha version.

Install the software on RPI

As a first thing, OS has to be installed on RPI. The used operating system is a modified version of Linux, Raspbian Jessie. We can obtain this operating system on the RPI site in the download section, at https://www.raspberrypi.org/downloads/. Then we needed copy that downloaded system to an SD card, which was inserted into the device.

We used Win32imager to upload the operating system to the micro SD card. The operating system must be in the .IMG format. We downloaded the program from the original manufacturer’s site at: http://sourceforge.net/projects/win32diskimager/files/Archive/.

RC Viper ACR - Basic of RPI, Alpha Version - Blog

In the Win32 Disk Imager program, we chose the location of the .IMG file with the operating system in the top row and select, where we wanted to write this OS. After selecting, we pressed “write” and saw the progress bar as writing. Once it was complete, the program made a successful sign-up report and we could close it. We inserted the card into the RPI and connected it to the power supply.

A keyboard, a mouse and a screen are needed for setup the RPI, as you need to enable SSH for remote access and set up a wifi module. Raspbian Jessie has a graphical interface, so there is no problem connecting the wifi module to the network, just click on the wifi icon and select the network, the setting as the Access Point is more complicated, but in the final version it is not used, so we skip this step in the documentation.

When we connected wifi, we could activate SSH. We opened a command prompt and typed into it sudo raspi-config. We chose Interfacing options and found ssh, pressed enter and selected Enable ssh server.

RC Viper ACR - Basic of RPI, Alpha Version - Blog

Subsequently, we could download and install the necessary programs directly from RPI or from a PC via SSH, namely PubNub, Python and Apache 2. Libraries like RPI GPIO that we used in the programs are pre-installed in Raspbiane Jessie, so we needed to install the server and update python. (Be sure to use pip install ‘pubnub = 3, <4’ since 2017).

RC Viper ACR - Basic of RPI, Alpha Version - Blog

We did not program directly on RPI but on a computer in Sublime Text 2, so after installing, we could turn RPI off and put it in the car.

Communication between the model and end devices

In order to ensure compatibility with various devices, we have decided to go through the web application. RPI in models works as a server, our mobile, tablet or laptop is a client. In the RPI run scripts written in Python, a browser page was created in HTML, CSS, and JavaScript. The communication between the web and Python is provided by PubNub.

PubNub is a cloud-based chat service, in this case it sends messages between JavaScript and Python in real time without the need to refresh the page. Because it is a cloud service, it needs an Internet connection. The original idea was that RPI would be an access point, so we were forced to create a script that would behave like a server-side cloud and send messages to static addresses. Later we realized that if we use a smartphone as an access point with mobile internet and leave RPI as a wifi client, it will be possible to control the car from anywhere in the world. This is a welcome option, if we bought a real car with a similar system, it is better for us if the manufacturer can repair or improve the software remotely.

PubNub, Alpha version of the program

RC Viper ACR - Basic of RPI, Alpha Version - Blog

Before we started programming a complex web application, we learned how PubNub works and we used it in the code of one of the Alpha versions. PubNub has two basic features - Publish and Subscribe. In the picture, we can see the script in which JavaScript sends a message via the ‘chatChannel’ channel, which catch another JavaScript, and write it to the console when it is received.
Then we implemented these messages into a simple Alpha version that served to verify component functionality along with PubNub. We had several checkboxes, a slider, and a dropdown menu.

If we look at JavaScript, we see a few publish features that launch when you click on the relevant element of the page to send it to the server. The individual parts are written as variables; for example, we can look at the checkbox lights in detail, in the code is written as a checkbox2, after click is his value as x, and it will be sent to the channel „channel“. This channel takes the script to the server, what we will see in the Python section. In this code, you still need to note the publish and subscribe key, which serves to identify you in the PubNub cloud. Messages are only sent between devices with the same keys.

Data, which JavaScript sends, passes through the cloud to the server, where they are captured by the python script. Python has a little bit different PubNub entry, but the essence is the same as for JavaScript, subscribe a particular channel when it picks up the message, and starts a function that evaluates it. We can notice key entry, PubNub import, import RPI GPIO, which is a GPIO pin control library, GPIO pin numbering according to board ordering and GPIO cleaning at the end of the program.

Let’s focus on the lights again. We have channel1 here defined as “lights”. This channel is subscribed and lights_callback starts when the message is received. The message is checked, depending on whether the checkbox has been checked or not, the GPIO pin 19 is set to output and then turns on or off the LEDs. GPIO pins can be used to either read or generate a stable or PWM signal. We now use GPIO pin 19 to supply LEDs, we set the logic 1 or 0 at the output, otherwise we control whether the current flows through the diode or not.

If we look at the servo slider, this is a little more complicated. Slider sends numbers even with decimal places, checks which mode we have selected, we can manually control the servos only in manual mode, it creates a variable pwm in which is defined that GPIO pin 22 is set to output, then calculates the angle of rotation, triggers the PWM on the GPIO 22 at 100Hz, changes the servo angle and turns off the PWM.

The last thing we look at here is reading the sensor. First, we define GPIO 18 as active input so that it better detects input drops, ie logic 0. If the value of GPIO 18 changes, the my_callback function will be triggered to check the state of the air brake, if it is on, outputs to the servo will be 150, which is an vertical spoiler position (airbrake up). If it is not, it detects the press, which we will use later in the MGU-K, when we want the recuperation to take place even without the activated air brake.

That is all for today, and little advice at the end - if you would build model like this, I recommend RPI ZERO W with built-in WIFI, I used ZERO v1.3.