#3: Crash

Last month, I ended my blog with a to do list on the things I wanted to accomplish by the time this blog was due.  The list was:

  • Fix the PID algorithm
  • Add the “I” and the “D” of PID
  • Add weights to sensors
  • Add hazards
  • Harder to navigate roads

And among those, these are what got accomplished:

Which is demoralizing.

Autopsy report

I once read something to the effect of: 90% of your project will be coded in the first 10% of the time spent on it.  The other 90% of the time will be spent on the last 10%.  This has been my first decently sized project since first learning how to program, and I already have a data point of one that agrees with that sentiment.

So, what happened?  Well, the simple answer was I started to implement the next part of the project, and it did not work.  I ended last month with a sensor that could detect when it was off road, and make a course correction based on that.  While that did get an object that can navigate a path, the point of this project was to navigate using proportional, integral, and derivative control.  So I added another type of sensor was located where the first sensor was, and followed the first sensor exactly, until the edge of the road was found.  When the edge was found, the new sensor would stay at the edge of the road, rather than continuing to move freely off road.  You would then have a sensor that was off the road, and another sensor at the edge of the road.  With these two sensors reporting their pixel location, the idea was to get an error calculated based on their pixel distance from each other.

The way I went about implementing the second sensor is I created a new class (the first sensor was called Color Sensor, and the new sensor was called Distance Sensor).  This new class was instantiated in the Color Sensor class’ init method, but did not inherit from the Color Sensor.  I was able to always get Color and Distance Sensor pair in this way, which is what I wanted.  However, in the implementation of the Distance Sensor, I had its movement based on the movement of the Color Sensor.  As a result, the Distance Sensor tried to move with the Color Sensor, but then was restricted to the road.  Every time I encountered anything other than straight road, issues would happen (usually the Distance Sensor would fly off to infinite).

After banging my head against a wall for 2 weeks, I asked my mentor, Brian Krasnopolsky, for help.  His recommendation was to remove the distance sensor and combine the two sensors in to one, reasoning that if you have 2*n sensors as opposed to just n sensors, that’s just doubles the number of things that could break.  I have no emotional attachment to any individual module of this project; my goal is to get it working, so I spent some time rewriting how the color sensor works, described in the pseudocode:

def __notRoad(self, carCenter, target):

update sensor

find the edge of the car

get (x,y) distance of center of car to the target location

start the sensor at center of car

set dx, dy to an increment equal to half the screen

while the sensor registers as on road:

move the sensor location (x, y) to (x + dx, y + dy) pixel location towards the                            target location

if sensor location (x, y) == target location (x, y):

return (color, edge of the car pixel location (x, y), sensor location (x, y))

# color determines whether the car is on road or not

read the color of the sensor at its current location

return (color, edge of the car pixel location (x, y), sensor location (x, y))

As a result, the sensor now can tell how far away it is from the car, and never reads past the edge of the road.  The desired result of having the Color Sensor and Distance Sensor rolled in to one has been achieved.  Furthermore, this is my first addition of a private function (with the use of __functionName).  I know nothing is ever really private in Python, but since I plan to learn C/C++ Soon™ it made sense to use it here as nothing should have access to the __notRoad function.  I imagine in a production environment having __functionName in Python would just let other developers working on the same project to know that this method should only be accessed by its class, rather than strictly forbidding it.

Light at the End of the Tunnel

This technically concludes the mentorship program, save for the presentation on this project to be given at a Chipy meeting in a few months.  The key takeaway is that it is in a few months, which means I can continue working on this project.  Were the presentations to be in a few days, I would not participate out of embarrassment; the project feels like it has taken two steps backwards from where I was a month ago.  Having said that: I know the code is better, satisfies my needs better, and is better organized.  But nobody cares if you rearranged your office furniture – they want a functional office.  So the work continues on the project, and with any luck and perseverance, I will have something more impressive to show off than I do right now, or compared to last month.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s