Category Archives: Primus

Primus in control and moving

After a lot of messing about with other stuff, Primus is now back on track and has the basic motion code written and installed. Movement within the maze is possible with steering off the walls to prevent collisions; fixed size moves and in-place turns are functioning; forward error detection and correction is working.

There is quite a lot that is new in this version of the primus code. In fact, I am not going to try and decribe all of it in much detail so download the package and have a good look at the code.

You can grab the package from here: primus5.zip

As before, all the source code is in the package and some automatic documentation has been done by Doxygen. The MPLAB project should open up readily enough and compile without problems. Primus uses stepper motors to push it along. These have particular benefits and drawbacks. In their favour, they are simple enough to drive and capable of precise movements while being driven open-loop. There is no need for any feedback from the wheels. As long as we don’t get any slip, the motor will do just what we tell it. Within reason.

The driver electronics are based on the SLA7062 microstepping driver, configured to produce x16 microsteps. Normally, for the kind of motors on Primus, a complete revolution of the output shaft takes 200 full steps. It is common to drive steppers in half-steps so that it takes 400 half-steps to turn the shaft once. The result is greater precision, smoother motion and better response at speed.

The SLA7062 can make the motor move in steps that are 1/16 of a normal step or 3200 steps per revolution. With wheels of about 50mm diameter, this corresponds to about 0.05mm movement per step or about 517 steps per inch for those of you still measuring in rods and chains. There is a price to pay for this apparent precision. Suppose we want to move at 3m/s. With a conventional stepper driver using 200 steps per revolution, we would need to provide pulses at a rate of 3750Hz for each wheel. Many examples of stepper driver code do this by generating an interrupt for each step. In the interrupt service routine, the code decides how fast to produce the next step and may also sort out things like error correction and steering. At these speeds, we would have no more than about 125us per interrupt to do everything. On top of that we might have to do velocity profiling and handle all the sensors as well as work out where we are.

Clearly, the ISR code would need to be simple and compact. Even then there are issues about the interrupts overlapping. Drive the motors in half-steps and you have twice the frequency and half the time to work with. All in all, things soon get sticky. With x16 microsteps we would need pulses sent to the motors at about 60kHz each and an absolute maximum of 8us per interrupt. Some other method will be needed. The trick is to use the processor hardware to generate continuous pulse trains without using processor time, except to change the frequency periodically.

Although the dsPIC is reasonably well equipped it takes a little experimentation to find the best way to generate a pair of independant pulse trains and arrange for the number of pulses to be counted, all in hardware. I am pretty sure I have not got the best way but it works well enough for this version. Feel free to improve things as you see fit.

Two sixteen bit counters are driven from external pins to get the pulse count from the motor drivers. One pulse train comes from the standard output compare module OC1 while the other pulse train comes from the motor PWM unit. These can generate any frequency of pulses between about 500Hz and 16MHz. Some further work would mean a slightly more useful frequency range but that will do for now. Once we get the pulses sent to the motor, the SLA7062 will sort out everything else. An advantage of the interrupt driven approach is that we know, in each ISR, exactly how many pulses have been sent and thus how far we have travelled.

With free-running pulse generators, we need the 16 bit up counters to keep track of that information. The main control loop runs every millisecond and works out, from the counters, how far the mouse has travelled since the last tick. This is only accurate to the nearst pulse but if we just miss one this tick, it will be included next tick so none should get lost. We are only able to control the speed of the steppers, not the amount of torque they produce so movement assumes the motors are doing what they are told. The motion profiler is given some information like how far to travel, what acceleration to use and what should be the top speed. When movement starts, the value of acceleration is added to the current speed. The profiler checks that the maximum speed is not exceeded. On each tick, a check is made to see if the amount of deceleration needed to come to a halt at the destination is greater than the currently available deceleration. As soon as it is, the profiler switches from accelerating to decelerating and brings the mouse smoothly to a halt at the finishing point.

