Hardware friendly C structures

What I really want is a language that has really good structures. That is, they can represent hardware/fixed layouts effectively.

Say I were to enhance C (instead of designing a whole new language). I want something like this:

struct FATBoot {
    uint8_t BS_jmpBoot[3];
    char BS_OEMName[8];
    little uint16_t BPB_BytsPerSec;
    uint8_t BPB_SecPerClus;
    little uint16_t BPB_RsvdSecCnt;
    uint8_t BPB_NumFATs;
    little uint16_t BPB_RootEntCnt;
    little uint16_t BPB_TotSec16;
    uint8_t BPB_Media;
    little uint16_t BPB_FATSz16;
    little uint16_t BPB_SecPerTrk;
    little uint16_t BPB_NumHeads;
    little uint32_t BPB_HiddSec;
    little uint32_t BPB_TotSec32;
    union {
        struct { // FAT12 and FAT16 only:
            uint8_t BS_DrvNum;
            uint8_t BS_Reserved1;
            uint8_t BS_BootSig;
            little uint32_t BS_VolID;
            char BS_VolLab[11];
            char BS_FilSysType[8];
        struct { // FAT32 only:
            little uint32_t BPB_FATSz32;
            little uint16_t BPB_ExtFlags;
            little uint16_t BPB_FSVer;
            little uint32_t BPB_RootClus;
            little uint16_t BPB_FSInfo;
            little uint16_t BPB_BkBootSec;
            uint8_t BPB_Reserved[12];
            uint8_t BS32_DrvNum;
            uint8_t BS32_Reserved1;
            uint8_t BS32_BootSig;
            little uint32_t BS32_VolID;
            char BS32_VolLab[11];
            char BS32_FilSysType[8];
        little uint16_t BS_FATMagic;

I picked this structure because I was unable to represent it in my fat C code because of the crazy alignment. The only thing new yet is the “little” and “align” keywords to specify endian, and alignment/offset respectively. You would probably end up using typedefs like “luint16_t” instead of “little uint16_t”. “big”, of course, would also exist. If neither is specified, then you don’t care and therefore should be implementation defined.

Then what I want is this:

