I'd Rather Be Building Robots

November 23, 2009

HiTechnic SMUX Tutorial

Filed under: NXC, RobotC, Tutorials — Xander @ 16:32

The HiTechnic Sensor Multiplexer (SMUX) is a complex sensor that requires a fair amount of intimate knowledge of its operation in order to use it.  The tutorial has both ROBOTC and NXC examples.  Please note that error checking for all the I2C calls is outside the scope of this tutorial.  This was done so as to not clutter the code and take away from the essence; to show how the SMUX works.

Table of contents:

  • The HiTechnic Sensor Multiplexer
    • Introduction
    • Operational states
  • Multiplexer Operation
    • How does it do what it does?
    • Programming examples
    • Putting it all together
    • Advanced operations
    • Dealing with the unexpected
  • 3rd Party ROBOTC Driver Suite
  • Appendix A Register Layout + Function
  • Appendix B: SMUX Status
  • Appendix C: Channel Mode
  • Appendix D: Channel Type
  • Appendix E: Supported Sensors

I hope you enjoy reading it and learning from it as much I enjoyed writing it.

You can download the tutorial here: [LINK].

Edited: 23-11-2009, updated tutorial to v1.0
Edited: 25-11-2009, updated tutorial to v1.0.1

November 8, 2009

ROBOTC I2C Howto (Part I)

Filed under: RobotC, Sensors, Tutorials — Xander @ 15:07

image

I2C is a very popular communication bus for connecting low speed peripherals to embedded systems.  It will come to no surprise that the NXT is also capable of communicating via I2C to digital sensors.  Out of the box, the NXT is capable of talking to up 4 sensors at once.  However, I2C can do much more than that.  This howto is not going to very deeply into the protocol specifications, those details can be found in more than one spot on the Internet.  Instead it will focus on how make the NXT play nice with all manners of I2C capable sensors and peripherals. This installment will introduce the basic calls and variables needed to make I2C calls.  The next installment will focus on error handling and other fun things!

Please note that I am using hexadecimal notation (0x..) for messages sent through I2C.  This is a convention you’ll find used throughout most of the world of microcontrollers.  I suggest you practise a little with it, it will come in very handy as you progress.  You can easily convert between decimal and hexadecimal using the built-in Windows calculator (calc.exe).

What you’ll need:

  • An NXT brick with the ROBOTC firmware installed on it.
  • A copy of ROBOTC on your PC, I recommend 2.00 preview or whatever comes after that.  You can download that here: [LINK].
  • A digital sensor, I’m using HiTechnic Colour Sensor in this example but you can use something else, like a Mindsensors ACCL-NX, it doesn’t matter.

Step 1: Let’s get started

Open a new source file in ROBOTC and create a standard main task with the following:

 Step 1

This configures port S1 as a low speed I2C sensor.  This is a speed that should work with any I2C sensor as it uses the standard Lego clock speed (9600 Hz).  The program doesn’t do a whole lot yet.  It mainly just loops around until the cows come home or the batteries run out, whichever comes first.

Step 2: Declaring an array

I2C works by sending messages.  To be able to send a message, we’ll need something to hold the message. An array of bytes will do the trick very nicely (line 5).

 Step 2

Step 3: Addressing the slave

Each device on the I2C bus has a unique address.  If you want to talk to a slave, it needs to know you’re talking to it and not one of the other possible 126 slaves on the bus. This is done by setting the address of the slave as the first byte you send  (line 11). 

Step 3

Why am I setting the 2nd byte in the array as the I2C address (0×02) and not the first?  We’ll get to that later, rest assured that it’s supposed to be that way.

Step 4: Selecting the read register

Most sensors made for the NXT use a standard register layout.  “What, pray tell, is a register?” you might ask yourself.  Well, think of registers as memory locations or drawers, if you will, where information is stored.  In order to get the right information, you need to know what register you want to read from.  Lucky for us, you don’t usually have to guess where the information is kept.  The register, or address, that holds the latest sensor reading is almost always 0×42 in the case of NXT sensors.  We’ll use that as the 2nd byte we’re sending to the slave (line 14).

Step 4

Step 5: Specifying message size

We’re almost ready to send our message to the slave but first we need to do one last thing; specifying the message size.  Remember I said I would get back the first byte in the array later?  Well, that time has come.  In order for ROBOTC to know how much data to send to the slave, you need to specify the size of the message.  In this case, our message is only two bytes long; the slave address (0×02) and the register we want to read (0×42), so let’s put that in our program (line 17).

Step 5

Step 6: Getting the message across

We’re good to go now.  The ROBOTC call to send a message via I2C is sendI2CMsg(nPort, sendMsg, nReplySize).  The first argument, nPort, is S1, S2, S3 or S4.  The second argument, sendMsg, is a pointer to the the array containing the message.  The third and last argument, nReplySize, specifies the number of bytes we want to read back from the sensor in response to our request.  We’ll tell ROBOTC that we want to read back a single byte (line 22).

 Step 6

Step 7: Handling the slave response

The code in step 6 isn’t all that useful considering we’re not actually doing anything with the slave’s response.  To retrieve the response from the slave, you must use readI2CReply(nPort, replyBytes, nBytesToRead).  Once again, nPort is the port specifier, S1 to S4, replyBytes holds the slave’s response and nBytesToRead tells ROBOTC how many bytes we’re expecting from the slave.  We’ll print the response to the screen, so we can see (lines 28, 29).

Step 7

Compile the program and upload it to your NXT brick.  Run it and look at the the number on the screen.  If you have the HiTechnic Colour Sensor connected, observe how the number changes when you hold differently coloured objects in front of the sensor.  If you’re using the Mindsensors ACCL-NX, you’ll notice the number changes as you change the orientation of the sensor.

The sensor updates the contents of register 0×42 every couple of milliseconds (ms).  What you are seeing are constantly updated values as returned by the sensor.  Congratulations on writing your first I2C driver for the NXT!

Final words

The code for this program can be downloaded here: [LINK].  In the next installment I will go over error handling and how to deal with more than one byte in a response.

August 4, 2009

Connecting the NXT to an ArduinoMega

Filed under: Experiments, Ramblings, Sensors, Tutorials — Xander @ 18:09

I’ve had my ArduinoMega for almost 2 weeks now and until today I had no luck getting it to work as an I2C slave with the NXT.  The NXT is a finicky beast when it comes to I2C.  It’s really not all that standard and requires very high pull-ups (82K) for the NXT to be able to pull the lines down properly.

After testing the ArduinoMega’s ability to play I2C master to a number of ICs I had lying around, an MCP23008 and a DS1631, I was pretty sure the I2C lines were working properly.  I tried in vain to make the ArduinoMega work as a slave with the NXT using the AVRlib I2C library, the one provided by the Atmel AVR311 Appnote (source code) and the Arduino Wire library.  Having tested it with 3 libraries, I was pretty sure it was probably not a software issue but some stupid electrical problem.  Little did I know how stupid it turned out to be.

Lacking the proper equipment to further diagnose the problem, namely a scope and a logic analyser, I employed the help of my friend Joep.  We hooked up the ArduinoMega to his PC based I2C master board and all tests completed successfully.  The board responded as it should so that definitely ruled out the software as a source of our problems.  So what could it be?  We connected the ArduinoMega to the NXT using a small breadboard and two 82K pull-ups.  With the scope hooked up to SCL the problem became pretty obvious; the NXT wasn’t pulling down the line enough.  That made no sense, since the internal pull-ups of the ATmega1280 were explicitly disabled in our program.  Joep re-examined the PDF with the circuit diagram and discovered that the SDA and SCL lines had in fact 10K pull-ups!  They were not drawn near the chip itself, but way, way off in the top right corner where you could easily miss them.  With the 10K pull-ups in parallel to the 82K ones, the resulting pull-up resistance had been reduced to a mere 8K9.  After a little brain surgery, the two evil pull-ups from Hell, were removed and subsequent testing resulted in a successful NXT to ArduinoMega I2C transaction! 

If you plan to use your ArduinoMega with an NXT, you will need to remove R2 and R3, which you can find below.  Click on the left picture for a larger version. 

[Click to enlarge] [Click to enlarge]
Before brain surgery After

Be very careful when removing these two resistor, of course.  You don’t want to end up damaging your board.  Joep is quite experienced in soldering very small components and has a very fine tipped soldering iron.  The end result was very nice.  Click on the picture on the right to see a bigger version. 

So why had no one else bumped into this problem with Arduino’s before?  Well, it turns out that the Arduino Duemilanove, for example, does NOT have these pull-ups.  The SCL/SDA lines are shared with the ADC channels.  You can’t very well put a pull-up on an ADC channel.  I am not sure why they would put the pull-ups on the SDA/SCL lines on the ArduinoMega when they weren’t there on the normal Arduino

Thank you, Joep, you saved the day!

June 23, 2009

Subsumption, all the cool kids are doing it

Filed under: Ramblings, Robots, Tutorials — Xander @ 21:05

Taken from http://roachnet.wordpress.com/ “Subsumption?” I hear you say, “What’s that?”  Subsumption is a form of behaviour based robotics where multiple behaviours can control a robot using a hierarchical system.  However, don’t take my word for it!  Check out Thom Roach’s great new blog.  He’s written some really ace articles on the subject without reverting to dry stuffy theory like you see in most research papers.  Using clear text and simple diagrams he shows us how you could make a simple robot that uses subsumption to get around.  Be sure to read them in reverse chronological order!

May 21, 2009

Control the world with the HiTechnic IR Link

Filed under: Sensors, Tutorials — Xander @ 20:53

IRLink Sensor The HiTechnic IR Link is capable of many things including controlling your Power Function devices.  The one thing you couldn’t do was transmit regular data.  That is, until now. 

The IR Link has several transmission modes, 0×02 for example is used for the PF functionality.  There is another mode 0×00, UART-mode, which can be used to transmit 8 bytes at a time at 2400 baud using a 38KHz carrier frequency.  It is also the mode that can be used for communicating with the RCX.

There are two ways you can use the IR Link to transmit your data.  The first method which can be used to send the same 8 bytes over and over again works by preloading the transmit buffer at 0×45 with the 8 bytes you wish to send, followed by the total message length (0×08) and the transmission mode to be used (0×00).  You can then trigger the IR Link to start transmitting by setting register 0×4F to 1 with another I2C message.  Care must be taken not to trigger a retransmission before the previous one is done or you’ll garble the currently transmitting message as it will immediately start transmitting from the first byte again.

If you don’t want to send just 8 bytes over and over again, you can also load up the transmit register at 0×45, followed by the message length (0×08), transmission mode (0×00) and the trigger (0×01). 

Due to the nature of UART-mode, the bytes are transmitted UART-style with odd parity.  That means that when you tell the sensor to transmit 0xAB (b10101011), the following is transmitted via IR:

Start B0 B1 B2 B3 B4 B5 B6 B7 Par Stop
1 0 0 1 0 1 0 1 0 0 0

Each data byte is preceded by a start bit which is always set to 1, then follows bits 0 through 7 of your data (least significant bit first), inverted (0 becomes 1, 1 becomes 0), the odd parity value and then a stop bit which is always 0.  Sounds complicated?  I agree, it took several edits of this blog entry to get this right!

For each bit set to 1, the IR Link’s transmitter goes high for 1/2400th of a second or about 0.42ms, that means that 2 consecutive bits set to 1 will mean an IR burst of a little more than 0.83ms. 

There is a gotcha with the IR Link’s transmission buffer;  its start register is counted backwards from 0×4D. That means that if you have an 8 byte message to send, the first data byte starts at 0×4D-0×08 = 0×45. So if you want to transmit the above sequence, you’ll have setup the IR Link’s buffer such that it looks like this:

Reg 0×45 0×46 0×47 0×48 0×49 0×4A 0×4B 0×4C 0×4D 0×4E 0×4F
Data 0xCC 0×76 0×66 0xFF 0xCC 0×00 0xAB 0xAA 0×08 0×00 0×01

Where:
0×4D is the number of bytes to be transmitted
0×4E is the transmission mode
0X4F is the transmission trigger
You will nee to skip the transmission trigger (0×4F) if all you want to do is preload the transmission buffers. 

Please note that there is no way for the IR Link to know if transmission was successful.  An easy way to check if your IR Link is sending *any* IR signals is to point it at a digital camera and look through the viewfinder.  The IR light should appear as a dot the small screen on the front of the sensor.  Most digital cameras are extremely sensitive to IR light.  This is also a good way to check if your TV remote is still working!

I’ve included two NXC programs, one which transmits the same message over and over again as fast as possible, the other sends a single message every 50ms.

IR Beacon: [LINK] Generates 1200Hz IR signal, can be used with IR Seeker V2
IR Messenger: [LINK]

The functionality to send messages via IR will be included in the next release of the 3rd Party RobotC Driver Suite, so I didn’t include any here.

May 14, 2009

RobotC Tutorial

Filed under: Tutorials — Xander @ 18:06

As great as RobotC is, the one thing that its users have consistently complained about is the lack of good tutorials that go beyond the simple “move motorA at 20% speed”.  Well, rather than waiting for the CMU Robotics Academy to come up with one, Albert Schueller, one of the professors from Whitman College, has written an excellent course called “Programming with Robots” based on RobotC.

It covers all the bases, variables, conditionals, loops and arrays, you name it.  Each new concept is accompanied by example code and every chapter ends with a series of exercises that you can use to test if you have grasped the new concepts covered thus far.

You can download this course as a PDF file here: [LINK].

Blog at WordPress.com.