How I programmed the delivery shuttle | How to build a delivery shuttle Part 1

By | March 30, 2023

So the code isn’t extremely advanced, as I didn’t   have much time left for the programming 
but I think it’s pretty comprehensible. I coded everything in Visual Studio Code with 
the PlatformIO Plugin. It works pretty well,   you can organise everything easily 
and you can commit to GitHub directly. Another thing I love is the autocomplete 
feature and the debugging mode. It’s also   easier to structure your code. 
And they have a dark mode (meme) But before we start, I would like 
to thank the sponsor of this video,   PCBWay.

Now you can sure remember from the 
last video, about the delivery shuttle,   how I ended the project with a remaining debt 
of about 134 swiss francs. After publishing the   video, PCBWay contacted me and financially 
contributed to the project. On pcbway.com,   you can order your custom PCBs, assemble them 
and so on. But what I think is actually pretty   interesting is the CNC-machining. This would 
have been pretty useful for the steering. But without any further ado, here is the code. Alright, structure is very important,   therefore I worked with a lot of header-files 
and I have tried to outsource as many functions   as possible and only have the most 
important processes in the main file.

So Arduino Code is like a simplified version of 
C++ and actually pretty pretty straight forward. A simple sketch for example consists of a few 
elements. Initially, the variables are defined.  Then there is the void-loop, where settings 
are configured for the microcontroller.   Setup is only executed once. Here 
you define whether the connected   parts are sensors or actuators.
And then there is the: void-loop. Everything else is placed in this function. Back to the shuttle. What are the Key concepts, 
that need to be controlled. We have the Steering   in the front and back, which basically consists 
of two linear motors. We have four hub motors   which have parameters such as: the direction, the 
speed, the brakes and we also have the 6 distance   sensors, which need to fetch some data. 
There is the Remote, which has a maximum   of 10 Channels. Also there are the headlights in 
the front, the three led strips and the alarm. There are also some other 
sensors such as, the MPU-,   the voltage and the current sensor, 
which weren’t programmed at all.

And that’s it! Let’s start with main.cpp file. In the 
beginning I included all the header-files,   the libraries and external files, 
that I created and wanted to use. The next thing are the 3 Adafruit Neopixels. And   after that I placed the code 
for the distance sensors. The next thing is the setup loop, which 
defines all the Pins, most of them are Outputs. After that, the “IbusRc” gets 
initialised. Which is used for   the communication with the RC-Remote. For 
debugging purposes I opened a serial monitor. To set the motor speed, the Motorcontroller 
needs to be controlled with an potentiometer.   For this I used a digital potentiometer, 
which is controlled with the SPI-protocol. The only thing I couldn’t exclude from 
the main file, were the led-lights. I   had some problems with the Neopixel 
library. But more about that, later. So the Led lights have three modes. They can 
be switched on with the front strip in white,   the back strip red or off or blinking in 
orange. Why only three modes? The shuttle   is currently controlled with a remote, 
which only has 10 channels the light is   controlled with this 3way switch. 
Which data is stored in Channel 6.

The same thing for the alarm,   which value is stored in channel 8. 
This button can turn it on or off. But wait, here it says channel 7. Why is that? Let’s head over to remote.cpp. here you can see 
how the channel values are converted and as I   have 10 channels, I made a loop to request all 
data and save it in an array. Sometimes I used   English words and sometimes I used German 
words and I kinda mixed them all together,   but I think it should still be comprehensible 
enough. The array counting starts with 0,   so channel 1 is assigned to 
the first spot in the array. In remote.h all the used variables 
are declared and initialised. The speed of the motors is controlled 
with a potentiometer, which means,   currently all motors are controlled with 
the same potentiometer.

The speed can be   controlled with this joystick or throttle here. The value from the remote control, 
gets converted to an analog value   from 0 to 255 and it gets transferred to the 
digital potentiometer with the SPI-protocol. The two other functions, set_direction and   set_brakes, both open or close an 
optocoupler on the motherboard. hubmotor.h fulfils the same function as remote.h. The linearmotors can either be opened or closed 
or off. This is achieved with switching the   power supply. That’s exactly what linearmotor.cpp 
does but three times, as we have 3 linear motors. The only thing left are the distance 
sensors, which are work exactly like   the well-known hc-04 distance sensor, except 
that the ones I used are actually waterproof. I again included all the header files in 
the beginning and you have to include every   library again if you’re splitting your code 
into different files. As I’m using 5 Sensor,   I thought is was smart to just program a 
class named Distance sensor, define all   the variables and add a string to later return 
the value of every sensor.

In the main file,   a new class instance is created for each sensor. 
In the end you just get less code and it works   somewhat more reliable. The “checkdistance” 
function works on the same principle as the HC-04. And that’s basically everything, 
now back to the main.cpp file.   Let’s look at the void loop function.
It basically gets the data from the   transmitter saves it to the array. Then it sets 
the light, the speed, the steering system in   the front and the one in the back. It either 
keeps the cargo bay door closed or opens it,   sets the brakes, the alarm and corrects, 
if necessary the direction of the motors.

The whole thing works pretty well, 
except the Neopixel library. If code   for the lights is executed, the inputs from 
the remote are delayed and the functions use   the data which has already been stored 
in the arrays from the previous call.   The delay lasts up to two seconds 
and I haven’t found a solution yet,   but once the Led-Strips are excluded from 
the code, everything else works just fine. There is still one thing that I didn’t mention 
and that’s the PINOUT file. The Arduino Mega has   54 Digital and 16 analog Pins. Of course, in 
the software it must be defined at which pin,   which device is connected. So the file consists 
only of assignments and some global variables. Luca: *slaps keyboard* Kerim: “So?” Luca: “This is ****” Kerim: “And he is totally right. It is incredibly 
hard to make a programming video targeting people,   who never programmed before. Therefore I decided 
to make a small powerpoint style animation,   showing the correlation between the remote 
and the car. But if you did understand,   what I was talking about before, 
you can just skip to this timestamp: Welcome to your most basic PowerPoint animation. 
What I was basically trying to tell you during   this whole video is, that I have about 5 different 
categories of parts.

The hub, and the linear   motors, the lights and the other part and 
let’s not forget the remote. They more or   less represent the program structures. Let’s look 
at the hubmotors, the code exists only to control   certain functions and variables. In Hubmotors, the 
things that can be controlled are the direction,   the brakes and the speed. Same thing goes for 
the linearmotors, where the 3 different motors   can be controlled. we have the steering 
in the front, the back and the cargo bay. And let’s not forget the lights. the code 
defines how what is to be controlled. It   is so to speak written down in the 
code how to control the headlights   and the led strips. The same goes for 
all the other parts. Such as the alarm,   the distance sensors and of course 
all the un-programmed parts. Via 10 channels, the individual 
functions can be controlled. And the hierarchy looks like 
this. The user, moves a joystick,   or pushes a button on the remote, and 
the remote sets the different values. But before that we have the Arduino, that 
interprets the values from the RC-Receiver.

So this is the code for now, for the Delivery 
shuttle. You may say it’s just a giant RC-Car,   but theoretically… the functions 
are implemented in such a way,   that they can be used without the remote. The link to the Github can be 
found in the video description. Thank you all for watching and I 
hope I could resolve all questions..

As found on YouTube