    tight FATBoot diskBoot;
    loose FATBoot memBoot;
    pread(fd, &diskBoot, sizeof(diskBoot), 0);
    memBoot = diskBoot;

memboot is the structure using the alignment and endianness of the host processor (the “loose” keyword). diskBoot is the structure packed as specified and using the endian specified. The endian would be enforced so that the memory image of diskBoot would always be correct. Accessing diskBoot.BPB_TotSec32 would evaluate to the EXACT same thing as memBoot.BPB_TotSec32 but memBoot would be quicker since it doesn’t have to reverse (potentially) or read the bytes out and reconstruct the uint16 in some odd way (because of the weird alignment of the structure). This puts a lot of extra burden on structure copies when one is loose and one is tight, so you would expect them to be slow–certainly not memcpy() speeds.

You could also use “strict” and “relaxed” but “loose” and “tight” are both 5 letters. 🙂

Basically, I want the compiler to deal with stupid endianness for me. I also want my structure to not be compiler dependant. I should be able to publish the structure in my spec and it should work on all compilers and architectures. Bit fields, for instance would be specified a certain way and not be “implementation defined”. At lease the tight ones would. Loose means “implementation defined, and fast”. Tight means “standards defined, but possibly slower”.

Scotch tape and chewing gum should fix that right up!

We came in today and went directly to the practice track. While waiting in line there we did a few warm up tests and discovered that the steering was not working right. It was turning hard left no matter what we did. So we took a look around and discovere the belt that turned our feedback pot had broken. So we had to drive back and figure out how to fix a belt a) without a new belt, and b) without taking off the steering column. We hashed around for a while but no clear solutions came up. Then Jeff arrived and suggested staples. I thought maybe the velcro we had would be a nice material to use since it was totally flexible but non elastic. So we threaded the belt around, used the velcro to tape it together (it had glue on the back of the velcro, and then stapled the velcro to the belt. Amazingly it seemed to work. We felt that this was a true MacGuiver moment. Turns out we found out later the belt was occassionally slipping and so we ended up having to recalibrate the center about 3 times during the day.

We discovered that someone had left Richards code commented out so last night’s run was really just waypoint following plus a small tweak (don’t just go straight if you go off course). Commented in Richard’s code and it started crashing. He fixed it and then we tried following way points but it just didn’t work. The truck would veer off course. I found that I had swapped latitude and longitude at one point, so we thought that would fix it but it did not. We asked for one more QID try but were told they had just declared the day done. Seems odd because usually they stop only after sun down but today it was only 5:30 or so. We don’t know if that is a good sign for us or a bad sign. Richard went to the team meeting but they said nothing about who qualified and who did not.

Came home and got to listen to music in my car and answer my email for the first time in 5 days. I had 1600 spams (that weren’t caught). Ugh.

DARPA Grand Destruction Derby

Tried to get the code I wrote all night to compile but I was litterally falling asleep on the keyboard. I made Jim get my stuff to compile and he had to delete large rows of ssssssssssssssssssssssssssssss and such from the source code wherre I had fallen asleep and my hands had rested on the keyboard too hard. Fell asleep for about an hour on an air matress in the garage. Woke up to a tremendous wind that caused us to keep the garage door closed all day.

We did a practice run and did the U-Turn paved course. We did pretty well and it was the longest the car had driven autonomously with no one sitting inside ready to pull kill switches. Then we tried a S-turn course and failed miserably.

Jim and I cancelled a second practice and countless QIDs (qaulifying sessions) to try to tune the controller constants in the car. We ended up feeling certain things were wrong but when we tried to fix them the overall system was even worse. So we backed out our changes. Richard and Josh argued loudly that we needed to qualify that day so we drove over to the queue.

Jim decided that it was pointless to do a QID when we were at the same point we were the day before softwarewise. So he got richard in the car and they threw together some code that tied the final parts of there code together (basically finished what I had done the night before). Then Jim left and I verified what he had done (and fixed a bunch of bugs), then Richard looked it over. We finished this all up while we were in the chute.

So we decided to just go for it and run the new code. The truck came out nicely and turned when it was supposed to. Then came the sandbags. Our huge wheels just plowed right over them. Then came the fence with a narrow gap. We didn’t detect it and blew right through the (fairly beefy) fence part. Just plowed it right down. Then we came to the part where there is an old van parked in the middle of the street. Our car went right up to it and smashed into it. Oops. But we were so excited that it had even gotten that far down the course that we considered it a victory. There was no damage to the truck at all as it has a monsterous 1/2 thick custom made metal bumper. After that they wouldn’t let us continue becuase it was getting too dark.

6 of us went to dinner at Chevy’s and talked about what worked and what didn’t and tried to decide what the important things left to do were. We went back to the hotel and crashed, having been awake for 39 hours with basically no sleep.

Coming out of the chute

I finally got some sleep. But we got up at around 6:30, breaking my personal commitment to never wake up before 7. We went to the speedway and… I don’t even remember.

Dead battery. Rechard went and bought a new one.

Thought the servo was blown again. Re-centered it. Completely removed MiniPod, everything started working so we put it back together.

Our first QID. We came out of the chute, went straight (just missing the concete border of the chute) then got off course and didn’t turn. We paused it before it hit anything.

Tried to drive around, getting some measurements but everyone was messing with the car or herding us to practices that we’d then back out of.

Drove around looking for food, finally ate at del taco.

All night I wrote a bridge between Richards map finding code and Jim’s controller world.

Race day (or so close, yet so far)

Jim and Richard had left when I woke up at 7am so Maribeth (Richard’s wife) and I drove to the speedway where the opening ceremony was about to begin.

All the robots were lined up and the press was there mingling among the teams and everyone was inspecting everyone else’s robot. There was a surprising amount of designs, from full custom vehicles, to converted SUVs, and some converted ATVs. Very few of them had what our team considers a feature, complete driveability.

The opening ceremony was your standard boring affair, with a flag presentation, a singing of the national anthem, a speech from the head of DARPA, and the introduction of the team leaders.

After that we went back to the garage, pondered schedules, and headed out for a test run around the parking lot.

Right before we headed out, though, we hooked up the e-stop “pause” signal so that the car could come to a stop but then could continue on its way when unpaused. We hooked it up to an input on our microcontroller but it turns out the line we hooked it to was a signal to the build in loader code to not run our firmware. It took us almost 2 hours of backtracking and comparing with a known good board before we figured that out though. When it was done I was very stressed.

So then we finally got to do some practice driving. Jim had inverted a term that we suspected was wrong last night and when we tested it, it started working correctly! So we started actually letting the car steer itself around the parking lot while we tweaked controller constants and tried to get it not to over or under correct too much. After about an hour or so we got the go ahead to go over to the practice run and try our code out.

To get around to the practice run we exited through the pit area got to drive on the actual racetrack till we got to the other side! It was very cool. Mike took some pictures. I hope some came out ok!

When we got to the practice area we found out that it wasn’t for maybe practicing fully autonomous mode, it was exclusively for practicing fully autonomous mode. As in we weren’t allowed to drive along in the car! This was bad because until then our controllers had always had a running start. We couldn’t just go from stopped state to 10 mph. We could go from 10 to 20, or from 20 to 10, or hold 10 indefinitely, but not starting from zero. Not onl that but the pause functionality had to work before we could go and we had only just hooked up the signal that morning! It turns out Jim (with me looking over his shoulder and helping and also trying not to let Josh inerrupt him too much) got all the code written in about 45 minutes. And then we were ready.

So I set up the computer like I normally do, but this time engaging everything, which we have never done. I make sure we get paused and that the brake is completely engaged. Then Jim puts the car into park… To me this feels like we just wound up some Giant catapult and disengaged the safety. At any moment the car may spring to life and go nuts. Or just calmly navigate through the waypoints (which we were hoping was the outcome).

So we go back behind the barrier (the whole field is surrounded by huge concrete barriers to help protect against giant runaway robots) and we signal the guy in charge to unpause the car…

… And nothing happens.


So we trudge back on the field and try to figur out what has gone wrong. It turns out the servo that drives the engine throttle has burnt out (something strange happened that morning and had left the servo going full bore against an object that it couldn’t budge). We decide the test run is over and leave back around the track again (which is still very cool!) and head back to the trailer.

At this point Jim and Richard go off looking for hobby shops to buy a replacement servo while Josh and I smuggle my parents into the pits using his tinted windows and leveraging the “only look in the front seat” mentality of the security guards (parents in the back, us up front waving wristbanded arms.

Eventually I take a 1 hour nap while Jim and Richard return with a new servo. We miss our first qualifying run (it was supposed to be at 4:30pm that day) but we get it rescheduled for Tuesday at 9:30am. The servo gets installed, Olive Garden gets our patronage, and Richard, Jim and I go back to the hotel and crash.

Off to the races!

Today I spent the afternoon at my house rewriting the visualization code in perl and OpenGL. I ended up getting bout 60 to 100 frames per second and the LADAR data now plots in real time and looks real good. I put in waypoint plotting code so we can see big squares where the waypoints are. . The car is represented as a yellow triangle pointing in the direction given to us by gps. The velocity is plotted along the bottom as a bar graph.

So I got up to El Segundo at about 7pm and everyone is frantically trying to get the truck ready for the drive to the California Speedway in Fontana for the pre-meating at 9pm. I Basically just stood around and watched as the hardware guys tried to get a new hall-effect sensor working that used graderature modulation to do the velocity instead of just a single signal. It didn’t work so they put the old one back on. It turns out that wasn’t working either but we didn’t find it out till we were on the road. Jim and I didn’t even get on the road until 8:45pm. So we expected to be late to the meeting.

So Josh installed a new “feature” to the estop hardware where when the e-stop triggers, it engages the brakes in addition to killing the engine. Jim wanted him to put a single switch bypass so that we could drive on the highway safely, but Josh kept resisting. So they finally stopped arguing and sped off down the 105.

Jim had made a software change to the LADAR code and it suddenly stopped working. Of course, he had forgotten that he had made the change so we (erroneously) blamed the malfunction on the LADAR’s finicky state machine that we sometimes get out of sync with. So he kept trying to reset the LADAR by flicking it’s power switch while continually stopped and restarted our code hoping that one time the LADAR would come up. All while driving 60 mph on the freeway.

So Jim goes to flick the switch and suddenly we are braking to a stop in the middle of traffic on a major LA freeway. Uh Oh. We quickly figure out the e-stop has activated and killed the engine. Jim frantically flicks the switch back, hits the hazard light, starts the car (though not in that particular order, and some of them happened multiple times) while I try to click back into our program to enter the cryptic “bd” command to dump the brake pressure so we can move again. It lasted a couple of seconds, but it felt like 5 minutes. I was just sure that someone was going to smash into our rear end and was continually bracing for the impact while we got it back going again.

Josh was right behind us and had to swerve out of the way to avoid smashing his BMW and our cool robot truck. We let him have it later about the override switch. Amazingly, he is still resisting.

So then we get to the speedway and get our credentials for the week (wristband). We then get ushered to our garage in the pit area of the speedway where we are immediately surrounded by a group of teenagers from the Palos Verdes high school team. Their car has been donated by Acura and has been washed recently, while ours is a souped up truck. The difference is striking…

We meet our DARPA TLOs (team liason officers) Tim and Pete. They are real nice and dedicated to getting us qualified for the race. They are also the people, should we actually get qualified, that will tail our vehicle and make sure it doesn’t crash.

We discover that we are not allowed to remove the truck from the premises once we have entered, so Jim and I drive around in circles from 2am to 4:30am while Tim the DARPA guy tails us in his car (the rules say that we have to remain in eyesight of one of our TLOs when our vehicle is moving). We get some good data but some troubling data as well: our car is veering off the path for some reason, even though it seems to turn corners correctly when we come to the given way point. Jim theorizes that a sign is wrong somewhere in the controller but we are just too tired to really think about it so he, Richard, and I head back to Richards hotel room and crash for 2 hours.

Slow and steady loses the race

Today started off great with me writing a really nice framework for our controller visualization program. I wrote a driver for the LADAR and we got to watch actual driving LADAR data for the first time. You could see blips where cars were on the road and even see the road boundaries and intersections pretty clearly. It was a success.

Then I started trying to draw the waypoints. And I started hitting snags. And more snags. And more. And even more. I’ve been working on this code for about 8 hours now and have accomplished almost nothing. I am very disappointed in the outcome. The Tcl canvas is just too slow. I’m think I’m going to split up the program into a perl OpenGL section (for the real time display) and a Tcl/Tk section (for the interface). Then I can have the best of both worlds.

Jim and I were hoping to drive tonight and get real time waypoint visualization. He actually fell asleep on the couch before midnight and was still asleep when I left so he probably wouldn’t have gone anyway.

I got some good ideas but I’m just too tired to implement them. First thing tomorrow I’m going to start cracking….

Hassled by “The Man”

I woke up late today after being awake all night yesterday. I stopped by Indigita and borrowed some boards from to use as backups in case we needed to redo any of the controllers. We probably wont need them though.

From there I got stuck in traffic and decided to reinstall Mac OS X while staring at brake lights. :-) This worked well and let me do half the install before I even got to El Segundo.

When I got there I ate some pizza and got everything installed and working again. I wrote some logging code so that on our next test run we would be logging all the data including LADAR to a bunch of log files so we can go back over them and peruse them at our leisure.

Apparently before I got there someone smoked the 24 volt controller that controls the braking valves. So we couldn’t play with the brakes. There was a nice new rack built into the car where we are going to put the RADAR windows computer and all the cords were cleaned up. It looks really nice!

Jim told us about some field nearby that had a big open area that we could drive around in to test the steering controller. Mike, Josh, and possibly a third person went ahead in another car to the field. Jim and I followed in the robot truck. Sadly the velocity sensor was messed up so we couldn’t play with velocity.

When we got to the field there was a police car waiting there. We pulled up into the field (which was fenced in but had the gate open and no “no tress-passing” sings posted) and 3 other cop cars pulled up and one blocked us from backing up. From there they asked Jim to exit the truck and he started talking to the officer in charge. I was told to stay in the car with the door closed. Josh was trying to explain to some other cops what we were doing but they either didn’t believe him or didn’t get it. Funny quote: “You sound like you are getting upset” said an officer to Josh. Josh talks loud an animated. :-)

One of the cops comes up to me and asks me to get out of the car. He asks if I have any knives or guns in the car (No I didn’t) and what we were doing. At this point I’m trying to think how to phrase “We’re attempting to make this massive truck drive on it’s own” in a non threatening manner. Then he asks me if I’m carrying “anything I shouldn’t be”. I inform him I’m not. He asks if I would let myself be searched then. This is a tricky question. I don’t really want to be searched and we haven’t actually done anything illegal so I could probably assert my rights and say, “no thank you”.

But of course I really don’t feel like making them mad and causing them to look for the minutest infraction to haul me in on, so I say, “ok”. As he searches all my pockets he’s making me hold my hands behind my back with my fingers interlocked and he’s grabbing my hands so I can’t move them. I identify everything he pulls out of my pockets (“That’s my cel phone”. “Those are my keys”. “Those are my dog walking bags” (puzzled look from him), “That’s my wallet”). He takes my wallet and flips through every page looking at all my cards. Now I’m slightly annoyed at the intrusion. “You scuba dive?” He asks. (“Yes, I’m certified though I haven’t dived lately”). I’m not sure why it is relevant or if he is just trying to make conversation. He pulls my drivers license out an gives it a thorough looking over. Though maybe it wasn’t so thorough because he doesn’t mention that it expired over a year ago. :-) He hands it back. Later I realize I was the only one to get searched. Apparently my long hair and goatee put me in the “don’t trust” category. Or possibly the “hippie druggie” category. Prejudiced buttheads. :-)

