How to run two functions simultaneously
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!
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 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.