Sunday, April 28, 2013

UAV part 5: Switch to V911

Just when I started to make progress on my overvolted single-rotor helicopter, the tail motor blew up (as I knew it would). To move forward, I could have gone back to Syma S107s, but I’m starting to dislike the mechanical complexity of the coaxial system. I have a few of the WL Toys V911 helicopters and they’re awesome. Once you get a controller, you can buy the helicopters for $20, which is actually cheaper than the S107. The V911 is bigger, faster, and has more excess lift than the S107, and weighs in significantly lighter:

I stripped down one of my V911s to install a MATH board. There was no good place to mount the board so I made a 3D printed bracket that screws into the posts where the stock PCB attached. The bracket also has a tray to hold the battery. After I mounted everything, I found that the CG is too far forward. I’ll have to reposition the battery later to fix it, but in the meantime the V911 includes an easy way to compensate for this – the swashplate.

I plan on using the pitch control eventually, but for now I needed to lock the pitch and roll. I used pieces of music wire and bent them into links that connect the swashplate control horns to some studs on the frame. I intentionally left them too long so I can make adjustments by slightly bending the wires to reduce the length. This gives me pretty precise control and I was able to adjust everything into balance.
Then I had some weird issues with a lack of power from the rotors. I checked my PWM output on the scope and tested 4 different batteries (including the stock V911 battery) with no success. Eventually I figured it had to be my FETs, so I salvaged the motor FETs from the original V911 board and put them on my board, and that fixed it. I’ve never had issues with the FDN5630s that I always used on these boards, but this motor is higher current. Another difference is that on my first single-rotor I had higher voltage on the digital section so the FETs had a higher gate voltage to work with, so they may not have been saturated when I tried it here.

 I started with the same code I had used on my smaller single-rotor, and it held yaw immediately, but with some oscillations which you can see in the video. It’s very promising that the PID loop is transferable to a completely different physical system. I just need to make some adjustments to the gains now to get this one locked in. After that I’ll start on altitude control. I did a quick lift test by taping an ultrasonic rangefinder board to the helicopter to see if it could lift it. It could, but just barely. I’ll have to come up with another altitude sensing solution. I may just have to buy one of the more expensive rangefinders that use the single transducer.


I made some other progress towards helicopter autonomy this week—I got the optical mouse sensor working. The one I found in my junk bin was an Avago ADNS-1620. This part had no available documentation, but I did find a lot of similar parts with identical pinouts. They all have the same serial communication protocol, but some lunatic at Avago decided to put the same registers at different addresses in each variant of the sensor. I went through datasheets one by one and punched in new addresses until I found a set that worked. They came from the ADNS-2620 datasheet.

I now have the sensor reading out x and y movement, as well as a “surface quality” value, which is an indication of how good the focus is. I found that this value tracks the distance from sensor to surface with exact repeatability and plenty of modulation in the value as you move around near the target height… Yeah, that’s a free altitude indicator using a sensor that I was already going to include! The downside is it’s only for one specific altitude (whatever I choose as the focal distance with the replacement lens I find), but I think it’s a viable option. I may attempt that as an intermediate step before working out the weight issues with ultrasonic. Wouldn't that be cool to detect altitude based on the focus of the image of the ground?! This method has another significant drawback: if the helicopter goes high enough above the focal distance that it can no longer recognize the correlation of increasing height to decreasing image quality (because the image quality is equally low for different heights so far from the target), then it could just keep going up. Luckily I don’t have to worry about such things any more since I have the radio link, so I can experiment with it freely.

No code in this post, it’s still the same software as last time. I’ll upload a new one once I pull my mouse sensor functions in. 

Saturday, April 13, 2013

Veloster Console Lighting


Here's the first of what I hope will be many posts about modifications to my new car, a Hyundai Veloster. I have a long list of ideas for things I want to do with the car and I thought this one was a good starting point. The car has a little tray under the dash directly in front of the shifter where I usually keep my work badge and other small things. I thought it would be a nice addition to have some lights illuminate the tray, but they would have to turn on and off automatically so they're not distracting while driving at night. There are some plastic handle-type things on either side of it, so I planned to put a beam break sensor across those to trigger the lights when someone reaches in.

