Anytime Help Center

Basic MUSE Program

Rating

The following MUSE code is written in Python.  This code is a very simple example of using Python.  It contains a single button that turns a relay on and off.  The comments within the code explain the uses of each section, and in many case, they include information on how each section of code ​​relates to a Netlinx program.


#This line is used in all Python projects
#Tells the MUSE controller to import the context class from Mojo (MUSE platform)
from mojo import context

#Sets the log base log level for the program
context.log.level = "INFO"

#This prints out a message to the standard out of VSCode as long as the log
#level is set to INFO or higher
context.log.info("Program starting")

#Context is the class that contains much of the information related to the MU
#including creating, querying, and identifying devices  
#this line queries the MU if it has a device named dvTP
if(context.devices.has("dvTP")):
    #If dvTP exists, the next line creates an object named dvTP that is loaded with
    #all of the data the controller has about dvTP    
    dvTP = context.devices.get("dvTP")

#This check will determine if idevice exists on the processor, the idevice is like the 5001
#device in Netlinx.  It handles all local devices including serial, I/O, relay, and IR ports.
if(context.devices.has("idevice")):
    #If the device exists, we will create an object to contain all of the information relating to idevice
    idevice = context.devices.get("idevice")    

#Prints out a message to the standard out of VSCode, these messages are good because
#it allows you to see what point your code is at and if it successfully made it to this point.
#This would be considered the end of the old DEFINE_DEVICE section of a Netlinx program  we just print this out to let us know the 
#program has made it this far.
context.log.info("DEFINE_DEVICE Complete")

#This creates a variable named relay1 that will be used to represent the first relay by referencing the idevice object's relay array using idevice.relay[0]
relay1 = idevice.relay[0]

#Creates a callback function named buttonEvent that is passed the event object from the watch 
#that calls the function.  This would be akin to the PUSH
def buttonEvent(event):

    #The following lines print out a dictionary of the event object, this will show you the kind of information you 
    #can receive from the event.
    dictionaryPrint = str(event.__dict__)
    context.log.info(dictionaryPrint)
    #Output example: {'source': , 'id': '1', 'path': 'port/1/button/1', 'value': True, 'newValue': True, 'oldValue':False}

    #This if statement evaluates the current state of value in the object.  If it is True, that is considered to be a PUSH,
    #if the value is False, that is a RELEASE of the button.
    if(event.value):
        #Prints out a simple string to the standard out
        context.log.info("PUSH on button 1")

        #This if statement evaluates the state of Relay 1 on the MU controller, if it is on we will turn it off as well as turn the button off and vice versa for when it is off
        if(relay1.value):

            #This sets the value for Relay 1 to false, thus disabling the Relay.
            relay1.value = False

            #When we turn off the Relay, we also want to show it on the touch panel.  Setting the channel on the button to False will change the button on the panel to the "OFF" state
            #This is similar to doing OFF[dvTP,1] in Netlinx
            dvTP.port[1].channel[1].value = False
        
        else:
            #This sets the value for relay1 to True, doing this will turn on Relay 1 on the MU controller, this is similar to doing ON[dvrelay,1] in Netlinx
            relay1.value = True

            #When we turn on the Relay, we also want to show it on the touch panel.  Setting the channel on the button to True will change the button on the panel to the "ON" state
            #This is similar to doing ON[dvTP,1] in Netlinx
            dvTP.port[1].channel[1].value = True
    
    #We are not going to trigger anything based on the RELEASE of the button, but this log statement will at least inform us the event has occurred
    else:
        context.log.info("RELEASE on button 1")

        


#This line creates a 'watch' for button 1 on port 1 of dvTP,
#this is very similar to creating a BUTTON_EVENT in Netlinx studio.
#When any 'event' is triggered by that button (push/release), the event object is passed on to a 
#callback function named buttonEvent that will process the event itself.
#The line is placed under the actual function definition, because without it the 
# program doesn't recognize buttonEvent.
dvTP.port[1].button[1].watch(buttonEvent)

#Lets you know that the program has loaded completely
context.log.info("Program loaded completely!!!")

​​

Downloads

Product

 

Topic

Programming

Related Articles

Last modified at 8/5/2024 3:15 PM by PRO Knowledge Base
Top