Programming HELP Please (orangutan X2)

Can you please give us an idea of how to start programing the motors. Is there a site that I can find examples. We have been try to understand the functions and commands from the Pololu site, but still confused.

(Note: Ben has edited the subject of this post to indicate to which product it refers and has moved this thread to the Orangutan forum.)


I’m not sure exactly what you want to do with your motors, but controlling motors with the Orangutan X2 is very easy. The Orangutan X2 test code has a function called motorsTest() that sets the motor speed as a function of the position of the user trimpot.

Your first step should be to put the files SPI.c and SPI.h into the same directory as your AVR Studio project and add them to your project. You can get these files by downloading from the Orangutan X2 webpage. Your next step should be to open the file SPI.h to see what command functions are available to you (scroll down near the bottom, below all the #defines, to get to the function prototypes). Each function is implemented in SPI.c, and each implementation is commented to help you understand what it does and how to use it.

Below is a simple program that will drive two motors:

#include <avr/io.h>
#include "SPI.h"

int main()
    SPIInit();  // *** you must call this before you can send commands to X2's auxiliary microcontroller

    setMotor1( -127 );  // motor 1 reverse at full speed
    setMotor2( 63 );   // motor 2 forward at 50% speed

    while ( 1 );  // loop forever

    return 0;

If you program your X2 with this, you should see the motor-direction LEDs on the top board light up such that one side is bright red and the other is a slightly dimmer green.

The following code will set the motor speed as a function of the position of the user trimpot (assuming you haven’t removed the solder bridge across the “ADC7=TRIMPOT” jumper on the back of the X2’s main board):

int main()
    // initialize ADC
    // bit 7 set: ADC enabled
    // bit 6 clear: don't start conversion
    // bit 5 clear: disable autotrigger
    // bit 4: ADC interrupt flag
    // bit 3 clear: disable ADC interrupt
    // bits 0-2 set: ADC clock prescaler is 128
    ADCSRA = 0x87;

    // bit 7 and 6 clear: voltage ref is Vref pin
    // bit 5 set: left-adjust result (only need ADCH)
    // bit 4 not implemented
    // bits 0-3: ADC channel (channel 7)
    ADMUX = 0x27;

    while ( 1 )  // loop here forever
        ADCSRA |= ( 1 << ADSC );  // start ADC conversion
        while ( ADCSRA & ( 1 << ADSC ));  // wait for conversion to finish
        unsigned char speed = ADCH;  // get upper 8 bits of 10-bit conversion result
        setMotor1( speed - 128 );
        setMotor2( speed - 128 );		

        delay_ms( 10 );  // delay for 10ms

    return 0;

- Ben

How do you plug in the SPI.h, and SPI.c to the program. We create a new project but when we want to add the files it won’t read them. We copy past them to the dirctory, but how do you add them to your project?

On the left side of AVR Studio you should see a docked window titled “AVR GCC”. In this window you should see an expandable tab with the same name as your project and sub-tabs called “Source Files”, “Header Files”, “External Dependencies”, and “Other Files”. Right-click on the “Source Files” tab and select “Add Existing Source File(s)…” Select the file “SPI.c” and click the open button. Now SPI.c should be listed as one of your source files under this tab and it will be linked into your project.

If you want, you can repeat the same procedure for SPI.h under the “Header Files” tab, but this isn’t necessary. Just putting “#include SPI.h” in your source files will be enough to get the header file included where you need it.

- Ben

Are we able to program a transceiver to the X2? We just purchased a RF module( RFD21130 ).

I know nothing about the RF module you just purchased, so I cannot answer your question. I’m guessing you can probably use the X2 to interface with your RF module, but I would need to see a datasheet (or at least get some rudimentary specifications) to give you a definitive answer.

- Ben

i need some help on how to get started working on emitter/dectector on our X2. Can you pls give us some guidelines on how to work with sensors in regards to programming our X2 microcontroller?

To give you any sort of specific information I’d need to know about the sensors themselves. In general, you will make use of the X2’s general purpose IO ports (the 16 pins on the left side of the board).

Starting from the top and moving down, the left-most column of pins are A0 through A7 followed by D0 through D7. The middle column is power (the power source for each bank of four pins is determined by the power solder jumper on the back of the board) and the right column is ground. These pins can either be used as digital inputs or outputs. Additionally, pins A0 through A7 can be used as analog inputs. The first step is deciding if you need to output signals or input them. To set a pin as a digital output, you would use the following code (replace x with the desired pin number):

DDRA |= (1 << PAx);
DDRD |= (1 << PDx);

For example, to make pins A1, A3, and D7 outputs, you would use:

DDRA |= (1 << PA1) | (1 << PA3);
DDRD |= (1 << PD7);

You can then set the value of an output pin to either 1 (5V) or 0 (0V) by setting its PORT value to 1 or 0, respectively. For example:

PORTA |= (1 << PA1); // drive PA1 high (5V)
PORTA &= ~(1 << PA3); // drive PA3 low (0V)

If you want to use a pin as a digital input, you need to clear its DDR bit. The following example will set pins A0, A2, and D0 as digital inputs:

DDRA &= ~(1 << PA0) & ~(1 << PA2);
DDRD &= ~(1 << PD0);

You can then read the value of the pin by checking the bits of the PIN register. The following code will read the input on pins A0 and D0;

pinA0High = ((PINA & (1 << PA0)) != 0);
pinD0High = ((PIND & (1 << PD0)) != 0);

Some sensors will output analog voltages (any voltage between 0 and 5V, rather than being limited to just the two values of high and low). PORTA can be configured to calculate the values of analog voltages. You can see an example of how this is done by looking at the X2 test code that ships on the product. The test code measures the voltage on the user trimpot to determine its position and set the motor speeds accordingly.

What you need to do is find out what input, if any, your sensors require from you. For example, a sonar sensor might require you to send a pulse in order to trigger each sonar ping. You then need to determine what your sensors will be outputting (digital signals or analog voltage) and how to interpret those signals.

- Ben

We will be using the infrared with an emmitter snd detector. The circuit will have an opamp (741) which runs on digital. We will connect it to pins D0-D3. We know our angle that the detectors reads is 120 but we don’t know how to impliment it in the program that will be make the motors run for a certain distance at a certain speed. Once it is closer we need it to reduce the speed and stop at a certain distance.

this is the pseudocode that we wanted to implement
read the digital I/O (for the sensor)
if left sensor detects the object
motor goes to the left
if right sensor detects the object
motor goes to the right
if left and right sensor detects the object
motor goes straight