So then he and the others start quizzing Josh and I about why we would ever want to make a truck that drives itself. “Because it’s cool!” doesn’t seem to satisfy them. Why would DARPA want this? Josh mentions automated troop carriers for wars. The officer isn’t buying it (“So let me get this straight, you have a troop carrier full of men but no one driving it?”). We mention that future wars will be fought with many robotic machines (they already have flying autonomous vehicles). He asks us, “Just who is this war going to be with?” I can’t help laughing at that and respond, “Well, I don’t really get to make those decisions.” Josh adds, “I think congress decides that.” This exchange elicits laughter from everyone within earshot except for the police officers. Maybe I just didn’t get the aim of the question.

I noted at this point that some of the cops seemed to be younger than me. When did that happen?

So at last, after explaining what we were doing and reiterating the trucks myriad safety features (manual overrides and kill switches galore), the police decided that we can’t use the field despite there being no signs saying we can’t. The fact that there is a gate seems to be the main sticking point, even though it is wide open. So they let us go and we drive off in search of large parking lots to test the steering controller.

But we can’t find a large parking lot so Jim and I end up testing steering on a 2 lane deserted road. I have to type “sr5″ or “sr-5″ to set the desired steering angle to 5 and -5 degrees respectively. I end up driving the truck from the passengers seat trying to drive in a straight line but actually making wide arcs across 2 lanes and growing more and more out of control. But we decide that the actual control loop is working well and we don’t really need to test it any more.