There will be a small amount of lost motion due to friction. This error is proportional to acceleration but on the Primus prototype is only a matter of a couple of millimeters at normal accelerations over a distance of 15 cells. The profiler code may appear complex. It may even appear overly complex. However, it is written with two aims in mind. First, that it should be extensible to allow smooth, integrated turns and secondly that it be extensible to the control of a DC motor driven mouse. There are other concessions to this extensibility in the code. They are there to give you a head start in adding to the capability of a future develpment of Primus. It has never been the aim to provide code to do all the things that are necessary in a competition mouse so simple rectilinear motion and in-place turns are as much as will appear in the published code. There will be a version of primus with integrated turns and diagonal running just to show that it can be done but don’t hold your breath for the source code for that.

Steering, in the sense of running straight down the walls, is a simple matter of collision avoidance. As soon as the mouse detects that a wall is getting too close, it adds a steering offset to the motor speeds thus steering it away. The diagonal sensors are used exclusively for this adjustment. The same idea could be applied to the forward looking sensors to help avoid collisions with the posts on both orthogonal (up, down and across) movements as well as when running diagonals. The side sensors are used to detect the presence of walls and to detect their edges. In particular, when a wall disappears, the mouse knows where it should be in relation to the centre of the cell. it can compare that position with the position calculated from the wheel counters and adjust that value if there has been any error. Such errors soon occur and can accumulate without suitable correction. They are not always due to wheel slippage. For example, if you are too far to the right before you execute a 90 degree right turn, you will start off the next straight already too far forward.

Note that the file ‘primus.h’ holds a lot of configuration information for the mouse. All of the constants to do with things like the number of counts per cell and the error correction offset etc will need to be determined for your mouse as they are different for every mouse. Calibration is a separate topic of its own and will (may) form the basis of a separate article. Driving the mouse from high level code is done through a command processor. A list of command can be created with instructions like

  • forward 3 cells
  • right 90 degrees
  • forward 6 cells
  • left 90 degrees
  • stop

The commands are executed in turn to move the mouse. This is particularly convenient when testing and the previouly published video of Primus running around a small test maze showed Primus simply following orders. In the full mouse software, this is also how the speed runs are done. Once the maze is solved a set of command can be generated that will take the mouse from the start to the finish and back again. The next installment should include code for simple exploration and solving of the maze.

It may even be enough to run in competition. Not long now…

if you have any questions or observations about Primus, its hardware or software or any of these articles, please go to the forum and ask them there so that others may benefit from both the question and the answer. You can find the forum here: www.micromouseonline.com/forum Happy Mousing

Continue reading

Posted in Primus | Tagged , | 4 Comments

Primus gets moving

Well, it has taken a while but I have finally got the motion control properly underway. Code details will follow shortly. In the meantime, you can see the beast move on YouTube:
This should be the embedded video:

Continue reading

Posted in Primus | Tagged | 2 Comments

Feeling our way with sensors

The sensors are a critical part of a micromouse. Primus uses six infra-red reflective sensors. Here is code to test that they are working.Now that we have a nice display to show what the sensor reading are, we can get that module tested. Since the hardware is very simple, there is not a lot to go wrong. Yet somehow I had a dead sensor when I first tested the Primus prototype. It was a simple dry joint that would have been detected by more careful inspection of the board.This program just sets up the sensor hardware and activates each channel in turn. All six channels are then displayed on the screen as raw ADC values. there is no real need to scale these numbers. If it is working correctly, each channel should show values between nearly zero and about 700 or so.For best performance the sensors will need careful alignment. For now, just check that they are working. They should easily detect the presence of a wall at distances of 150mm and should not saturate until the wall is only about 35-40mm away.The reason the maximum value is only about 700 is that the TSL262 can only reach a maximum output value of around 3.5V. You should find that the sensors are relatively immune to ambient light levels although they will become less sensitive under very bright conditions. A black paper or card shield is needed over the TSL262s to reduce the effect of ambient light.The sensor module is also responsible for measuring battery voltage via a potential divider on the battery input. While not essential for use with NiMh or NiCd cells, it is particularly important that LiPo cells do not become discharged to too low a level. You program can monitor the battery voltage and sound an alarm or shut the mouse down if the battery voltage gets too low. The processor will carry on as long as the voltage into the regulator does not drop below about 7V so you should have plenty of time to avoid trouble.The project files can be downloaded here:primus4.zip Continue reading

Posted in Primus | Tagged , , | Leave a comment

Adding a timer and the graphical LCD

