Sparki / Python Library

[quote=“Stephen”]Hi, I want to send commands from my android device to Sparki using sparki_myro. What are the commands and the string format required to send to Sparki. Please advise.

Thank you very much.[/quote]

That’s great! When you get the code done, please share with us.

The broad logical sequence will go like this for your program:

  1. Wait for the sync character (which is ASCII character 22) – you can find an ASCII table here: http://www.asciitable.com/
  2. Send the command code, the terminator character (ASCII 23), and any arguments, each followed by the terminator character
  3. If there is a return value, read those back.

All arguments and return values are sent as characters. So, if you sent the value 3.1415 – it gets sent as the characters 3.1415 and not some sort of floating point value.

So, to give the command to beep at frequency 440Hz for 2 seconds, you would send:
b [TERM] 440 [TERM] 2000 [TERM]
Where [TERM] is ASCII character 23. Each of the numbers would be sent as the bytes for 4, 4, 0 and then 2,0,0,0

If you wanted to, say, read the line sensors, you would send:
m [TERM]
and then from your Bluetooth port, you will read the values from all of the line sensors, which would be something like this:
400 [TERM] 0 [TERM] 25 [TERM] 4354 [TERM] 123 [TERM]
and that would be the sensors from left to right.

The commands can be found in the sparki_myro.ino file (https://github.com/radarjd/sparki_learning/blob/master/sparkiduino/sparki_myro/sparki_myro.ino). That file lists the character which is the command code, and tells what arguments the sparki is expecting. The section is commented with COMMAND CHARACTER CODES.

At present, the status light turns on when the Sparki is receiving or processing a command. If you need to debug things, you can undefine the NO_DEBUGS by commenting out what is presently line 34. The problem is that the debugs take up a lot of space, and so usually when I’m debugging, I have to play with particular areas.

Let me know if I can be of additional help. I’ve never done in Android programming, so I’m not sure I can assist much with that, but I can help on the Sparki side.

Thanks for your quick response. I will try it out and let you know if it is successful. Thank you very much,

Hi,

What commands should I send if I want the robot to move forward or backward forever with maximum speed? Please advise. Thank you.

Hi,

What commands should I send if I want the robot to move forward or backward forever with maximum speed? Please advise. Thank you.

[quote=“Stephen”]Hi,

What commands should I send if I want the robot to move forward or backward forever with maximum speed? Please advise. Thank you.[/quote]

The motors() command is used for those. The first argument to motors is the left wheel speed, the second the right wheel speed, and the third is the time. The first two arguments should be ints between -100 and 100. The third argument (time) should be a float. If the third argument is negative, the robot will move until it receives a stop(). So, forward at full speed would be:
A [TERM] 100 [TERM] 100 [TERM] -1 [TERM]

backward at full speed would be:
A [TERM] -100 [TERM] -100 [TERM] -1 [TERM]

spin left at full speed would be:
A [TERM] -100 [TERM] 100 [TERM] -1 [TERM]

spin right at full speed would be:
A [TERM] 100 [TERM] -100 [TERM] -1 [TERM]

to stop, send:
K [TERM]

Hi Jeremy, I have successfully controlled Sparki using the “motors” command on my android phone. I will be exploring and testing more commands using “App Inventor” on my android device. Thanks for your help and support.

I have released another new version on github, which should also be up on pypi. The github code can be found here: https://github.com/radarjd/sparki_learning

To get the python code via pypi, you can use:

Obviously, for that to work, you’ll need pip somewhere in your path.

This release is primarily code cleanup. I’ve switched to use of tuples instead of lists in a few places where tuples are more appropriate. I’ve worked on the documentation more – I’ll be using Sparki to teach a python course in the fall, so I want that to be student ready.

There really aren’t many more things I want to implement with the library. I’d like to have speaking using a third party library, but I can’t find something that’s cross platform and reliable. If anyone has any suggestions, I’d be happy to consider them. Also, if you find any bugs, please let me know!

Hi all, I’ve uploaded a new version with some better error handling when connecting to Macs. Macs seem to drop the Bluetooth connection if no data is transmitted for a few seconds – I’m uncertain if this is due to the Bluetooth driver, or pyserial, or hardware. I don’t think it’s my library’s fault, but I’m not certain of that either :slight_smile:

In any case, I’ve swept the issue under the rug by writing in some error handling to make the library realize there’s a timeout early and try to re-initialize immediately. It appears, in my testing so far, to work. The Mac is not my primary development platform, and I am not as familiar with it.

If you can, I’d suggest you upgrade or install using pypi. For example:

(for a new install)
or

(for an upgrade of an existing installation)

The code also remains available on github at: https://github.com/radarjd/sparki_learning

If you have any suggestions or bugs, please let me know.