SmartTerminal for Controlino

From OpenCircuits
Jump to navigation Jump to search

What[edit]

This is part of the Instrumentino/Smart Terminal Challenge. In this part I am going to try to adapt the SmartTerminal ( PC/Python ) Python Smart Terminal to the ( microcontroller ) Controlino program Controlino Notes. The code for this extension is in ext_process_co.py. This documentation is written as if you were writing ext_process_co.py starting with ext_process_gh.py' as a sort of template ( copy and modify ). And as I try to bring this up to date I see that a lot has changed and this needs a top to bottom rewrite. Email me if you need this information.

First Connection[edit]

For your first connection we will just try to establish a basic connection between the two programs ( Python and Arduino ), typically this involves matching the communications parameters. Baud rate is, perhaps, the most basic. At this point we will not write any code, but just adjust the parameter program ( remember all this work is already done in the code at git hub, this is a recreation story ).

Steps:

  • go to Arduino Controlino program find baudrate #define SERIAL0_BAUD 19200
  • go to SmartTerminal parameters.py and define a new mode Controlino: ( this code is near the top of parameters.py and you will see a bunch of modes defined there, likely with several comented out, remember the lats value set is the one that wins. Your code should then look something like:
     # self.mode              = "GreenHouse"  # add sensor processing, monitoring and logging for greenhouse sensors
     # self.mode              = "IR"
     # self.mode              = "MotorDriver"  # special buttons, send arrays for motor controller arduino
     self.mode              = "Controlino"   # define mode for Controlino
   
    • defining a mode this way using a subroutine ... is not really the way I do it when this documentation was written .. it is badly out of date. is not really needed but this way it is easy to turn on and off the Controlino settings.
    • find self.baudrate and add at end of baud rate section
    if self.mode == "Controlino": 
        self.baudrate          =       19200   # match to the Arduino program 

this way of setting the baud rate will only be in effect for the mode Controlino which, for now, we will implement as a straight terminal

Install Controlino on your arduino and fire up both the SmartTerminal and the arduino. Open the port and send "set" the arduino responds with "done", the eagle has landed.

Add strings for commands to interface[edit]

  • look at the bottom of the C program and extract the following commands: "Set", "Controlino", "BlinkPin", "Read", "Write", "SetPwmFreq"
  • find the section in Parameters ( in parameters.py ) that looks something like:
    elif self.mode == "MotorDriver":
        self.send_ctrls = [ ...

and add a clause to the if then section like:

    elif self.mode == "Controlino":
        self.send_ctrls = [ "Set", "Controlino", "BlinkPin", "Read", "Write", "SetPwmFreq" ]

note that this is just setting self.send_ctrls to a list of strings.

Run the SmartTerminal again, this time you will find the above commands loaded into the first several send areas as defaults. Try them out, all but "Controlino" respond with done. Invalid commands also seem to get no response. These commands look like they are supposed to have numbers appended as well, it would be nice if we had some values in place just to let you know what the expected form is.

Try Blink[edit]

  • Blink using BlinkPin looks like it might be easy to use and test so lets look at it.
  • the command seems to link to cmdBlinkPin(argV) and searching on it we find
void cmdBlinkPin(char **argV) {
	blinkingPin = strtol(argV[1], NULL, 10);
	blinkingDelayMs = strtol(argV[2], NULL, 10);

	pinMode(blinkingPin, OUTPUT);
	blinkLastChangeMs = millis();
	startBlinking = true;

This in turns suggests the command "BlinkPin 5 300" will blink pin 5 with a delay ( half period or full period ) of 300 ms. Looking at my board it is very easy to put in a led on pin 7 so lets try "BlinkPin 7 300" as a command.


Back to

elif self.mode == "Controlino":
        self.send_ctrls = [ "Set", "Controlino", "BlinkPin", "Read", "Write", "SetPwmFreq" ]

lets change it to

elif self.mode == "Controlino":
        self.send_ctrls = [ "Set", "Controlino", "BlinkPin 7 300", "Read", "Write", "SetPwmFreq" ]

and running SmartTerminal again try the button for "BlinkPin 7 300" and it works.

Try Blink More[edit]

Looking at Controlino code I do not see any way to turn of the blinking ( beyond a reset ) "BlinkPin 7 0" might be a way to implement it. So what I will try to implement is a program that runs blink at 250 ms for 30 seconds then at 1000 ms for 30 sec and then repeats forever. First lets put the 2 commands on 2 buttons:

elif self.mode == "Controlino":
        self.send_ctrls = [ "Set", "Controlino", "BlinkPin 7 250", "BlinkPin 7 1000", "Read", "Write", "SetPwmFreq" ]

Run the SmartTerminal again, open the port and try the two buttons, they work for me, but this is not really automated.

Note if you have set up the SmartTerminal with a parameter for text file editing ( see: .... ) you can edit the parameter file with the <Edit Parms> button in the terminal, save the file and then restart the SmartTerminal with the new parameters by pressing <Restart> button.

Automation[edit]

Make ext_process_co.py[edit]

Here we will automate the above. This is done by creating a class called ControlinoProcessing in ext_process_co.py and inserting/editing the code. To make this easier we will copy another processing unit of code. Copy ext_process_gh.py and save as ext_process_co.py

Edit Parameters[edit]

Back to parameters.py

  • Find the section containing
        elif self.mode == "GreenHouse":
            print( "parameter says GreenHouse" )
            self.ext_processing_module      = "gh_processing"
            self.ext_processing_class       = "GHProcessing"

and add a section in the "if then" like

        elif self.mode == "Controlino":
            print( "parameter says Controlino" )
            self.ext_processing_module      = "co_processing"
            self.ext_processing_class       = "ControlinoProcessing"
  • Now lets edit the new co_processing.py:
  • Find
# ================= Class =======================
#
class GHProcessing( abc_def.ABCProcessing ):

and change it to

# ================= Class =======================
#
class ControlinoProcessing( abc_def.ABCProcessing ):

Run the terminal again. It should run fine with some new buttons just below the <Open> <Close> ... buttons that have been around all along. Our job now is to modify the buttons and the code behind them.

Delete many co_processing methods[edit]

Back into co_processing.py:

  • GreenHouse Processing is fairly complicated, this is fairly simple so first we will rip out a lot of code:

( I have done some of the work here, you can start from a copy of co_processing_bak.py instead of gh_processing )

  • since so much code will go this is a list of the methods you should keep:
    • __init__
    • add_gui
    • set_time
    • find_and_monitor_arduino
    • cb_find_and_monitor_arduino
    • cb do_end_helper
    • send_rec -- russ move to helper
  • keep the class ButtonAction


Clean __init__[edit]

There is a lot of junk values in init that come from parameters. All we really need are ( notice also a few edits of the string for ControlinoProcessing :

        self.logger        = logging.getLogger( self.controller.logger_id + ".ControlinoProcessing")
        self.logger.debug( "in class ControlinoProcessing init" ) # logger not currently used by here
        self.time          = time.time() # set in set_time -- taken as same for all measurements

Run again, again it should start up fine. Do not try the buttons, we need some fixing first.

add_gui[edit]

The function of this method is to create a frame, populate it with widigits, link the widgets to code, and finally return the frame.

To do this you need to know or research a bit of Tkinker coding.

I am going to give directions using the same style of Tkinker coding that I have used, but other style can also work, you just have to make sure the widgets link to the correct code.