You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: programming/arduino/custom_firmware.md
+91-99Lines changed: 91 additions & 99 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,148 +3,140 @@ layout: page
3
3
title: Arduino custom firmware
4
4
---
5
5
6
-
Arduino custom firmware
7
-
=======================
6
+
# Arduino with custom firmware
8
7
9
-
<divclass="info">
10
-
This documentation refers to a feature which is only available on the physical robot kits.
8
+
<divclass="info"markdown="1">
9
+
On this page we talk specifically about Arduinos.
10
+
However this page is applicable to opening any device that shows up as a serial port.
11
11
</div>
12
12
13
-
The Ruggeduino that came as part of your kit was shipped with a firmware that provides the functionality outlined in the [Ruggeduino](/docs/programming/arduino) page.
14
-
You may wish to extend the functionality of this firmware, or completely replace it.
15
-
The `sr.robot3` library provides support for three Ruggeduino firmware scenarios:
16
13
17
-
1. Default SR firmware
18
-
2.[Extended SR firmware](#extension): Firmwares that add commands to the default SR firmware.
19
-
3.[Completely custom](#completely): Any firmware not derived from the SR firmware.
14
+
## Ignoring a device
20
15
21
-
By default, the [`sr.robot3`](/docs/programming/robot_api/) library assumes that all connected Ruggeduinos are running the SR firmware
22
-
or firmware which is compatible with the SR Ruggeduino firmware.
23
-
If you're using completely custom firmware, you'll need to tell the kit to ignore the ruggeduino so that you're able to define your own setup logic.
16
+
By default when the `robot` object is created it will try to communicate with all Arduinos and will expect them to respond in the way the SR firmware does.
17
+
If you want the API to not try connecting to a device we need to *ignore* the Arduino.
24
18
25
-
[Extension of the SR firmware](#extension) {#extension}
26
-
------------------------------
19
+
To configure a `Robot` object to ignore a Arduino with custom firmware, you will need to provide it with the Arduino's serial number.
20
+
The Arduino serial number is a string of numbers and letters, and is output in the robot log when you run a program on your robot with your Arduino connected.
27
21
28
-
<divclass="warning"markdown="1">
29
-
Because the API sets all pins to inputs when the robot is initialised, you cannot set pin modes in the setup function as these will be overridden.
30
-
In order to setup pins you can either create a function on the ruggeduino that you call after your robot is initialised or use the [Python functions](/docs/programming/arduino/#pin-modes) to setup the pins.
31
-
</div>
32
-
33
-
You may wish to extend the SR firmware with additional functionality.
34
-
This will allow you to continue using the commands already provided by the SR firmware (e.g. `digital_read()`),
35
-
which means any existing robot code you have won't need modifying very much.
36
-
When you extend the SR firmware, you'll be adding at least one new command to the firmware.
37
-
There are almost limitless possibilities of what your commands may do, but here are some examples to give you an idea:
38
-
39
-
* Talk to an SPI or I2C sensor.
40
-
* Read N input pins at the same instant in time.
41
-
* Time pulses received from an ultrasound sensor.
42
-
43
-
There are three steps that you will need to go through to implement and use your custom commands:
44
-
45
-
### Step 1: Add your command to the Ruggeduino firmware
46
-
47
-
To extend the SR firmware, you will need to first download its [source code]({{ site.baseurl }}/resources/kit/ruggeduino-fw.ino), and edit it in the Arduino IDE.
48
-
When the SR ruggeduino python library wants the ruggeduino to run a command, it sends it a single character to tell it which command to run.
49
-
You'll find a `switch` statement in the `loop()` function that processes this command character:
50
-
51
-
~~~~~cpp
52
-
switch (selected_command) {
53
-
case 'a':
54
-
command_analogue_read();
55
-
break;
56
-
case 'r':
57
-
command_read();
58
-
break;
59
-
case 'l':
60
-
command_write(LOW);
61
-
break;
62
-
63
-
// ... and so on ...
64
-
~~~~~
22
+
You'll need the serial number later, so it's best to save it into a variable:
65
23
66
-
For example, you can see in the above that when it receives an "a" character, it calls the `command_analogue_read()` function.
67
-
This function does pretty much what it says on the tin: it reads an analogue pin.
24
+
~~~~~python
25
+
from sr.robot3 import*
68
26
69
-
You will need to add your own entry into this `switch` statement for your new command.
70
-
This will need to be represented by a character that doesn't already appear in the switch statement.
71
-
Let's say you chose "c"; your entry would look like this:
27
+
# Replace this with the actual serial number of your board
28
+
ARDUINO_SN="752303138333517171B1"
72
29
73
-
~~~~~ cpp
74
-
switch (selected_command) {
75
-
case 'c':
76
-
command_bake_cake();
77
-
break;
30
+
robot = Robot(ignored_arduinos=[ARDUINO_SN])
78
31
79
-
// ... all the original entries ...
32
+
# The rest of your code
80
33
~~~~~
81
34
82
-
You would then write your `command_bake_cake()` function.
83
-
Your command can read additional data from the serial port if it requires additional information to operate.
84
-
It can also write a response back to the host (your Python code).
85
-
Have a look at the `command_read()` function to see how to do this.
86
35
87
-
### Step 2: Use your new command from Python
36
+
##Opening a serial port
88
37
89
-
You can send a custom command from your Python code to the Ruggeduino to control your cake-baking.
38
+
If you need to communicate with a device, you will need to open its serial port.
39
+
If you want the `robot` object to do this and provide a serial port for your use, you will need to do the following.
90
40
91
41
~~~~~python
92
-
cake_result = R.ruggeduino.command("c")
93
-
~~~~~
42
+
from sr.robot3 import*
94
43
95
-
The `cake_result` variable will contain any response from your firmware, if you sent one.
44
+
# Replace this with the actual serial number of your board
45
+
ARDUINO_SN="752303138333517171B1"
96
46
97
-
You're done! You can now use your custom cake-baking firmware!
47
+
# Set this to the baud rate that the device communicates with
48
+
SERIAL_BAUD_RATE=115200
98
49
99
-
If you have multiple Ruggeduino running custom firmware, you can keep track of which one is which
Refer to the [pyserial documentation](https://pyserial.readthedocs.org/en/latest/) for more information on how to use pyserial.
127
+
128
+
129
+
## Finding other serial devices
130
+
131
+
If you are using your own serial device that you want to access via a [raw serial port](#opening-a-serial-port), you will need to know its serial number.
132
+
To do this we provide a helper function.
133
+
Running the below code example will print a list of all the devices connected to the system.
134
+
135
+
Once you have found your device, and copied the serial number, you can follow the guidance in the [opening a serial port](#opening-a-serial-port) section.
136
+
If you need help finding which device is yours contact us on Discord for help.
You may wish to extend the SR firmware with additional functionality.
9
+
This will allow you to continue using the commands already provided by the SR firmware (e.g. `digital_read()`),
10
+
which means any existing robot code you have won't need modifying very much.
11
+
When you extend the SR firmware, you'll be adding new commands to the firmware.
12
+
There are almost limitless possibilities of what your commands may do, but here are some examples to give you an idea:
13
+
14
+
* Talk to an SPI or I2C sensor.
15
+
* Read N input pins at the same instant in time.
16
+
* Time pulses received from an ultrasound sensor.
17
+
18
+
There are two steps that you will need to go through to implement and use your custom commands:
19
+
20
+
21
+
## Step 1: Add your command to the Arduino firmware
22
+
23
+
To extend the SR firmware, you will need to first download its [source code]({{ site.baseurl }}/resources/kit/arduino-fw.ino), and edit it in the Arduino IDE.
24
+
When the SR Arduino python library wants the Arduino to run a command, it sends it a single character to tell it which command to run.
25
+
You'll find a `switch` statement in the `loop()` function that processes this command character:
26
+
27
+
~~~~~cpp
28
+
switch (selected_command) {
29
+
case 'a':
30
+
command_analog_read();
31
+
break;
32
+
case 'r':
33
+
command_read();
34
+
break;
35
+
case 'l':
36
+
command_write(LOW);
37
+
break;
38
+
39
+
// ... and so on ...
40
+
~~~~~
41
+
42
+
For example, you can see in the above code that when it receives an "a" character, it calls the `command_analog_read()` function.
43
+
This function does pretty much what it says on the tin: it reads an analog pin.
44
+
45
+
You will need to add your own entry into this `switch` statement for your new command.
46
+
This will need to be represented by a character that doesn't already appear in the switch statement.
47
+
Let's say you chose "s"; your entry would look like this:
48
+
49
+
~~~~~ cpp
50
+
switch (selected_command) {
51
+
case 's':
52
+
command_read_sensor();
53
+
break;
54
+
55
+
// ... all the original entries ...
56
+
~~~~~
57
+
58
+
You would then write your `command_read_sensor()` function, which would implement reading the sensor.
59
+
Your function can read additional data from the serial port if it requires additional information to operate.
60
+
It can also write a response back to the host (your Python code).
61
+
Have a look at the `command_read()` function to see how to do this.
62
+
63
+
64
+
## Step 2: Use your new command from Python
65
+
66
+
You can send a custom command from your Python code to the Arduino to read the sensor.
67
+
68
+
~~~~~python
69
+
sensor_data = robot.arduino.command("s")
70
+
~~~~~
71
+
72
+
The `sensor_data` variable will contain any response from your firmware, if you sent one.
73
+
74
+
You're done!
75
+
You can now use your custom firmware that can read from a sensor.
76
+
77
+
If you have multiple Arduinos running custom firmware, you can keep track of which one is which by using the serial number.
0 commit comments