Skip to content
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

Merged
merged 20 commits into from
Feb 15, 2018

Conversation

PeterJCLaw
Copy link
Contributor

@PeterJCLaw PeterJCLaw commented Jan 31, 2018

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:

  • understanding (and documenting) what happens to values which are sent as numbers (either float or integer)
  • what kind of Arduino is it?

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.
@PeterJCLaw PeterJCLaw force-pushed the generic-arduino-commands branch from 998e7b1 to 75ec553 Compare January 31, 2018 16:13
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
Copy link
Member

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

Copy link
Contributor Author

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>
Copy link
Member

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 */}}

Copy link
Contributor Author

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!

Copy link
Contributor Author

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.

Copy link
Contributor Author

@PeterJCLaw PeterJCLaw Jan 31, 2018

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.

Copy link
Member

@RealOrangeOne RealOrangeOne Feb 2, 2018

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!

@@ -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)
Copy link
Member

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.

Copy link
Contributor Author

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!

Copy link
Contributor Author

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
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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
Copy link
Member

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.

Copy link
Contributor Author

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.
Copy link
Member

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?

Copy link
Contributor Author

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
Copy link
Member

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)

Copy link
Contributor Author

@PeterJCLaw PeterJCLaw Feb 2, 2018

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?

Copy link
Member

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
Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Member

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")
Copy link
Member

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?

Copy link
Contributor Author

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 '.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

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.
-->
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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:
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good spot: 422a0f8

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#89

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:
Copy link
Member

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

Copy link
Contributor Author

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).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/like/line/?

Copy link
Contributor Author

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
Copy link
Member

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

Copy link
Contributor Author

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).
Copy link
Member

@kierdavis kierdavis Feb 2, 2018

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.

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

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
Copy link
Member

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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"an" -> "and"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PeterJCLaw PeterJCLaw requested a review from kierdavis February 2, 2018 16:08
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? -->
Copy link
Member

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?

Copy link
Contributor Author

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.
@PeterJCLaw PeterJCLaw requested a review from kierdavis February 6, 2018 20:21
@RealOrangeOne RealOrangeOne mentioned this pull request Feb 6, 2018
Copy link
Member

@Adimote Adimote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

return OK;
}
```
<!-- TODO: mention being able to send back comments? -->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo message here?

@Adimote Adimote merged commit dc0ed32 into master Feb 15, 2018
@PeterJCLaw PeterJCLaw deleted the generic-arduino-commands branch February 15, 2018 22:22
PeterJCLaw added a commit to PeterJCLaw/sourcebots-docs that referenced this pull request Mar 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants