Arduino Driver guide: Difference between revisions
Jump to navigation
Jump to search
(Created page with "Arduino can easily be combined with SweepMe! However, there are some guidelines which make the implementation much easier: # If the Arduino sends a text during its setup() ro...") |
No edit summary |
||
Line 1: | Line 1: | ||
Arduino can easily be combined with SweepMe! However, there are some guidelines which make the implementation much easier: | Arduino can easily be combined with SweepMe! However, there are some guidelines which make the implementation much easier: | ||
# | # Try to sends a text during the setup() routine of the Arduino, so that SweepMe! knows that sending commands can be done from now on. | ||
# The Arduino can send text via its function pringln() which uses a LineFeed character as terminator ("\n"). Thus, the Arduino should also expect to receive messages ending with a LineFeed ("\n") | # The Arduino can send text via its function pringln() which uses a LineFeed character as terminator ("\n"). Thus, the Arduino should also expect to receive messages ending with a LineFeed ("\n") so that the write and read terminators are identical. The standard terminator of SweepMe! is a LineFeed ("\n") so that you do not have to set it. | ||
# If the Arduino sends a message back after receiving a command, do send more than one line. Otherwise, one has to read out multiple times in SweepMe! Device Class | # If the Arduino sends a message back after receiving a command, do not send more than one line. Otherwise, one has to read out the COM port multiple times in the SweepMe! Device Class | ||
# Try to make every parameter adjustable by a single command, e.g. "Pixel = 03" | # Try to have a constant length or format of the return message, so that reading out the string is less errorneous | ||
# Use the standard baudrate of 9600 so that other Arduino Device Classes can easily be copied and modified | # Try to make every parameter adjustable by a single command, e.g. "Pixel = 03", "Voltage = 0.05", or "Frequency = 400". That way, multiple items of the sequencer can communicate with the Arduino and multiple parameters can be swept. | ||
# Use the standard baudrate of 9600 so that other Arduino Device Classes can easily be copied and modified. | |||
{{syntaxhighlight|lang=python|code= | |||
from EmptyDeviceClass import EmptyDevice # Loading the EmptyDevice Class | |||
class Device(EmptyDevice): # Creating a new Device Class by inheriting from EmptyDevice | |||
def __init__(self): # The python class object need to be initialized | |||
EmptyDevice.__init__(self) # Finally, the initialization of EmptyDevice has to be done | |||
self.variables = ["A variable to read out"] | |||
self.units = ["The unit of the variable"] | |||
self.plottype = [True] # set to False if the variable is a string and cannot be plotted | |||
self.savetype = [True] # set to False if there is no need to save the variable to the data file | |||
self.port_manager = True # using the port manager works seamlessly | |||
self.port_types = ["COM"] # Arduinos are always listed as COM ports | |||
self.port.properties = { | |||
"timeout" : 2, # set timeout of the COM port to 2 seconds, which is necessary to prevent timeout error during initialization | |||
} | |||
def initialize(self): | |||
self.port.read() # readout the COM port to know that the Arduino is ready to receive commands | |||
def apply(self): | |||
print self.value # self.value is always the actual value to be set, print to see exact formatting | |||
self.port.write("Pixel = " + str(self.value)) # here we send the new command to the Arduino | |||
self.port.read() # read out the COM port to know that the Arduino has finished setting the parameter. Only use it if the Arduino sends a response. | |||
def measure(self): | |||
self.port.write("R?") # send some command to request a reading of parameters, for example "R?" or any other command. Only use it if the Arduino can read parameters | |||
def call(self) | |||
self.answer = self.port.read() # read out the COM port | |||
return [float(self.answer)] # return the answer to SweepMe!. Do not forget to transform the string to any format as needed e.g. float or integer | |||
}} |
Revision as of 15:05, 20 February 2018
Arduino can easily be combined with SweepMe! However, there are some guidelines which make the implementation much easier:
- Try to sends a text during the setup() routine of the Arduino, so that SweepMe! knows that sending commands can be done from now on.
- The Arduino can send text via its function pringln() which uses a LineFeed character as terminator ("\n"). Thus, the Arduino should also expect to receive messages ending with a LineFeed ("\n") so that the write and read terminators are identical. The standard terminator of SweepMe! is a LineFeed ("\n") so that you do not have to set it.
- If the Arduino sends a message back after receiving a command, do not send more than one line. Otherwise, one has to read out the COM port multiple times in the SweepMe! Device Class
- Try to have a constant length or format of the return message, so that reading out the string is less errorneous
- Try to make every parameter adjustable by a single command, e.g. "Pixel = 03", "Voltage = 0.05", or "Frequency = 400". That way, multiple items of the sequencer can communicate with the Arduino and multiple parameters can be swept.
- Use the standard baudrate of 9600 so that other Arduino Device Classes can easily be copied and modified.
from EmptyDeviceClass import EmptyDevice # Loading the EmptyDevice Class
class Device(EmptyDevice): # Creating a new Device Class by inheriting from EmptyDevice
def __init__(self): # The python class object need to be initialized
EmptyDevice.__init__(self) # Finally, the initialization of EmptyDevice has to be done
self.variables = ["A variable to read out"]
self.units = ["The unit of the variable"]
self.plottype = [True] # set to False if the variable is a string and cannot be plotted
self.savetype = [True] # set to False if there is no need to save the variable to the data file
self.port_manager = True # using the port manager works seamlessly
self.port_types = ["COM"] # Arduinos are always listed as COM ports
self.port.properties = {
"timeout" : 2, # set timeout of the COM port to 2 seconds, which is necessary to prevent timeout error during initialization
}
def initialize(self):
self.port.read() # readout the COM port to know that the Arduino is ready to receive commands
def apply(self):
print self.value # self.value is always the actual value to be set, print to see exact formatting
self.port.write("Pixel = " + str(self.value)) # here we send the new command to the Arduino
self.port.read() # read out the COM port to know that the Arduino has finished setting the parameter. Only use it if the Arduino sends a response.
def measure(self):
self.port.write("R?") # send some command to request a reading of parameters, for example "R?" or any other command. Only use it if the Arduino can read parameters
def call(self)
self.answer = self.port.read() # read out the COM port
return [float(self.answer)] # return the answer to SweepMe!. Do not forget to transform the string to any format as needed e.g. float or integer