ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How to run two functions simultaneously

asked 2021-03-05 01:35:39 -0500

Jefferson gravatar image

Hi,

I would like to know on how to execute two functions as I couldnt managed to call both of them. For example, I would like to call redlighton() and buzzer_on() functions at the same time

    void red_light_on(void)
    {
        unsigned char buff_read[6];                    
        unsigned short motor_speed_address = 0x0001;    
        buff_read[0] = 0x04;                            
        buff_read[1] = 0x05;                            
        buff_read[2] = 0x00;                            
        buff_read[3] = 0x02;                            
        buff_read[4] = 0xFF;                            
        buff_read[5] = 0x00;                            
        write_modbus(&buff_read[0], 6);
        frame_sent = 1;
        frame_checked = 0;
        rev_frame_size=8;
        current_ctime=ros::Time::now();

        if (modbus_cmd_send_delay_t_!=0)
            sleep(modbus_cmd_send_delay_t_);    
    }

    void red_light_off(void)
    {
        unsigned char buff_read[6];                    
        unsigned short motor_speed_address = 0x0001;    
        buff_read[0] = 0x04;                            
        buff_read[1] = 0x05;                            
        buff_read[2] = 0x00;                           
        buff_read[3] = 0x02;                            
        buff_read[4] = 0x00;                            
        buff_read[5] = 0x00;                            
        write_modbus(&buff_read[0], 6);
        frame_sent = 1;
        frame_checked = 0;
        rev_frame_size=8;
        current_ctime=ros::Time::now();

        if (modbus_cmd_send_delay_t_!=0)
            sleep(modbus_cmd_send_delay_t_);    
    }

    void buzzer_on(void)
    {
        unsigned char buff_read[6];
        //unsigned short battery_address = 0x0014; // 0014
        buff_read[0] = 0x04; //Slave ID Address
        buff_read[1] = 0x05; //Function code (0x04: read registers)
        buff_read[2] = 0x00;
        buff_read[3] = 0x03;
        buff_read[4] = 0xFF;
        buff_read[5] = 0x00;
        write_modbus(&buff_read[0], 6);
        frame_sent = 1;
        frame_checked = 0;
        rev_frame_size=7;
        current_ctime=ros::Time::now();
        if (modbus_cmd_send_delay_t_!=0)
            sleep(modbus_cmd_send_delay_t_);    
    }


    void buzzer_off(void)
    {
        unsigned char buff_read[6];
        //unsigned short battery_address = 0x0014; // 0014
        buff_read[0] = 0x04; //Slave ID Address
        buff_read[1] = 0x05; //Function code (0x04: read registers)
        buff_read[2] = 0x00;
        buff_read[3] = 0x03;
        buff_read[4] = 0x00;
        buff_read[5] = 0x00;
        write_modbus(&buff_read[0], 6);
        frame_sent = 1;
        frame_checked = 0;
        rev_frame_size=7;
        current_ctime=ros::Time::now();
        if (modbus_cmd_send_delay_t_!=0)
            sleep(modbus_cmd_send_delay_t_);    
    }

while (ros::ok())
    {

        if (get_new_cmd == 1)
        {

            //estop_read();
            ros::Duration(0.03).sleep();
            get_new_cmd = 2;    
            //printf("Estop: %d \n",led_points); 
        }


       else if (get_new_cmd == 2)
        {
            modbus_response();

/*
        if (led_points == 1)
             {
               buzzer_on();
               red_light_on();
           printf("Estop Transmitted successfully! \n");
             }

       else 
            {
            buzzer_off();
            red_light_off();
           printf("Estop not successful! \n");
            }
*/
    if (agv_battery > battery)

        {
            buzzer_on();
            //red_light_on();
           printf("AGV_Battery: %.2f \n", agv_battery);
           printf("Battery: %.2f \n", battery);
           printf("Transmitted successfully!\n");
        }

        else 
        {
            buzzer_off();
            red_light_off();
           printf("Please try again!\n");
        }   

            get_new_cmd = 1;
        }

Thanks!

edit retag flag offensive close merge delete

Comments

on what type of device is this code executed? Because if it is a microcontroller or such, I think it's impossible to call two functions simultaneously. If it is a computer with a proper operative system (like linux), then you could use multi-threading or multi-processing to call these two functions at the same time.

Another way may be to use two ROS nodes: one for the Buzzer and one for the Light and send messages or call services to control them. This may have the same effect of multithreading but it is multiprocessing.

davide.cremona gravatar image davide.cremona  ( 2021-03-05 02:58:35 -0500 )edit

@davide.cremona is right, but for something like turning on a buzzer and light, "at the same time" can mean sequentially but very fast. If you only need to fool human perception into think they happen simultaneously (and the functions themselves are fast enough), calling one function immediately after the other should be close enough. I suspect you have a modbus issue rather than a threading issue. What undesirable behavior do you observe when you try to call your functions sequentially? Does the program crash; does the first function succeed but not the second; or something else?

For troubleshooting, it may be worthwhile to simplify your main loop to only toggle both the buzzer and light on/off then sleep 2 seconds.

tryan gravatar image tryan  ( 2021-03-05 09:43:50 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-03-05 02:52:14 -0500

dei gravatar image

you can use threaded programming

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2021-03-05 01:35:39 -0500

Seen: 293 times

Last updated: Mar 05 '21