Reader small image

You're reading from  Learn Robotics Programming - Second Edition

Product typeBook
Published inFeb 2021
PublisherPackt
ISBN-139781839218804
Edition2nd Edition
Concepts
Right arrow
Author (1)
Danny Staple
Danny Staple
author image
Danny Staple

Danny Staple builds robots and gadgets as a hobbyist, makes videos about his work with robots, and attends community events such as PiWars and Arduino Day. He has been a professional Python programmer, later moving into DevOps, since 2009, and a software engineer since 2000. He has worked with embedded systems, including embedded Linux systems, throughout the majority of his career. He has been a mentor at a local CoderDojo, where he taught how to code with Python. He has run Lego Robotics clubs with Mindstorms. He has also developed Bounce!, a visual programming language targeted at teaching code using the NodeMCU IoT platform. The robots he has built with his children include TankBot, SkittleBot (now the Pi Wars robot), ArmBot, and SpiderBot.
Read more about Danny Staple

Right arrow

Chapter 5: Backing Up the Code with Git and SD Card Copies

As you create and customize the code for your robot, you will invest many hours in getting it to do awesome things that, unless you take precautions, could all suddenly disappear. The programs are not the whole story, as you've already started configuring Raspberry Pi OS for use on the robot. You want to keep your code and config in case of disaster, and to be able to go back if you make changes you regret.

This chapter will help you understand how exactly code or configuration can break and the disasters you might face while customizing code for your robot. We'll then take a look at three strategies for preventing this.

In this chapter, you will learn about the following:

  • Understanding how code can be broken or lost
  • Strategy 1 – Keeping the code on a PC and uploading it
  • Strategy 2 – Using Git to go back in time
  • Strategy 3 – Making SD card backups

Technical requirements

For this chapter, you will require the following:

  • The Raspberry Pi and the SD card you prepared in the previous chapter
  • The USB power supply and cable you used with the Pi
  • A Windows, Linux, or macOS computer or laptop, connected to the internet and able to read/write to SD cards
  • Software: FileZilla and Git
  • On Windows: Win32DiskImager

Here is the GitHub link for the code files of this chapter:

https://github.com/PacktPublishing/Learn-Robotics-Fundamentals-of-Robotics-Programming/tree/master/chapter5

Check out the following video to see the Code in Action: https://bit.ly/3bAm94l

Understanding how code can be broken or lost

Code and its close cousin, configuration, take time and hard work. Code needs configuration to run, such as Raspberry Pi OS configuration, extra software, and necessary data files. Both need research and learning and to be designed, made, tested, and debugged.

Many bad situations can lead to the loss of code. These have happened to me a week before taking robots to a show after weeks of work, and I learned the hard way to take this quite seriously. So, what can happen to your code?

SD card data loss and corruption

SD card corruption is when the data on the SD card used to hold your code, Raspberry Pi OS, and anything you've prepared on it gets broken. Files become unreadable, or the card becomes unusable. The information on the SD card can be permanently lost.

If a Raspberry Pi unexpectedly loses power, the SD card can be damaged, causing data loss. A hot Pi can slowly bake an SD card, damaging it. Visual processing on a...

Strategy 1 – Keeping the code on a PC and uploading it

Secure File Transfer Protocol (SFTP) lets you transfer files from a computer to a Pi. This strategy enables you to write code on your computer, then upload it to the Raspberry Pi. You can choose your editor and have the safety of more than one copy.

Important note

But wait – which editor? Editing code requires software designed for this purpose. Recommendations for Python are Mu, Microsoft VS Code, Notepad++, and PyCharm.

SFTP uses SSH to copy files to and from the Raspberry Pi over the network. So, let's see how to do it:

  1. First, make yourself a folder on the PC to store your robot code in; for example, my_robot_project.
  2. Inside that folder, make a test file, using your editor, that will just print a bit of text. Put this code into a file named hello.py:
    print("Raspberry Pi is alive")
  3. We will copy this to the robot and run it. You can make the copy using the SFTP tool FileZilla...