We tested gps waypoint following next. I had to put in some code to log the inner workings of the way-point following control code, and we pretty much drove around and tried to figure out if it was getting the right kind of data. After that we went back to the home base.

So Josh and Mike called us from the “tail car” and reported something very strange. They saw some well dressed man in a black car parked on the side of the road taking notes and looking around. Then all of a sudden a huge convoy of black secret service looking cars came down the road flanking a huge flat bed truck with “something that looked like a nuclear missile” on top. This sped up Sepulveda blvd towards LAX. Very interesting!

When we got back I copied some of the logs from the drive to my laptop, helped Richard check in his path code and decided to call it a night.

Midnight and the tweaking of stop and going

Today started badly when I couldn’t get my Titanium Powerbook to use the wireless connection reliably. I had a 10.3 beta installed on so I thought maybe that was the problem. I tried to update to the latest beta (since Apple didn’t have the downgrader for the version I had installed on their site any more) but after I finished updating the dumb thing panicked every time it booted. So there went all my programming ideas. I pretty much tried to just help out whoever needed it.

I discussed logging and log visualizations with Jim. I’m going to write an OpenGL program to parse the log file from Jim’s main control loop and display all the pieces of information in nice graphical form. Right now we just have ascii-art for the LADAR visualizations and we haven’t even got the radar power supply worked out yet. As cool as the ascii-art is, I feel we can get a better representation of the data with and nice OpenGL app.