primus micromouse splash screenThis is a fairly big jump. With the LEDs and buttons tested, it is time to add the main graphical display, made from a Nokia cellphone display. Also, there is the main system timer event.

The simple user IO is working, and it is time to add a system timer. This will generate an interrupt every millisecond. In the final mouse a lot of work will be done in here – processing buttons, driving the LEDs, reading sensors and calculating drive signals for the motors.While that may seem a lot in a millisecond, the dsPIC used in primus can execute about 16,000 instructions in that tme and we should only need a small proportion of that time.Another function of the timer will be to maintain an internal clock so that the main program can time events and produce more reliable delays without holding up everything else. We shall continue to use the already present delay functions when it is acceptable for delays to be a bit rough, very short (less than 100us) or when the blocking behaviour is not going to cause any problems. The interrupts from the system clock will make the software delay functions run slow – possibly very slow so they can no longer be relied upon for anything but the simplest, non-demanding tasks.The system timer interrupts come from a 16 bit timer which will run continuously as long as the mouse is turned on. Since actually configuring the timer is pretty simple, this example will make use of the timer to debounce button presses, flash the LEDs and produce tones from the speaker. Trivial really.As with the other examples, the timer has its own source code module and header. Unlike the others, it is a relatively high-level module and must be able to call functions defined for the LEDs and buttons, and later for the sensors and motors.While we are at it, the nokia LCD is working. The test code should display a start up message followed by the Primus splash screen when you press a button. After that, pressing buttons will change the picture. This is just to show off the fact that it works. The LCD will become an essential tool in debugging and setting up so don’t leave it out.While the demo code displays pictures, these take up quite a bit of flash memory – about 760 bytes each – and they are not really needed. I like to put a splash screen up after a restart – just because I can. The images are a bit of a fiddle to create. I make a 2 colour image in PaintShop and save it as a RAW image file. A utility I have will read that file and convert it into the C definition you see in the source code.Remember that the display I have has 96×65 pixels. Try to get the right one if you are to avoid re-writing some of the driver code. Actually, the text stuff will work fine but you get shorter lines and fewer of them so take care.The complete project for MPLAB contains all the source code, the object files and som HTML documentation generated by Doxygen.Primus3.zip Continue reading

Posted in Primus | Tagged , , | Leave a comment

Revised schematics and pin functions

After a bit more thought, there is now a new set of pin assignments for the Primus micromouse, along with a corresponding schematic.The problem with Primus was that it is not possible to generate two independant pulse trains from the PWM generators. All the PWM channels have to share a common time base.The Output Compare peripherals on the dsPIC30F4011 can generate up to two independant pulse trains in hardware but one of the time bases must come from Timer 2 which is needed for counting steps sent to the motors. One of the fundamental design decisions on Primus is to drive the two steppers at high speeds without any interrupt service routine soaking up processor cycles.It seems that one motor will have to be driven from an OC channel and the other from a PWM channel.The PWM channel is fairly straightforward, we just set the duty cycle to something appropriate, like 50%, and change the frame rate to get a square wave at some arbitrary frequency. steps are always made on the leading edge of the pulse so the actual duty cycle is not too important as long as the pulse satisfies the minimum leng specified for the driver chip. This is 2us so we have plenty of scope.For the other motor, one of the OC channels (OC1) will take its time base from the currnetly unused Timer 3. OC1 will be configured to generate short pulses at the start of each cycle. Again, only the leading edge is significant so we just need to make sure that the pulses are at least 2us long.This is not as elegant as I had hoped. the limitations of a 40 pin device start to bite here. Other devices in the family with more pins (but surface mount) give access to a couple more counter inputs and would make the problem a lot more simple. Just to add an extra layer of complication, I amtrying to arrange the pin functions so that conversion to a DC motor driven mouse will be easier. It might have turned out simpler if I had not bothered!The previously published schematics and pin functions have been updated and are available here:Mainboard SchematicsPin function list Continue reading

Posted in Primus | Tagged , , , | 6 Comments

A bit of a change to Primus

