Raspberry Pi RC Transmitter Phase 5 – Saving configuration file

Advertisements

Now that I can modify all the variables in the transmitter in the field I need to be able to make the changes persistent. At the moment the changes to any variables are not saved when the transmitter is powered off. I will need to write the changes to a seperate file that is read at start-up. This part turned out to be easier than I thought it was going to be.

A quick Google found what I needed is Pythons Pickle command. The pickle command writes the variables one at a time to an external file so they can be recalled later. I am going to do the opposite and recall the variables at the start of the program and write the variables to the file whenever they are changed. This means that whenever the program starts it will load the latest configuration and continue right where it was left off.

The problem with loading all of my variables from a file at the start of the program is that I need to have a file ready before I start the program the first time. So the first thing I did was add some code to my program that writes all the variables to a new file at the top of my program.

with open('config.pickle', 'wb') as f:
    pickle.dump(reverse_channel, f)
    pickle.dump(centre, f)
    pickle.dump(upper_end_point, f)
    pickle.dump(lower_end_point, f)
    pickle.dump(expo, f)
    pickle.dump(channel_assignments, f)

I ran this once to generate a file with my default variables. After I confirmed that the config.pickle file existed I then cut and pasted this code to the end of the pygame event handler section so everytime the D-pad is pressed the file is immediately updated. Then I added new code at the beginning of the program that reads all the variables from the file one at a time.

with open('config.pickle', 'rb') as f:
    reverse_channel = pickle.load(f)
    centre = pickle.load(f)
    upper_end_point = pickle.load(f)
    lower_end_point = pickle.load(f)
    expo = pickle.load(f)
    channel_assignment = pickle.load(f)

So now I have persistent changes to the configuration I need to finish the configuration screens on the transmitter to allow changes to the channel assignments because I didn’t do it before. After that I was thinking it would be nice to have selectable config files to be able to save and restore multiple configurations depending on what robot the transmitter is currently controlling