After taking my first Controls Theory class in undergrad, I wanted to actually implement my own controllers and apply them to robotics. I enjoyed the theory, but I knew making that practical would require knowledge and experience I would not otherwise have if I didn't take the time to build it. That, coupled with the fact that I wanted to write my own basic library for the PIC used in my school's microcontrollers class, led me to this project. It is my attempt to create and experiment with a PID controller implemented with a dsPIC33EP128GP502.
Although it would have been quicker in some respects to program the PID controller by setting the correct Special Function Registers to the desired values, part of my goal was to develop a useful library of code that I could use later on for future projects. Before I started on anything too complicated, I wanted to have code to manipulate the GPIO pins.
I found an article (one I can't seem to find now...) describing a technique to place the same types of SFR in an array. Just index for the desired SFR to access it. For example, here is the array with pointers to the TRISx registers that manipulate whether the pin is an input or an output.
To access any specific register, all that is needed to be done is to index it from an array, like so:
*(TRIS_ARRY[port]) |= (0x1 << pin);
This also adds adaptability to the code. If I decided to use another PIC uC, it isn't difficult to add extra special function registers it might have to the array.
Once this was done, I had a pattern I could use to develop the rest of the code, and I could start to focus on sections that the PID controller was directly dependent on.
The first steps were to decide on a development environment to use, and then getting the PIC up and running. I knew I did not want to use MPLABX IDE as I always felt like it was slow, and cludgy. I liked the idea of using a basic text editor with some supporting framework, feeling that it would not let fancy IDE's do all the hard work of setup and configuration. After doing some research, I eventually settled on using Ceedling. I chose it because it aided in Test Driven Development (TDD), something I've grown quite fond of over time after seeing the benefits first hand.
After some days of tweaking and fiddling, I was able to get a basic program running to blink an LED using Ceedling as the build environment. I eventually settled on a pattern of testing my code using the simulator that comes with the MPLABX IDE, and running the code on the hardware periodically to make sure it worked on target as well. (Now I wish I had always tested in hardware as some of the SFR's in the simulator do not seem to be able to be set, which made things annoying to say the least. )
The next steps were to write up some functions to manipulate the GPIO.