It seems I made a bit of a mistake in how I thought the dsPIC could drive its PWM module. Consequently, there will have to be some changes.First, what can’t change is the need to use T1CK and T2CK as external inputs to count motor pulses. There are no other choices on the dsPIC30F4011 without using interrupts. Since I anticipate pulse rates of up to 100,000 Hz per wheel, life will be a lot easier not having to service that interrupt rate, even with a fast processor and a very efficient ISR.
Somehow, I had it in my head that the PWM module could use different time bases for each channel. Wrong – and stupid. So, how to produce two independant pulse trains at frequencies from about 500Hz to 125kHz?Well, the dsPIC has a set of Output compare modules. These are quite capable of the job and have a variety of useful modes. Each can use one of two 16 bit timers for its timebase. At first sight, it would be best to use the OC modules for both motors and have each derived from a separate timer. Unfortunately, the two timers we can pick form are Timer2 and Timer3. But we have already committed Timer2 to the pulse counting job. That means that only one OC module can be used and it must be driven from a timebase using Timer 3.This all makes things a bit tacky. The pin assignments are now a little more confusing and earlier posts will need to be edited along with theschematics and such. Hopefully this will be done by the time anyone gets to read this post. Apologies if anyone has done any work based on the errors. Continue reading

Posted in Primus | Tagged , , , | 1 Comment

Pushing the button on Primus

The next stage in testing the Primus hardware is to make sure the buttons are working.Building on Primus 1, the button hardware is tested in the simplest possible way. Calling the readButton() function sets a number of exported variables indicating the button status.These are tested inside a loop in the main program. The UP and DN buttons each activate a different lamp while the GO button lights both lamps. The lamps then flash – you will see that it is necessary to call the readButton() function again to update the status of the keyPressed flag.Note that the buttons are connected to analogue input pins on this mouse. These pins need special configuration to ensure that they are treated as digital inputs rather than analogue. The pins chosen might also have been used for a quadrature encoder input which would require the same fixup.The complete project for stage 2 can be downloaded as a single ZIP file.this one seems big because I have added some automatically generated documentation created by Dozygenprimus2.zip Continue reading

Posted in Primus | Leave a comment

A little bit of test code

Once the main board is assembled, it can be tested without waiting for the sensor or motor driver boards to be ready. In time honoured fashion, we start with a little program to flash the LEDs.With any new microcontroller project, it is a good idea to start with some simple code that will allow you to test your ability to create a project in the chosen development environment, compile the program, transfer it to the microcontroller and run it. While you are at it, you get to test the simplest part of the system hardware.Development for Primus used the MPLAB IDE from Microchip and the free Microchip C30 port of the GCC C compiler. Between them, these are powerful tools if a little fussy in places. To get started, you will need to install both these packages to their default locations on your PC.The complete project for MPLAB/C30 is contained an a ZIP file that you can download here: primus1.zipCreate a folder in a convenient place on your hard drive and use MPLAB to open the primus.mcp file which defines the project. this project, like all those that follow, contains a number of folders, each with a specific purpose.

src   holds all the C source code files. These all have the extension .c
include   holds all the C header files. These all have the extension .h
tmp   used by the compiler during compilation and linking. Let the compiler handle these. You don’t normally have to worry about them.
output   where the compiler creates the actual rom image that gets sent to the processor. Again, you don’t need to worry about them unless you are using an external programmer and need to find the hex file to download
config   not normally used here but can hold configuration options for building the code using a command line compiler.

The file include/primus.h is where all the hardware is defined for the mouse as well as som common definitions for things like directions and such. this file is included by most other source files in the project. Ensure that you have configured MPLAB to allow you to set the configuration bits in the code, then the macros in primus.c will set things up for you. Primus uses an 8MHz crystal with a x8 PLL multiplier. This gives an effective processor clock of 64MHz which in turn runs the processor at 16MIPs.This short piece of code not only test that the LEDs and speaker are wired correctly but, if you can measure the frequency of the signal driving LEDA, it will be 500hz if you have the oscillator correctly set up. This will be essential when we come to drive the sensors and motors so get it right now, before doing anything else. Continue reading

Posted in Primus | Tagged , | Leave a comment

A new mouse is born

I finally have a primus mouse built and ready to test. All the bits are there and the processor runs up and can be programmed. Here are a few pictures to get you in the mood.By placing the components on three separate boards, each is relatively simple to build and test. Notice that everything is hand-wired on relatively easy to get perforated protoyping boards. The main board is double sided only to make it easy to mount the pin header connectors underneath.