Strategy 2 – Using Git to go back in time

Git is a popular form of source control, a way to keep a history of changes you've made to code. You can go back through changes, see what they were, restore older versions, and keep a commented log of why you made the changes. Git also lets you store code in more than one location in case your hard drive fails. Git stores code and its history in repositories, or repos. In Git, you can make branches, copies of the whole set of code, to try ideas in parallel with your code, and later merge those back to the main branch.

I will get you started, but this section can only scratch the surface of what you can do with Git. Let's begin:

  1. Install Git, by following the instructions at https://git-scm.com/book/en/v2/Getting-Started-Installing-Git for your computer.

    Tip

    If you are using Windows or macOS, I would suggest using the GitHub app for easier setup.

  2. Git requires you to set your identity using a command line on your...

Strategy 3 – Making SD card backups

Git and SFTP are great for keeping code safe, but they don't help you reinstall and reconfigure Raspberry Pi OS on a card. The procedures for Windows, Linux, and macOS are quite different for this. The basic idea is to insert the SD card and use a tool to clone the whole card to a file known as an image, which you can restore with balenaEtcher when you need recovery.

Important note

You should only restore images to cards of the same size or larger. Putting an image on a smaller device is likely to fail to write, creating a corrupt SD card.

Before we begin, properly shut down your Raspberry Pi, take out its SD card, and put that into your computer. These clean images are large, so do not put them in your Git repository. It's beyond the scope of this chapter, but I recommend finding a way to compress these files as they are mostly empty right now. In all cases, expect this operation to take 20-30 minutes due to the image...

Summary

In this chapter, you have learned how to look after your code and configuration. You have seen how things can go wrong, and the strategies to protect your work from them. You have a starting point with Git, SFTP, and SD card backups that you can use together to be a bit more experimental and fearless about changes to your robot. You can use SFTP to edit on your computer, giving you at least one copy other than the code on your robot and letting you use powerful editors. You can use Git to go back in time, so you can wind back from mistakes and experiments, or just see the differences. You can use SD card backups to get a complete image of the storage your Raspberry Pi is using, and restore it if it goes wrong.

In the next chapter, we'll start to build a basic robot. We'll assemble the robot chassis with motors and wheels, determine what power systems to use, then test fit the overall shape of our robot. Bring a screwdriver!

Assessment

  • Try creating a file on your computer – a simple image or text. Try using SFTP to send it to the Raspberry Pi, then, using PuTTY, see if you can list the file with the ls command. The file could be a simple Python script, which you could try running on the Raspberry Pi.
  • Make a change that is incorrect to hello.py. Use diff to see the difference. Use Git resources (see the Further reading section) to find out how to return this to how it was before the change.
  • Make a backup of your Raspberry Pi SD card using the preceding instructions, make some changes to the data in /home/pi, then restore the image using balenaEtcher. You could even restore your backup to another SD card, and plug it into the Raspberry Pi as if it was the original.
  • I recommend finding out more about how Git can be used to look after your code, and even as a method of getting code onto the Raspberry Pi. Use the Further reading section to find out more about Git, and ways to work it...

Further reading

Please refer to the following for more information:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learn Robotics Programming - Second Edition
Published in: Feb 2021Publisher: PacktISBN-13: 9781839218804
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Danny Staple

Danny Staple builds robots and gadgets as a hobbyist, makes videos about his work with robots, and attends community events such as PiWars and Arduino Day. He has been a professional Python programmer, later moving into DevOps, since 2009, and a software engineer since 2000. He has worked with embedded systems, including embedded Linux systems, throughout the majority of his career. He has been a mentor at a local CoderDojo, where he taught how to code with Python. He has run Lego Robotics clubs with Mindstorms. He has also developed Bounce!, a visual programming language targeted at teaching code using the NodeMCU IoT platform. The robots he has built with his children include TankBot, SkittleBot (now the Pi Wars robot), ArmBot, and SpiderBot.
Read more about Danny Staple