Jim and I discussed adding accelerators for the gas pedal so that the gas pedal doesn’t get floored when you tell the car to go 20mph from a dead stop. We want it to go nice and smoothly like you do when you accelerate from a intersection after a red light (assuming you are not racing the guy next to you).

I had to replace a power supply after Rob put together the radar computer for us. After that we got it to boot and installed RealVNC on it so that we could run it headless (it has to run windows).

At some point we were out during to test the truck when the Kill Switch system failed and shut down the car. Jim had to phone Josh and make him drive down to us and bypass the kill switch hardware so we could get out of the road. Oy!

The most memorable part of today was when we tested the servo system. From about 12am to 3am We drove all around El Segundo and on PCH from Manhattan beach to past LAX at 15 MPH. The first half of the time we were testing the throttle servo system and tweaking all the constants so that the system didn’t overshoot too much and so it was generally responsive.

The exciting parts came at the beginning when we discovered a wrapping bug that caused the servo to go full throttle when we least expected it! Luckily Jim was fast with the servo override switch and all that happened was that we got a little scared. I must say it’s a very strange feeling to be tweaking parameters on a control system that you also happen to be a passenger in — I was very tense at first but it was strangely exhilarating.

Testing the brakes was just as exciting, especially the very first time. We just did a full brake to see what it could handle. We had the pressure tweaked so that there was no skidding, just a nice, if slightly heavy footed, deceleration.

We kept driving past police cars and desperately hoping that they wouldn’t pull us over. Luckily none ever did.

Jim wanted to test the steering control, but we decided we were both too tired and so we called it a night.

Last Modified on: Dec 31, 2014 18:59pm