Primus micromouse from above

The Nokia display works and there are three control buttons conveniently mounted just below it. underneath are the motor and sensor connectors. At the top are the serial connectors and the siz pin programming connector. The small stereo jack is for my convenient USB to TTL serial adaptor that is terminated with a 2.5mm stereo plug.

Primus micromouse 3/4 view

At the front, you can see the sensor assembly. Black card stops the detectors seeing the emitters directly and affords some shade from ambient lighting.The emitters need to be aligned and fixed in place with epoxy to stop them going out of alignment. this task would be better with some rigid holders to keep them in place but this will do for now. Also at the front is one of the two 6-cell AAA NiMh batteries.

Primus micromouse 3/4 view from the rear

At the rear are the motor drivers and the other battery pack. The batteries are wired together in series and can be unplugged for charching or when the mouse is in transit to prevent shorts. Not the sharpest picture in the bunch but you get the idea. Continue reading

Posted in Primus | Leave a comment

Primus pin functions

The Primus micromouse will use the dsPIC30F4011 processor from microchip. It has plenty of power and resources for just about any micromouse and is relatively easy to get and use. Here is a list of the proposed functions for each processor pin on the controller board

dsPIC30F4011 Pin Functions for Primus
Pin Functions Primus (NOKIA LCD)
1 /MCLR reset
2 EMUD3/AN0/VREF+/CN2/RB0 left forwardsensor
3 EMUC3/AN1/VREF-/CN3/RB1 left diagonal sensor
4 AN2/SS1/CN4/RB2 left side sensor
5 AN3/INDX/CN5/RB3 right side sensor
6 AN4/QEA/IC7/CN6/RB4 UP button
7 AN5/QEB/IC8/CN7/RB5 DN button
8 AN6/OCFA/RB6 right diagonal sensor
9 AN7/RB7 right forward sensor
10 AN8/RB8 battery voltage monitor
11 VDD VDD
12 VSS VSS
13 OSC1/CLKI 8MHz resonator
14 OSC2/CLKO/RC15 8MHz resonator
15 EMUD1/SOSCI/T2CK/U1ATX/CN1/RC13 left stepper count input
16 EMUC1/SOSCO/T1CK/U1ARX/CN0/RC14 right stepper count input
17 FLTA/INT0/RE8 GO button
18 EMUD2/OC2/IC2/INT2/RD1 diagonal emitters
19 OC4/RD3 LED_A
20 VSS VSS
21 VDD VDD
22 OC3/RD2 side emitters
23 EMUC2/OC1/IC1/INT1/RD0 forward emitters
24 SCK1/RF6 LCD clock
25 PGD/EMUD/U1TX/SDO1/SCL/RF3 programmer and LCD Data
26 PGC/EMUC/U1RX/SDI1/SDA/RF2 programmer clock
27 U2TX/CN18/RF5 Serial TX
28 U2RX/CN17/RF4 Serial RX
29 C1TX/RF1 LCD command/data
30 C1RX/RF0 LCD select
31 VSS VSS
32 VDD VDD
33 PWM3H/RE5 Motor Enable
34 PWM3L/RE4 LED_B and speaker
35 PWM2H/RE3 right motor direction
36 PWM2L/RE2 right motor step
37 PWM1H/RE1 left motor direction
38 PWM1L/RE0 left motor step
39 AVSS AVSS
40 AVDD AVDD

Primus will use the Nokia LCD display from a 3410 cellphone. Note that this is not the common screen from a 3310. the 3310 screen can only do 84×48 pixels. To display the maze we need a bigger screen and the 3410 display can do 96×64 pixels. They are easy to find on the web.
You will see there are six sensor inputs but three emitter outputs. The emitters are fired in pairs to save processor pins. There is provision for three user buttons, a serial interface, the LCD and a battery monitor as well as a speaker and a couple of LED indicators.If you wanted to build Primus without the LCD then two of the buttons are probably redundant also as they are used for the menus on the mouse. The LCD is well worth the trouble and, while you may have thought of using a common 16×2 alphanumeric display, the nokia screens are cheaper, smaller, lighter and much more flexible. Continue reading

Posted in Primus | Tagged | Leave a comment