-
Notifications
You must be signed in to change notification settings - Fork 0
Example Python skill script (for provided Arduino sketches)
Because I only have a Arduino to communicate with for now I only wrote a few Arduino sketches ArduinoObject/
. Here I will try to explain how to communicate with those specific sketches from a skills/your_function.py
script (Remember those naming conventions we use for script, etc!!)
You can however write your own sketches for more specialistic Arduino/other device functionality. The provided Sketches just intercept serial communication and act on that. It's a sort of passthrough.
An example script/function looks like this. Assuming that there is a LED connected on the PWM pin 9 of the Arduino:
# Mandatory imports
import sys
import serial
# Imports used in your function
from time import sleep
def test_function(arg_1, arg_2, arg_3, arg_4):
serial_port = str(arg_1)
baud_rate = int(arg_2)
time_out = int(arg_3)
parameter = int(arg_4)
device = serial.Serial(serial_port, baud_rate, timeout=time_out)
x = 0
for i in range(0, parameter):
while x < 255:
device.write('1/1/9/' + str(x) + '/')
sleep(0.000005)
x += 1
sleep(0.000005)
sleep(0.000005)
while x > 0:
device.write('1/1/9/' + str(x) + '/')
sleep(0.000005)
x -= 1
sleep(0.000005)
sleep(0.000005)
# Tip always write the pin back to 0.
# Otherwise it could be that the last value
# send is sticking.
device.write('1/1/9/0/')
device.close()
if __name__ == "test_function":
test_function(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
A little deeper look:
The function parameters:
def test_function(arg_1, arg_2, arg_3, arg_4):
Are being past from the IRCBot main script. The Arguments are arg_1 = serial_port
, arg_2 = baud_rate
, arg_3 = time_out
and arg_4 = parameters
.
The parameters that are passed are the ones you coded into the Brain.csv. In this example we only pass one parameter to control how many times this function needs to loop:
for i in range(0, parameter):
Let's say you want to pass multiple parameters. You could do something like:
In the Brain.csv make a parameter that looks like x/y/z/128/
you catch that parameter and split it into an array and distribute that to individual variables that you can accordingly use in your function:
parameter_array = parameter.split('/')
parameter_x = parameter_array[0]
parameter_y = parameter_array[1]
parameter_z = parameter_array[2]
parameter_value = parameter_array[3]
With the code:
device = serial.Serial(serial_port, baud_rate, timeout=time_out)
We initiate a serial communication port to use (pySerial)
How the communication to the provided Arduino sketches looks like:
device.write('1/1/9/0/')
or when you want to pass a variable
device.write('1/1/9/' + str(x) + '/')
Variables need to be passed as strings, so wrap them in str(x)
.
The ultimate string that is being send to the Arduino is:
w/x/y/z/
w = A value of 1
means, treat the string as a writing command. A value of 2
means treat it as a read command.
x = A value of 1
means, do an analog read/write. A value of 2
means treat it as a digital read/write
y = Is the pin number on the Arduino you want to use
z = The value you want to use to write. This can range from 1 to 255 for analog write's and 1(LOW) or 2(HIGH) for digital write's (It was not convenient to use 0's and 1's for digital write's)
A read command triggers a serial.println(var)
in the Arduino sketch. You can catch those by readline():
x = device.readline()
The last few lines in the script are implemented because it provides a convenient way to execute a script/function from a Terminal.
if __name__ == "test_function":
test_function(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
You now can call the script with the arguments serial_port
, baud_rate
, time_out
and a specific value
Open a Terminal:
$ cd your/path/to/the/directory/of/the/script
$ python test_function.py 'dev/your/serial/port' '115200' '2' '3'
This can be very handy for development purposes. No need to start the main.py script over and over again.