Tuesday, October 8, 2013

Hooray some code

Well I finally got the directory to be uploaded to github https://github.com/arduic/RC_Car So while there is a bit of comments on the github page here I'm going to try an explain some of the things I learned while doing this so others don't go through the same pain.

Since heartBeat.cpp is what I promised last time I'll explain this first.

1. In the beginning there are 2 commented out functions. This was my original idea on how to create a timed event using a command called alarm which throughs a SIGALARM which I told emergencyShutDown to catch whenever this alarm was thrown. Thing is this was an unnecessary repetition of another idea so I later scrapped the idea.

2. I have two loops running loopHeart and loopKiller which while very similar are very different. One handles the heartbeat that needs to be constantly fed and the other handles the stop command which will be fed one time and kill immediately. The threads for these commands are setup in main in the commented out section at the bottom. The thread creation itself is fairly self explanatory the only important part is the passing of void pointers which are explained better latter on.

3. Void pointers. So these are really cool, what they do is allow you to pass data to programs that have no way of communicating (eg multiple threads). Thing is most tutorials for these use C but I'm writting C++ files. When using C you don't need to type cast it assumes your saving data of the same types, in C++ you have to cast the data to make them the same type.

4. Poll. Here is how you actually setup a "interrupt" for the BBB. As best as I can see. Poll is a funny command because people sometimes claim it is non-blocking but in almost every piece of documentation I can find it is blocking. If ANYONE can find a non-blocking approach to handeling GPIO interrupts I would be happy to use them.

5. Yes I know the shutdown does nothing but print right now. To that I say, one can not shutdown an RC Car in code until they actually know how to do this. Also strange is the fact that the way I'm handling poll appears to hint itself towards a non-blocking approach because one kill will usually come up with 2-4 kills instead. Not sure how to fix this but seeing as it likely won't cause any issues who cares.

pwmController.cpp I know I didn't say I had this but well I finished it. For the most part, there are still some bugs and these need to be worked out.

1. Make sure to properly read the crap your typing. I spent a lot of time wondering why I couldn't write to a file I had permissions for. Well when you open it as read only that can cause an issue.

2. I should be using fopen and fwrite instead of open and write. Why do I switch between them you ask? They work interchangeably and I don't plan on being hacked any time soon so security not a big priority on a machine with no wireless capabilities.

3.  snprintf is a pretty cool command. It loads the formatted string into a buffer, and returns the size of the string put into the buffer without printing anything.

4. As a large early comment states the pwm controllers are exported with a .13 .14 etc directory extension. This directory appears to be soley based on what order they were exported in. The only clean why I can think to handle this is with another input to functions which is a bit dirty but what can you do. I could have a 2D array but that's unnecessarily complicated, or I could use a reference file but I can't find one that defines these extensions.

5. Unlike the GPIO's which have a driver it appears that the PWM's have an inital driver which I just need to bring to user space.

Speaking of drivers I should really explain that this needs to be made in order for GPIO's to be brought to user space. This file can be found in my src directory it's extension is .dts

1. As the initial comments suggest I modified some of derek molloy's code here. Infact I didn't change a single comment or add my own seeing as the code is very self explanatory beyond the inital learning process. I highly suggest watching his video on device tree's in order to understand what is going on here.

2. While the driver can do a lot it's extremely complicated and I opted for the user space tactic. (If I wanted more efficient code which I eventually will I should be doing more in the kernel/drivers)

Lastly is the trackingTest.cpp file. This file is beyond version 0.1 it's more like 0.000001 but it's a start. I've had a lot of ideas on how to do image processing such as heuristic approaches and edge detection but this one appears to be the most functional. I got the original idea HERE and modified it heavily to be more embedded.

1. openCV is extremly strange and finicky but eventually it does what you want just have to be patient.

2. While I can get eclipse to use arm compilers I cannot get eclipse to use the arm version of openCV, I've infact messed up openCV so much as to make it difficult just to get a clean install on my machine. I plan on instead passing all the cpp and hpp files onto the BBB which while do the compiling. Not really a big deal just a bit slower.

3. This is completely commented out because of 2. Eclipse refuses to compile the rest of my project as long as opencv is in the code. While it should be able to use the intel version I broke that so for now the solution is comments which are removed whenever the user wants to work with openCV.

4. This will not open the camera, setup the camera, or read from the camera, this is all explained in derek molloy's videos (mostly on his git repo) so no need here.

5. Watch that video these are some pretty simple to grasp concepts but the documentation on them is purely mathematical which is not easy to grasp for most people (myself included). So as the title of this blog suggests, who cares of the math behind erode and dilate let's just learn how to use them correctly and effectively.

6. Moments are really strange in openCV i'm still trying to figure out exactly what they are and how they work. The basics as far as I can tell is that it uses contours to approximate objects and where they are / where they are going.

7. Scoring!!! This is a super important concept that I'm taking a blind jab at (good decision you can tell). The idea behind scoring is that you have multiple objects, but how do you know which one is the one you care about. Well you assign each of them a score and chose the one with the best score. My method for this is to find where the object is and what it's area is, the new object closest to the previous one is likely to be the original. If no objects score above a certain amount than we assume there is no object and must stop immediately seeing as we don't know where to go (that last bit is not implemented yet)

8. PID!!! PID is another very important concept that I used in my previous project with the quadcopter. The sad fact is, this is the real world and no system is ideal there are variables we can not account for such as momentum and rather than blindly ignoring them or trying to perfectly handle them we adopt a good enough system. This is what PID does, we say alright this is what we knew and this is what we think we know. So let's make a best guess on what we will know about the current state and use that as our next chunk of knowledge. There are a lot of pretty good articles out there on PID but sorry I didn't really save them and I implemented this one from memory. Certain programmers have a tendency to over complicate a function that by nature is meant to be simple, and as we all hopefully know KISS (keep it simple stupid) Also yes PID uses calculus but things like rhimean sums and slops which more accurately resemble algebra in my opinion.

Lastly sorry but no knew pictures for this update. I got the rest of the parts for my car but I bought the wrong adaptor for the battery to the car. I meant to get a T-connector to a banana plug, I didn't. It also turns out that my local hobby works does NOT have any banana plug adaptors in house so it's going to be another 3 weeks waiting for customs to get me a single part unless I find one that's closer without having to pay a large sum of money.

See you all soon and hope to be driving the car soon.

No comments:

Post a Comment