The script for the dashcam project has been
completed for the most part. Hello everyone,
this is Frenoy and version 1 of the dashcam
project is almost ready for road testing.
A bunch of things have been added to the script
and what’s left to do is automate it so
that it runs every time the Pi boots. We also
need to have some way of safely shutting down
the Pi once the car ignition is switched OFF.
I will add both these features, place everything
inside a suitable enclosure and take it for
a test drive in next weeks video.
This is part 3 of the dashcam series, so please make sure you watch the other videos using the links
which should be somewhere around here.
We will start this video by taking a look
at the logic behind the different modules  and we will then glance through
the script, so let’s dive
in.
On the hardware side of things, I decided
to use version 1 of the camera module. This
is because it costs much less than version
2 while still being able to output 1080p video.
I also soldered the battery for the GPS module
which was surprisingly difficult to do because
the terminals are originally spot welded to
the battery. It does hold up some charge,
but I’m pretty sure that the battery life
has been reduced due to the excess heat that
I applied during the process. If you are looking
to use double sided tape to mount a GPS module,
then be very careful when removing it or simply
avoid adding it over the battery to be on
the safe side.
Going back to the script: The first thing
we will look at is the video capture. The
picamera library has a parameter called quality,
which can be used to determine the capture
quality. The value can range between 1 to
40, with 1 being the highest quality and 40
being the lowest. I decided to start by using
20 for now as this can be updated later, if
needed. I have also reduced the framerate
from 30 to 25 frames per second as this is
adequate for the application and doing so
will also reduce the video file size by a
bit.
Next we have the file management section which
has been updated to take care of file creation
as well as automatic deletion. I decided to
have continuous file numbering which will
make the deletion a bit easier.
Let’s talk about file addition. In the previous
script, we started from 0 and we sequentially
checked if the file existed. If it did, then
we moved to the next one. If it didn’t then
we created it and used that as the starting
point. This works, but it is a little wasteful
as we need to check all of the file numbers.
The current script saves the newest file number
to a configuration file and this is read the
next time the script is run. Here’s what
the config file looks like and it uses the
JSON format. It simply contains the file number
that was last created. This makes the file
creation process much more efficient.
The configuration file can be used to store
more information, including the definitions
for the resolution, frame rate and so on,
that we have currently hardcoded into the
script. The script limits the total number
of files to 99,999 which should be enough
but you can update this as required. Next
week’s video will also contain a script
that you can use to convert the video files
to the mp4 format and also reset the file
number in the configuration file.
The dashcam script automatically checks if
the config file exists and if it does, then
it uses that for file numbering, if not, it
creates a new one and starts from 0. The script
also checks to see if the videos folder exists,
and if not, it will create it and use that.
This eliminates the need to create a folder
before running the script for the first time.
When the memory card space starts to get full,
we need to delete the old files so that the
script can record continuously. Like before,
the script allows you to configure the trigger
point where it will start to delete old files.
I’ve set it to 80% which means that if more
than 80% of the memory card space is used
then it will call a function to clear up some
space. This function is pretty straightforward.
It simply creates file names starting from
0, and then checks to see if the file exists,
if it does, then it will delete it. This will
repeat for 10 files which should free up the
required space. Once again, this limit can
be adjusted as needed.
I also decided to add an LED to the board,
to provide the status. It blinks while video
is being recorded, and this way, you can simply
look at the LED to obtain its status. If for
some reason the LED stops blinking then there
is most likely something wrong which will
need to be looked at.
One more thing to keep in mind is that you
need to safely shutdown the raspberry pi in
order to prevent corrupting the memory card
or operating system. Here’s an overview
of how I plan on implementing this feature.
Currently, the raspberry pi is directly powered
using the 5V supply from a USB cable. This
will be removed and it will be connected to
a battery charging module which will obtain
power from a single cell lithium battery.
This battery will provide about 3.7-4.2V which
is not enough to power the raspberry pi. I
will then add a DC-DC step up module to bring
this up to 5V, which will be suitable for
the raspberry pi. I will also add a US power
detection circuit which will be made up of
a simple voltage divider. This way, the Raspberry
pi will be able to detect if it is receiving
external power or if it is running from the
battery.
The battery only needs to provide power for
a few seconds, which is enough to safely shutdown
the raspberry pi. The board also has a RUN
pin which can be used to reboot the board
and we will have to figure out a way to reboot
it once USB power has been restored.
This is the DC to DC step-up module that I
have ordered for this project. It hasn’t
arrived it but it should be here this week.
This particular board can take an input voltage
as low as 2V and the output voltage can be
set by adjusting the onboard potentiometer.
It also has a USB port that can be used to
supply the input voltage, but we will not
be using it for this project. The output from
the battery charger module will be connected
to the input of this board. And the final,
5V output from this board, will then be connected
to the Raspberry Pi. Be sure to use a module
that can provide at least 1A of current though
the actual current draw should be much less
than that.
I will test and add this feature in the final
version which will be released next week but
for now, I decided to add a switch which can
be pressed to manually shutdown the dashcam
when the script is running.
And that is pretty much it for the logic behind
everything. Here’s what the script looks
like and it is actually not that complicated.
We start out with the definitions and then
the functions to take care of freeing up space.
As mentioned, we check the amount of space
that is being used and if we need to free
up space, then we start with 0 and recursively
check to see if the file exists. If it does,
we delete it and repeat this process a total
of 10 times. The deleted file names are also
printed to the serial terminal.
The script then sets up the GPIO pins, one
as an output, for the LED and another as an
input for the switch. It then tries to open
the config file and obtain the file number
and if it doesn’t find the file, it then
creates a new one and starts from 0. This
section checks the videos folder and creates
it if needed. Finally, we start with the actual
recording.
The file number is stored to the config file,
the file name is printed to the serial terminal
and we then set the timeout or duration which
is 60 seconds by default. While the camera
is recording to the file, we read data from
the GPS module, parse it and add it to the
video as an overlay. We also toggle the LED,
check the free space and check the status
of the switch. If the switch has been pressed,
then we stop the recording and shutdown the
board immediately. If not, we sleep for about
20ms and then check the GPS data again. This
loop continues until 99,999 files have been
created.
Here’s what the hardware looks like. We
have the status LED, shutdown switch and v1
of the camera module. The GPS modules is the same one we used in the last video.
You can use this reference
wiring diagram if needed. The script uses
the internal GPIO pull resistor for the switch
and this eliminates the need to add an external
one.
Let’s now run the script to see it in action.
Simply copy it to your raspberry pi first
and I will be doing that using FTP and FileZilla.
I will clear up the old scripts and also delete
the video folder so that you can see those
new features in action. To run the script,
you will first need to access the terminal
and I will be using SSH. I decided to reduce
the video file capture duration to 5 seconds
just to speed up the file creation process
but you do not have to do this. The script
can be run using the following command.
As you can see, the board will start by creating
the config file, followed by the videos folder
and it will then start creating the video
files. I will speed up this section to let
it create 20 video files and I will then stop
the script by pressing the CTRL and C keys
togethers. Now, let’s test the file deletion
feature. First, we need to know how much space
is currently being used. That can be done
by running the following command. In order
to trigger the file deletion function, we
would need to reduce the space limit to something
lower than the value shown. Since the space
used is more than the limit that we have set,
the script will trigger the file deletion
function. The script can be updated like so.
Now, if we re run the script, then it should
start deleting files to clear up the memory.
It will then continue recording new video
files and this can also be seen in the actual
file structure.
Lastly, we can shutdown the board by pressing
the switch, like so. The board will then shutdown
in a few seconds. And that is the update for
this video. Next week’s video should be
the last one for version 1 of the dashcam
so do subscribe to stay updated. Thank you
for watching and I will see you in the next
one.
