-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add documentation for custom arduino commands #87
Conversation
This includes a summary of how to create the command on the Arduino as well as how to call it from Python and the errors which could be thrown.
I was going to use this to show how to do comments, but I think it's probably better that we don't do that for now.
998e7b1
to
75ec553
Compare
running on the Raspberry Pi, you'll need to base it on SourceBots default | ||
firmware. You can get a copy of the source code from GitHub: | ||
|
||
```none |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use txt
here? I'm not even sure none
is a valid highlight
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
git clone https://github.com/sourcebots/servo-firmware | ||
``` | ||
|
||
<TODO: describe how to do that without git> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use Go templates comment syntax rather than this or the html comments below.
{{/* a comment */}}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha, thanks. I searched hugo's docs for what the comment syntax was, but couldn't find it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
5328e1d resolves this TODO outright; I'll changes the other comments shortly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turns out that {{/* a comment */}}
unfortunately doesn't work for me. I suspect it might be related to these being markdown sources, though it would be an odd behaviour if the comment stripper happened after the markdown conversion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turns out the actual reason for this not working is that Hugo doesnt run Go's templating engine over the content files, so never strips out the comments 🤦♂️ D'oh!
content/api/servo-assembly/_index.md
Outdated
@@ -7,6 +7,7 @@ The Servo Assembly is used to connect to multiple types of peripheral: | |||
- [GPIO pins](gpio) (digital and analogue) | |||
- [Servos](servos) | |||
- [Ultrasound sensors](ultrasound) | |||
- [Custom Arduino commands](arduino-commands) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this link fits here, as this isnt a peripheral that's communicated with through the arduino.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point; I'd not actually read the preceding line!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After both inspecting the source and testing, it appears that what I hoped would be the case (and originally documented) is indeed the case. In robotd we call `str` on each argument before sending them over serial, so numbers (etc.) are converted to strings. The alternative I'd wondered about was integers arriving a raw integer bytes, rather than as their ASCII representation.
firmware. You can get a copy of the source code from GitHub either by | ||
downloading the [repo archive](servo-firmware-zip) or by cloning the repo: | ||
|
||
```txt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would bash
(or similar) be a better choice than txt
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe, though that causes "clone" to be highlighted a different colour. I've no idea why -- as far as I know "clone" isn't a special word in any shell. I opted for it being less distracting even if it's slightly less correct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, weird. txt
is fine here in that case.
|
||
Here's a simple command which just echoes back any arguments it receives: | ||
|
||
```cpp |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should mention that this function definition must be placed before its use (the commands
array), unless they add a prototype for the function as well of course.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
b93eaf0 reworks the start of the "Implementing the command" section and includes this.
|
||
For other examples of how to define commands (and the helper functions you can | ||
use), you can have a look at the other commands the Arduino already supports. | ||
In particular the `get_version`, `led` and `servo` are a good starting point. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you missing the word "commands" after "servo" here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
almost: 932d341
The first argument is a quoted `String` literal containing the name you'll use | ||
in Python to call this command. It can contain letters, numbers and dashes (`-`). | ||
|
||
The second argument is the function you defined for your function, with an |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the function defined for your function
Should this say "the name defined for your function"? (IMO "the name of your function" would probably be more straightforward anyway)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I meant "the function defined for your command" (de16f25); do you think that that's clear enough that we mean the function as a symbol?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still say "name" later in the sentence, so I think that's clear enough 👍
@@ -8,6 +8,10 @@ The Servo Assembly is used to connect to multiple types of peripheral: | |||
- [Servos](servos) | |||
- [Ultrasound sensors](ultrasound) | |||
|
|||
At the core of the Servo Assembly is an Arduino. If you need more direct control |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big deal, but is there any reason why "Arduino" here isn't hyperlinked in the same way that the occurrences in the introductions to arduino-commands.md
and kit/servo-assembly/_index.md
are?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only that it didn't seem entirely useful to do so. Anyone wanting more detail will probably be on one of those pages anyway. Do we prefer to be over-linking?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough, good point.
`Servo` instance. You can send strings and numbers: | ||
|
||
```python | ||
r.servo_board.direct_command('echo', "first argument", 2, "third argument") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason for the mixing of quote types here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'echo'
is for the machine and is close to being an enum value or a slug. The others are less so, though are arguably still much more that than my usual use of double quotes (which is for text intended for humans).
They should probably all be '
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'd agree than in typical applications the arguments to the command won't be intended to be human-readable text - more likely to be numbers or keywords (e.g. 'low' / 'high').
At some point we might need to detail how to remove bits of the firmware | ||
which the teams aren't using. That's intentionally omitted for the moment | ||
as it's nontrivial to describe which bits are safe to remove. | ||
--> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haven't tested this but I would imagine that removing or commenting an entry in the commands
away should cause the corresponding handler function to be optimised out of the binary. This should produce some initial space savings, at least.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does indeed seem to happen. I think I'd still rather defer this for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's something for separate PR.
As your new firmware will still need to be able to talk to the Python code | ||
running on the Raspberry Pi, you'll need to base it on SourceBots default | ||
firmware. You can get a copy of the source code from GitHub either by | ||
downloading the [repo archive](servo-firmware-zip) or by cloning the repo: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parentheses -> square brackets
Currently this is resulting in a broken link to https://HOSTNAME/api/servo-assembly/arduino-commands/servo-firmware-zip.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good spot: 422a0f8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way we can test that all internal links go somewhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sure a broken link checker (e.g. broken-link-checker-local) would be a useful addition to the CI tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It turns out that Python will also use single quotes for strings too, so this also updates the printed return values.
This reworks these instructions to clarify where to put the function as well as provide more guidance about why some parts of the function need to be the way they are.
As your new firmware will still need to be able to talk to the Python code | ||
running on the Raspberry Pi, you'll need to base it on SourceBots default | ||
firmware. You can get a copy of the source code from GitHub either by | ||
downloading the [repo archive][servo-firmware-zip] or by cloning the repo: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps here, rather than specifically getting a zip or using git clone
, we just link to the repo. This means they can not only see the source without downloading it, but the discovery path for clone / download instructions is very straightforward
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
You'll need to define a function to implement your command. The function should | ||
be placed in the `firmware.cpp` file just above the definition of the `commands` | ||
array (likely about like 205). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/like/line/?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as it's nontrivial to describe which bits are safe to remove. | ||
--> | ||
|
||
[arduino]: https://store.arduino.cc/arduino-uno-rev3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we have done with almost all other links in the docs, can we use markdown links inline
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For cases where that causes the source to wrap oddly I tend to prefer separate links. 83266fe however does this.
|
||
You'll need to define a function to implement your command. The function should | ||
be placed in the `firmware.cpp` file just above the definition of the `commands` | ||
array (likely about line 205). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like line numbers are quite a volatile way to refer to parts of the code, since changes to the file can result in many of the line numbers changing. IMO better solutions are to either copy the line verbatim here so users can ctrl+f
for it, or just to say the rough location qualitatively (e.g. "about halfway through the file") - both of these methods are less likely to break when the file is modified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd hope that this wouldn't change very much, also hence the "about" on the line number. Given that we do include the definition of the commands array below I don't want to repeat it here.
Do you think we need to add a comment like "see below" for this, or can we assume people will be able to read the whole of the documentation to get a full understanding?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could just remove the remark - I think it should be easy enough to find commands
in the source file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's useful to have the line number; I think it's clear enough that it's approximate.
firmware. You can get a copy of the source code from GitHub either by | ||
downloading the [repo archive](servo-firmware-zip) or by cloning the repo: | ||
|
||
```txt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, weird. txt
is fine here in that case.
on an Arduino. | ||
|
||
The following example command shows how to handle arguments from the Pi as well | ||
as how to return errors an other values: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"an" -> "and"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In particular the `get_version`, `led` and `servo` functions are a good starting | ||
point. | ||
|
||
<!-- TODO should we error in robot-api/robotd if the incoming arguments include spaces? --> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't really related to the docs - could we break this out into an issue on robot-api
or a Trello card instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll see how this goes, if we find that this is needed (i.e: the lack of it causes problems) we can add it then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return OK; | ||
} | ||
``` | ||
<!-- TODO: mention being able to send back comments? --> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
todo message here?
This includes a summary of how to create the command on the Arduino as well as how to call it from Python and the errors which could be thrown.
See sourcebots/robot-api#55 and sourcebots/robotd#35 for the related implementations.
Note: there are some TODOs inline. The ones I think need fixing before we ship are: