Arduino

Reference Language | Libraries | Comparison | Changes

Robot

## Write Your Own Firmware

The Robot Motor Board is intended to work as a "state machine", which means it receives commands from the Robot Control Board and executes them accordingly. It also returns data when requested. The default motor board firmware does not house much intelligence, but does not need to be adjusted for most applications.

You can, however, extend the functionality of the motor board by defining your own modes or creating your own command. To do this, you must modify the motor control board library files.

These instructions are for people already comfortable with programming.

### Modes

The robot has a number of pre-defined modes that instruct the motor board how to operate. For example, there is the "line following mode". When entering this mode, the motor board will read values from the infrared sensor array mounted on the bottom of the board, calculate the desired direction of robot and manipulate the motors directly.

You can change modes with setMode() one the Control Board.

You can define your own modes. To create a new mode, you need to define the mode in both Control Board and Motor Board, and write the logic for the desired behavior.

#### Define a New Mode

- In your OS, navigate to the Arduino sketches folder.
- Open "libraries/Robot_Control/ArduinoRobot.h" and "libraries/Robot_Motor/ArduinoRobotMotorBoard.h" in the text editor of your choice
- In each file, find the section with the mode definitions. It looks like this :

//motor board modes
#define MODE_SIMPLE 0
#define MODE_LINE_FOLLOW 1
#define MODE_ADJUST_MOTOR 2
#define MODE_IR_CONTROL 3
[Get Code]

- add a new mode after these in both files, like this : "#define MODE_MY_MODE_NAME num". num should be a different number than the other modes.
- Save and close ArduinoRobotMotorBoard.h and ArduinoRobot.h
- Open "libraries/Robot_Motor/ArduinoRobotMotorBoard.cpp"
- Find the definition of "process()". Add in a new condition corresponding to the mode's name. Code here will be carried out in `loop()`, so write your logic as you would in your sketch.
- Save and close ArduinoRobotMotorBoard.cpp
- In your sketches for the Robot's Control Board, you can now use "setMode(MODE_MY_MODE_NAME )" to switch to your new mode

### Commands

There are two different types of commands:

- orders (1-directional communication) : Similar to motorsWrite(), the Control board sends a command, that the Motor Board receives and carries out. Nothing is sent back to the command board.

- queries (2-directional communications) : Like with updateIR(), the control board sends a message to the motor board, which then returns data to the control.

To create a new command, you need to define the command code and write the send/receive code for it.

#### Define a New Command

- In your OS, navigate to the Arduino sketches folder.
- Open "libraries/Robot_Control/ArduinoRobot.h" and "libraries/Robot_Motor/ArduinoRobotMotorBoard.h"
- Open "libraries/Robot_Control/ArduinoRobot.h" and "libraries/Robot_Motor/ArduinoRobotMotorBoard.h" in the text editor of your choice
- In each file, find the section with the command definitions. It looks like this :

//Command code
#define COMMAND_SWITCH_MODE 0
#define COMMAND_RUN 10
#define COMMAND_MOTORS_STOP 11
..........
#define COMMAND_READ_TRIM_RE 81
#define COMMAND_PAUSE_MODE 90
#define COMMAND_LINE_FOLLOW_CONFIG 100
[Get Code]

- add a new command after these in both files, like this : "#define COMMAND_MY_COMMAND_NAME num". num should be a different number than the other commands.
- If your command is a 2-directional query, define another command code here:"#define COMMAND_MY_COMMAND_NAME_RE num1" which will be used for the returning command. Note that "num1" is a different number than "num" above.
- Plan the structure of your command. You can have a `byte` or an `int` stored in the command. When sending/receiving a command, writing/reading the corresponding data needs to happen in the same order. See the comment starting with "A message structure will be:" for examples.
- To send the command, you should create a function in the RobotControl library. For an example of how to send a message, see the "motorsWrite()" function in motors.cpp, or "updateIR()" in sensors.cpp.
- To identify the command, find the "parseCommand()" function in ArduinoRobotMotorBoard.cpp. Add a new condition to the case clause that is the same name as the command you defined earlier. Refer to the other conditions as examples.
- If your command is an order, write the function to carry out the logic and you're all set. If it's a query, you'll need to write the function that executes the command, as well as one to return the requested information. Refer to "_digitalRead()" in ArduinoRobotMotorBoard.cpp for an example.

Reference Home

Corrections, suggestions, and new documentation should be posted to the Forum.

The text of the Arduino reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.

### Share

### This link has expired.
Please re-subscribe to our Newsletters.

### Subscribe to our Newsletters

Email

Please enter a valid email to subscribe

Arduino Newsletter
Arduino Store Newsletter

### Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy

###### Newsletter

- ©2015 Arduino
- Copyright Notice
- Contact us
- About us
- Careers