Wednesday, May 19, 2021

Adding Sensors

Overview

The next step was to integrate all sensors and the SD card reader onto the microcontroller so that both sensor and receiver data could be recorded in flight for offline use in developing filters and models. In this post, I'll go over the details of selecting and integrating these external pieces.





Sensors

My criteria for all sensors were as follows:
  • Affordable
  • Well documented and supported
  • Communicate over I2C or SPI (ideally I2C)
  • Bonus: Have Qwiic connector attachments
The bonus here was initially not important to me until I realized how much I appreciated the simplicity and security of the qwiic connectors. While setting up sensors, I had a bug that I chased down for a while that was stopping consistent IMU readings. Turns out it was due to the only I2C connection I had that wasn't qwiic. Since then, I've used qwiic exclusively.

GPS

The GPS I chose was the basic Sparkfun GPS. This product hit all of my criteria and didn't require an external antenna. My main concern was that it wouldn't be accurate enough but so far it has impressed me. Specifics of error will be discussed in another post.

The GPS had extensive libraries available. Though its maximum operating frequency was below the 20Hz number I was initially targeting (some documentation said 18Hz but others varied slightly), I found that there was a mode that allowed the microcontroller to fetch whatever data was most recent, regardless of it was fresh or not. By taking the most recent data at every step of the loop I was able to simplify the logic in the filter to not have to switch between cases with or without GPS sensor measurements. However, this method was being slightly dishonest with the filter so I raised the 1 sigma error on the GPS measurements to approximately account for it. There are more precise and elegant solutions to this problem but this method was the simplest and worked for now.

IMU

I chose the basic Sparkfun IMU for mostly the same reasons as I did the GPS. Unlike the GPS, the IMU had an operating frequency of 100Khz, substantially faster than my main loop speed, 20Hz. This meant I did not have to worry about having fresh data. The examples attached to this sensor made its integration very simple, the first one I actually added.


Barometer

I purchased the Barometer after my first flight with the sensors saving data. Since the GPS gives a poor estimate of altitude, I wanted another measurement of height. However, I neglected to properly read the documentation for the Barometer I picked. The highest frequency I was able to get out of was about 1Hz. This necessitated that the filter had measurement functions to deal with measurements both with and without barometer data.

SD Card

In order to record all of the measurement data and receiver data, I needed some form of non-volatile memory. Though the Arduino does have some options involving the EEPROM, I decided that an external memory source would be simpler and more effective. I purchased both an Micro SD card reader and an FRAM breakout board. I ended up using the SD card reader because I preferred the ease of popping in and out the SD card to transfer the data rather than connecting the microcontroller to the computer. I am aware of the issues the SD card might have with acceleration or shocks and am prepared to switch to the FRAM if that becomes an issue. Eventually, I intend to design a PCB to take care of all of the sensors and connections, in which case I will likely use the FRAM as well.

New Microcontroller

At this time I was still using the Arduino Uno. As I began adding the sensors to the code base and running them on the board I immediately noticed that my Arduino flash memory was nearly filled by the GPS object. Rather than working to optimize memory, I instead took the more time-efficient route of moving to the Arduino Mega which had 8x as much flash memory as the Uno. Moving to the Mega also gave me access to longer pin change interrupt vectors, allowing 6 full channels of input read and output write PWM signals. 

In addition, I took this opportunity to clean up and secure all connections with a solder board shield over the Mega. Below is the general schematic I used:

 
Overview of solder board shield for Arduino Mega


The first main feature of this board is the 6 aligned PWM connectors on the left side for easy and secure connection to different servos. The I2C connection is on the same side, allowing that side of the board to point to the servos and sensors on the inside of the aircraft. The main safety feature is the independent throttle connection. The signal pin on this connection is connected in series to the receiver pin for the throttle. This means that the throttle can only be controlled manually but the outputs can still be recorded.


Lessons Learned and Next Steps

This process was the most hardware integration that I've had to deal with or at least deal with alone. There were a lot of small bugs and issues that came up along the way that I had to work through but it got easier and easier as I added more and more sensors and came to know what to expect. 

I especially appreciated designing the solder board shield. I knew what I'd need as both the pilot and the person writing the controls code so I was able to account for all of that in my design. This is not to say that there are no mistakes. There are many things that I will do differently in the future. But for now, I've created a consistent enough hardware system to be trusted in flight and I'm happy with that.



No comments:

Post a Comment