LEO Software

Possible to implement 3 GPIO Buttons in comming Firmware ?

1 = ON/ OFF over Webbrowser
2 = ON/ OFF over Webbrowser
3 = Pulse ON /OFF 0,3s

Need for Lighting System.

Comments

  • We're not planning to add such functionality at the moment, but you can implement new features by yourself. Let me know and I can guide you through it.

  • Thats nice. I have have no plan :-)

    When you have time, you can write a litle guide. I will implement by self.

  • You will first need to prepare your environment:

    1. Install git and Visual Studio Code.
    2. Open Visual Studio Code and install Husarion extension ([Ctrl]+[Shift]+[X] and find "Husarion"). Restart Code
    3. Using git, clone leo_firmware repository and add it to your workspace in Code.
    4. Check if you can succesfully build the project. Open main.cpp file and type [Ctrl]+[Shift]+[B]. If everything is correct, leo_firmware.hex file should appear in your workspace.

    Now you can start to modify the firmware to your needs.
    Let's start with the 3rd option. I'm assuming you want an output pin which changes the state (between high and low) every 0.3s

    Put this function before hMain:

    void GPIO3Loop()
    {
        // Sets pin 2 on hExt port mode to output
        // pin 1 is already used for informative LED (you can change it in param.h file)
        hExt.pin2.setOut();
    
        // Gets system time
        uint32_t t = sys.getRefTime();
    
        // Specifies the pulse duration in ms
        long dt = 300;  
    
        while(true) //infinite loop
        {
            // waits for the next pulse cycle
            sys.delaySync(t, dt);
    
            // changes state on pin to opposite
            hExt.pin2.toggle();
        }
    }
    

    In hMain, before while loop, type:

    sys.taskCreate(&GPIO3Loop);
    

    Build the project and flash it. You can flash it from raspberry pi like described here or using Husarion extension in VSC ([Ctrl]+[Shift]+[P] and select "Flash project to CORE2")

    Now you can connect your electric circuit. You can read more about the board's pinout here

    If this guide was helpful to you, I can write a second one (about the 1st option)

  • Thanks for the instructions

    However, it's just about a simple I / O control. Controllable via the HTML page. 2GPIO with ON / OFF button and the third Gpio only as Pulse. Quasi Button pressed = GPIO High ---> Button Release = GPIO LOW. I've got that on my module relatively easy with Flask via Websocket. So without firmware ...... That's the real problem. Nowhere is it clear how to do it. With Flask and Python no problem - but via Husarion ?????.

  • When i "RUN BUILD TASKS" in VSC --- Is the HEX File also automatic updated ?

  • When i "RUN BUILD TASKS" in VSC --- Is the HEX File also automatic updated ?

    Yes

    However, it's just about a simple I / O control. Controllable via the HTML page. 2GPIO with ON / OFF button and the third Gpio only as Pulse. Quasi Button pressed = GPIO High ---> Button Release = GPIO LOW.

    I get that you want to do WebUI <-> firmware communication (I just wanted to start with something simpler).

    We use ROS to send messages components, so you need to write a ROS subscriber in your firmware.

    First, we need to include the message type you want to use. The most logical would be to use a boolean value (true -> GPIO High, false -> GPIO Low). So add this line at the top of main.cpp file (after including ros.h):

    #include "std_msgs/Bool.h"
    

    Declare ROS subscriber that takes Bool messages:

    ros::Subscriber<std_msgs::Bool> *gpio2_sub;
    

    Add the following function before initROS:

    void gpio2Callback(const std_msgs::Bool& msg)
    {
        hExt.pin2.write(msg.data);
    }
    

    The function will be called for every received message. msg.data is the boolean value.
    Now, we need to initialize the subscriber. In initROS add:

    gpio2_sub = new ros::Subscriber<std_msgs::Bool>("/gpio2", &gpio2Callback);  
    nh.subscribe(*gpio2_sub);
    

    We also need to set the pin to output mode. In hMain add this line:

    hExt.pin2.setOut();
    

    Here's a modified main.cpp: https://pastebin.com/2GChWacq
    Build and flash the firmware.

    Before adding the buttons to WebUI, let's test if the new functionality works.
    Log in to your Raspberry Pi and type:

    rostopic list
    

    You should see a list of available topics. The /gpio2 topic should appear in this list. You might need to restart ROS nodes (sudo systemctl restart leo).
    To set GPIO to High state, type:

    rostopic pub /gpio2 std_msgs/Bool -- True
    

    For low state, change True to False

    Now for the Webui, we use roslibjs to send ROS messages to RPi (I'm assuming you use leo_ui and not your custom UI).
    You will need to edit leo.js file (should be located in /opt/leo_ui/js/).

    To initialize ROS publisher, declare variable at the top of the file:

    var gpio2Pub;
    

    And in initROS add the following:

    gpio2Pub = new ROSLIB.Topic({
        ros: ros,
        name: "/gpio2",
        messageType: `std_msgs/Bool`,
        queue_size: 1
    });
    gpio2Pub.advertise();
    

    And add somewhere these functions:

    function gpio2High() {
        msg = new ROSLIB.Message({
            data: true
        });
        gpio2Pub.publish(msg);
    }
    
    function gpio2Low() {
        msg = new ROSLIB.Message({
            data: false
        });
        gpio2Pub.publish(msg);
    }
    

    Now, you can open the UI in your browser, open Developer Console ([Ctrl] + [Shift] + [J] in google chrome) and run these functions (gpio2High() and gpio2Low()).

    Now, what's left is to add buttons to the UI that run these functions.

    I'm sorry for the lack of tutorials on our docs page. This should change in the near future

  • A big thank you. Now things are getting clearer. I'm already busy progging and it works. Is quite difficult .. but feasible. I once created a flowchart to illustrate the way. Incidentally, I try to put a recording function via HTML5 (Stream Recording) on ​​the surface - but it does not quite want it. But it starts to be great fun. Unfortunately my laptop does not like Ubuntu properly (Nvidia driver) otherwise I would try out Rvitz and CO. But come time, come advice.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!