Here's a schematic for the circuit I designed. It's run by a PIC12F615 and has an IR led and phototransistor for the beam, a 5V regulator, and three white LEDs driven by a MOSFET. After getting the dash open I discovered a good way to include a programming connector so that's included as well.

The software for the microcontroller operates the beam break sensor entirely in interrupts, and the main loop interprets that information and loads brightness levels into the PWM peripheral. The algorithm for the beam detection is a technique I've used before (in Tiny Tank Bot, among other things): the infrared LED is toggled on and off, and I take an analog measurement from the phototransistor in each state. The two values are then compared to see if they differ significantly, which indicates that the IR light from the LED is "visible" to the phototransistor. This technique works well to reject varying levels of visible light (and other sources of IR) that influence the reading from the phototransistor. Repeated comparison results are then accumulated in a counter so that a confirmed detection of the beam requires a number of consistent positive results. The duty cycle of the IR LED is kept low to save power. 

The brightness levels for the white LEDs are taken from a logarithmic table, and I played around with a variable timing technique on the ramp up and down until it looked smooth. I have the LEDs stay on at 1% while no objects are detected so there will be a nice glow on the tray. The software ramps up the brightness to maximum as soon as the beam is broken, but it waits for the beam to come back for 1.5 seconds without interruption before dimming back down. This makes the lights stay up for as long as you're reaching into the tray, and then smoothly go back down when you're done. Here's a software test on the breadboard:


Once the software and circuit were proven out, I built the circuit on perfboard. I left the IR LED and sensor on headers so I could disconnect them to make installation easier. 

Here's a test of the completed circuit:

Next step was the scary part: disassembling my dash. Luckily I found some instructions from a Hyundai service manual posted on a forum. The center piece of the dash that holds climate controls and two vents is held in by friction clips and can be pried off with plastic tools ( I made some from lexan stock I had on hand).

With that piece off there's plenty of room to work in there, and you can see straight down to the piece above the tray (which has the 12V outlets, aux input, and USB port). I could then see how to get that piece off: it's held in by the same type of friction clips, and it just slides straight out towards the shifter. There is a slot to put a (very short) flathead screwdriver in the back to pry it off. If anyone wants to reproduce this, you don't have to take off the climate control piece, just slide that little pod straight out until the clips pop off. I took that piece in to my workbench and prepared an LED board with three cool white XP-Gs. I drilled holes for the LEDs in the plastic and stuck the LED board down with VHB tape. 

The rest of the circuit went in next. It steals power from one of the 12V outlets, which means this is only active while the car is on. This is not ideal--it would be nice to have it on when you first get in the car and for a short time after turning it off, but this way I can be sure I don't make a mistake that kills the battery while it's parked. I could connect it to a 12V line that's on all the time as it only draws 2-3mA in the idle state, but I didn't want to run new wires to this area. 

At this point I noticed that the 12V outlets have little cutouts in the metal cylinder that are just big enough to pass a 0.1 header (pin strip) through, which gave me a perfect way to include a programming connector. I wired in the connector and insulated it with electrical tape. I then close the lid on the 12V outlet and it's completely hidden, but I still have the opportunity to update the code if I think of a change I want or if my parameters don't work anymore in the real application. Also, the signal from the phototransistor is shared with the data line, which may help in debugging since I can put it on a scope without taking anything apart. 

The outlet isn't usable like this, but that's unlikely to be an issue any time soon since there are two of them. Once I'm sure that I'm happy with the software, I can just push the connector up into the cavity and then the outlet will be usable again. Here's a test after everything was installed onto the plastic pod:


After I got to this point, I went back to the car and spent a whole bunch of time trying to get the plastic handle things open so I could install the two parts of the beam sensor. I didn't make much progress, and decided that it was silly to waste any more time on it when I could just change the sensor from beam break to proximity. Beam break would have given more reliable signals, but changing to proximity allows me to install all components onto a single piece of the dash, not run any wires, and also hides the sensor components from view. 

