Friday, December 3, 2010

LTspice Tips: Custom Components

So you've got LTspice, but you need to add a custom component, like a motor, or Op Amp or digital logic or something. Not just a simple transistor but some wildly complex black box type of thingy.

No problem. Here's how.

Overview

Whereas in MacSpice one has to manually set up files with subcircuits to be included for modeling complex devices, LTspice front ends these SPICE features by enabling the user to create new components. First, you draft a new symbol, in this case, I drew a typical motor symbol (right), a circle with an M in it, and two pins (ports) shown as squares.

Next you set up a schematic in the same directory and with the same base name as the symbol (e.g., motor.asy and motor.asc). You associate the pins/ports on the drawing with those in the schematic and now you can just plug in this component by adding it out of your directory just like you'd add any other LTspice component. This is a pretty clever approach that gives LTspice a lot of flexibility and power.

Behind the scenes, LTspice is using subckt cards to implement this hierarchy. It puts the subcircuit description inline in your circuit diagram when you load a component. You can see how this is implemented by selecting View > SPICE Netlist.

Walkthrough Example: Creating a Motor

Create a symbol. Select New Symbol from the File menu. Use the drawing tools in the Draw menu to create a circle with an M in the middle (schematic symbol for motor).



Circles are defined by clicking two points, upper left and lower right. You can use the drag tool to center it. Text is added with the text tool of course, and you would want to select centered justification.


Now it's time to add pins/ports. From the Edit menu, select Add Pin/Port (see image below left).  Label the ports "a" and "b". Then click the location on the symbol where the port belongs. I added a port at the top and bottom. Now, draw a line/wire from each port to the main body of the device--the circle in this case (see image below right).


Save the file under LTspiceInstallDirectory\lib\sym\YourSubDirectory as an .asy file, such as motor.asy


Create a Subcircuit. A subcircuit is the schematic that is inside the symbol's "black box". Open a new schematic and draw the components that model your motor.


Let's just simply model the motor as a resistor. So, add a 16 ohm resistor, call it RM. Now, add two wires on each end of the resistor. You can always make the model more complex later.


Label each wire to match each port in your symbol. In our case, label the wires (aka nets) "a" and "b". Save the file into the same directory as the symbol file and give it the same motor base name. So save the file as motor.asc


Use the Component. Now, open a new schematic, and add a component. Select the motor component that should now appear (if for some reason it doesn't, make sure you opened a new schematic after creating the symbol; if all else fails restart LTspice). Add it to your schematic.



Toss in a voltage source, set it to 9 VDC. Wire it up and add a ground.


Click the Run Simulation tool. Click the DC op pnt (operating point) analysis tab. Click ok. The simulation should run and you should see some current through the motor component.


Here's a zip file of the files I created for this walkthrough: ComponentDemo.zip

And that's all there is to it.

Thursday, November 25, 2010

Ideas for cheap Laser Rangfinder

The bounty for hacking the XV-11's $30 LIDAR, teardown talk on Sparkfun and hacking thread on Trossen Robotics prodded me into doing some quick experiments.

I've been pondering, but not actually doing anything with laser-based object detection for a year now, for both the Trinity-style firefighting competition and the Sparkfun AVC.

The XV-11 unit looks to me like a camera and a laser pointer spun by a small CD motor (details of the system in this white paper). Picture from Sparkfun below. How hard would it be to hack something like this together?



Awhile ago I bought a line-generating laser to identify obstacles. Powered with a LM317 generating the required 3.0V, the laser generates a pretty nice line once you focus it.



Based on ideas I found online, one places the laser at some point low on a robot, with the laser's axis and line parallel to the ground, while the camera is placed some distance above and pointed slightly down, so that parallax can be leveraged.

A horizontal line is generated lower in the camera image for nearby objects, and higher in the image for far away objects.

Since I'm using Pokey's grayscale camera, rather than a color camera, I took pictures with and without a photographic red filter to see if that would help distinguish the laser reflection.

Laser line, red filter. Note height of line on cup, laser printer.

Laser line, no red filter

One could probably write code to detect the line even in a grayscale image although this would be substantially easier with an IR laser and IR filter. Or with a color camera.

Here's a color picture of the test area, my desk. Old HP laser printer to the left, McD's cup, front and center, etc.

As for the LIDAR concept, put a camera next to a laser pointer, align them to leverage horizontal parallax so one can measure distance based on the position of the dot.

I was curious to see what a tiny red laser pointer dot would look like on the grayscale camera. The dot is pretty tiny at about 8' distance (see red arrows).

Distant dot (red arrow), no red filter

Distant dot, with red filter

Maybe too tiny to reliably detect, although with the LIDAR one can constrain the search area vertically and possibly horizontally.

And constrain the search algorithm to square and circular shapes.

Come to think of it the code could correlate dot size with position for added robustness.

And actually the red filter seems to make the laser dot the brightest thing in the image, once the exposure and gain is set properly.

Nearby dot, no red filter

Nearby dot with red filter
Once code and camera can find the dot, the system has to do it quickly so it can be mounted on something spinny.

Doing a 360 degree sweep at 10Hz like the XV-11 means 3600fps which suggests it is using a much faster optical device than Pokey's camera or anything like it. I am guessing Neato is using a linescan sensor. You also need a fast processor. Speculation on the Sparkfun teardown blog post points to a Texas Instruments DSP device, TMS320C2801 at the heart of the XV-11 LIDAR's image processing.

I hit a wall at 3fps with an Arduino running Pokey's camera. I'm in the middle of interfacing with a Propeller but at absolute best I can get from that camera is 30fps. Nowhere close to fast enough for LIDAR. But workable with the laser line sensor concept. But all that's best left to another blog post or two.

I plan to just wait for someone to hack the XV-11 LIDAR and wait for spare parts to come out of Neato and hope I can afford the darn thing or build it myself for $30 as promised.

Sunday, November 21, 2010

Robotics goodies on sale...

Check it out, microcontrollers and other robotics goodies on sale. No affiliation with either company. Hope this is helpful to others on a shoestring budget like me.

Pololu is doing a black Friday sale with great prices on what I think is really useful stuff. I'm a fan of Pololu; Pokey's brain and drivetrain came from them. Good guys. Anyway, I have my eye on the $20 Orangutan B-328, $10 dual serial motor controller, $5 step-up step-down regulator, and $9 3-axis accelerometer but there's a bunch of stuff others might want too.  Hm, I better check my bank account balance...

Newark's Element-14 has some nice promotional deals going on controller boards like a $17 Multicomp Arduino board (35 day lead time, though), Multicomp Mega for $35, a Multicomp Nano for $28, an Amicus18 PIC board for $32 and an NXP OM11042 rapid prototyping board for $41.25 with the NXP LPC2368 ARM7 MCU.