Skip to content

Programming

Béla Knopp edited this page Sep 18, 2023 · 5 revisions

Any setup requires some software for the Arduino and the controlling PC. Any .ino and .py files can be downloaded from GitHub repository cyclotron-bonn/irrad_control/irrad_control/devices/arduino. Each folder contains a .py file and another subfolder with a .ino file. Import the .py file into your script or use the functions directly via a terminal and upload the .ino file to the Arduino. Any communication with the Arduino is done via Serial using the library cyclotron-bonn/irrad_control/irrad_control/devices/arduino/arduino_serial.py. Additionally, code for the NTC readout can be found on basil repository.

Temperature Readout

Choose the ntc_readout folder and set up the devices according to the introduction. Make sure to choose the correct PCB setup. In order to read temperatures from any single or list of sensors use:

get_temps(sensor):
"""Gets temperature of sensor where 0 <= sensor <= 7 is the physical pin number of the sensor on
     the Arduino analog pin. Can also be a list of ints."""

To increase accuracy it is possible to take multiple samples:

n_samples(n_samples):
"""Set the amount of samples to take"""

Reading the current setting is done using the same method without input:

n_samples():
"""Returns the current number of n_samples"""

Sampling is done on the IC itself.

Communication via I2C

You can use this mode very freely as the Arduino just translates your input and any programming does not make any suggestions about how it should be done. There are some constraint still:

  • Incoming serial data is to be limited to 32 bytes.
  • Always end your message with "\n".
  • Different parts of a message (e.g. address, command, value etc.) can be transmitted in a single line if they are separated with a ":".

Additionally, there is a library which implements rather simple functions for Serial Communication and features a general approach to command creation as described before. The I2C address can be manipulated or read using:

i2c_address():
"""return the current i2c address the Arduino is communicating to"""

i2c_address(addr):
"""set new i2c address"""

The Arduino is limited to storing one address. Communication with multiple devices requires a change of address every time.

Communication with the I2C peripheral is done with:

query_i2c(self, msg):
     """
     Queries a message *msg* and reads the i2c return code.
     Checks the return code of the Arduino Wire.endTransmission.
     Additional data after the query can be retrive using a self.read

     Parameters
     ----------
     msg : str, bytes
         Message to be queried

     Returns
     -------
     str
         I2C return code as in self.I2C_RETURN_CODES

     Raises
     ------
     NotImplementedError
         return_code is unknown
     I2CTransmissionError
         dedicated error code from Wire library
     """

read_register(self, reg):
     """
     Read data from register *reg*

     Parameters
     ----------
     reg : int
         Register to read from

     Returns
     -------
     int
         Data read from *reg*
     """

 write_register(self, reg, data):
     """
     Write *data* to register *reg*

     Parameters
     ----------
     reg : int
         Register to write to
     data : int
         Data to write to register *reg*
     """

Frequency Counting

The application of frequency counting has only a few functions:

gate_intervall():
   """
   Sampling time during which is counted in ms
   Returns
   -------
   int
      Sampling time in milliseconds
   """

gate_interval(self, gate_interval):
   """
   Setter of the gate interval property

   Parameters
   ----------
   gate_interval : int
      Gate interval in milliseconds

   Raises
   ------
   RuntimeError
       Set gate interval and retrieved interval are unequal
   """

To read how many hits were counting in given gate interval use:

counts()

One can also read the frequency of hits using:

frequency()
Clone this wiki locally