The change to proximity is almost as simple as inverting the logic in the software: turn the lights on when the IR beam is detected instead of when it goes away. In reality I also had to make some tweaks to increase sensitivity and chose a different phototransistor that performed better in that arrangement. You can see both versions of the software at the bottom of the post. After those changes, I drilled two small holes in the bottom of the plastic piece and stuck the IR sensor and emitter down there. The great thing about this location is that it's pretty much impossible to get your head low enough in the vehicle to see the bottom of this piece, so none of the modifications will be visible. 

Here's a final test of the electronics before being installed in the car:


Installation in the car was very easy, just pop the pod back in and plug in the connectors. Here's a camera shot from below where you can see the mods, but you can't see these while you're sitting in the car. 

And from the video at the top, you can see it works very well. So far it's properly detected my hand every time I reach into the tray, and I'll be watching to make sure I never get any false detects while I'm driving which would necessitate a code tweak. 

Here's the original beam break code and the final proximity code

Bonus project: 
While I had the climate control piece out, I decided to swap some LED colors just for fun. I changed the passenger airbag indicator from orange to blue (this one always bugged me because most of the other indicators are blue), and I changed the seat heat indicators from both orange to orange and red to indicate low and high heat levels. 

Saturday, April 6, 2013

UAV part 4: single rotor

This is the helicopter I’ve been working on for the long period since my last post. After I got the coaxial helicopter up and running with my custom electronics, I flew it way too high outside and crashed it hard enough to break the plastic frame. Then I flew it without the frame like this:

That left the drive gears on the bottom exposed, and I broke one of the gears in another crash. That left all the mechanics pretty much useless, until I had the crazy idea of converting it into a single-rotor helicopter since I still had one working drivetrain.

The board I used for this one is a custom helicopter board I’d been designing for a while. This board put all the electronics I used on the 2.4GHz coaxial into a single PCB, but with a more capable microcontroller to allow for more functions. The board features are:
·         PIC16F1829 microcontroller
·         nRF24L01+ 2.4GHz radio transceiver
·         LY550ALH single-axis gyroscope
·         3 N-channel motor FETs
·         Breakouts for connecting ultrasonic rangefinder & optical sensor

This board is called MATH, for Mostly Autonomous Tiny Helicopter (although at this point it stands for Mildly Autonomous). The core functions are just to get me up and running on a helicopter with the radio link, but later I’ll connect in the ultrasonic rangefinder for altitude control and make it really autonomous.

The connections for the optical sensor (labeled “terrain imager” in the schematic) are for an idea I had that’s not yet tested: I have seen a few robotics projects where people used optical mouse sensors to keep track of motion by focusing them on the ground below the robot. I think it will be possible to use this technique on a helicopter provided I can find optics to focus the sensor on the ground at a height of 3 or 4 feet (and maintain altitude precisely). If this works, the helicopter could keep track of its path and position and navigate autonomously with high precision.

To make the single rotor helicopter, I stripped down the Syma S107 to minimal weight and removed one of the drive motors, gears, and rotor. The tail rotor is now needed for torque balancing so it has to aim sideways. Early tests showed that the stock motor wasn’t powerful enough for this job, so I make a custom tail boom to extend it and increase torque without increasing power draw. I later replaced this aluminum boom with a carbon fiber boom, and put a bigger propeller on the tail motor.

I was able to make the single-rotor fly, but it had to be stripped to less than 20g and run at full power just to stay aloft. Clearly I needed a bigger motor for the main rotor, but I decided to do something crazy just to make this one work, and went to a 2-cell battery to double the voltage. Now I don’t have to worry about weight so much and have power to spare. Unfortunately the motors get hot as hell and will probably burn out very soon, but for code development purposes this setup works well.

I started from scratch on the software to run this helicopter to keep everything clean. I struggled for weeks trying to stabilize the helicopter with my yaw control loop, and finally found the solution this weekend. I was using PID all along but could never seem to get my gains right, but then finally discovered that the control loop was being screwed up by noisy readings. The solution was to use a 100ms moving average (4 samples at 25ms) to get the yaw value from the gyro, and then derive the integral and derivative terms from that value. That smoothed things out and it finally stabilized.


It still jitters right and left, so there is more work to do. Once I’m happy with this I plan to work on adding a magnetic (non-contact) swashplate mechanism to this helicopter to control pitch. Here’s the code