Ask Your Question

Help in flags for python

asked 2020-10-19 20:55:58 -0600

kamui123 gravatar image

Hi all, I am new to Python programming and I being tasked to create codes by my supervisor but I have no clue how to.

I am doing a project on robots for my internship and he asked me to implement flags for different kind of 'modes' on the robot. He told me to add the flags in front line 5 so that the 'modes' can be easily detected. Example of a mode being like 'Manual Driving'.

def callback(self, vel):
    x = format(vel.linear.x, '+.2f')
    z = format(vel.angular.z, '+.2f')
    pck = b':' + x + b',' + z
    rospy.loginfo("Data Transmitting %s", pck)
    # Check the number of bytes to fit the format
    # b':+X.XX,+X.XX' => 12bytes ;)

This is what I am given. Any help would be greatly appreciated! :)

edit retag flag offensive close merge delete


Hi @kamui123, what are you trying to do in this instruction: pck = b':' + x + b',' + z. That wil not be interpreted properly by any python interpreter. Does your flags depend on the robot velocity? Why are you using as a check methond the number of bytes? If you want some flags you can just read the callback msg use a global variable to store the "mode" and then use this global variable in your main.

Weasfas gravatar image Weasfas  ( 2020-10-21 04:29:04 -0600 )edit

@Weasfas if I am not wrong, that instruction is to package the command to format <magnitude><angular>

kamui123 gravatar image kamui123  ( 2020-10-23 01:04:14 -0600 )edit

@kamui123, that line must be converted in something like:

pck = bytes(':', 'utf-8') + bytes(x, 'utf-8') + bytes(',', 'utf-8') + bytes(z, 'utf-8')

That will give literally: b':+X.XX,+X.XX' and will not contain 12 bytes but 45. In fact the size will only depend on the integer part of the inputs, so my doubt is why you want to decode the inputs to produce in-flag codes like that when you can have an input flag (1,2,3 or "Auto', 'Manual', etc.) that generates the operating mode on your system.

Weasfas gravatar image Weasfas  ( 2020-10-23 04:28:01 -0600 )edit

@Weasfas I am very sorry, I am new to Python programming and these codes is not done by me. It was from previous batch of interns. That's why I resorted to forums to ask for help. How do you go about implementing flags? Could you give me an example?

kamui123 gravatar image kamui123  ( 2020-10-26 01:21:03 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2020-10-26 05:29:32 -0600

Weasfas gravatar image

I am going to post some code that I think can help you with your problem. If this does not align with what you want just let me know and I will update the answer. I wrote some comments in the code so you can understand what is doing. Here it is:

#!/usr/bin/env python
import rospy
from std_msgs.msg import Int32
from geometry_msgs.msg import Twist

operating_mode = 0
vel_x = 0.0
vel_z = 0.0

# Callback for the operation mode, is just an integer that can be check with an if statement.
def mode_callback(mode):
  global operating_mode
  operating_mode =

# Callback for the vel command sent to move the platform.
def cmd_vel_callback(twist_cmd):
  global vel_x, vel_z
  vel_x = twist_cmd.linear.x # Extract linear component from Twist.
  vel_z = twist_cmd.angular.z # Extract angular component from Twist.

# Main loop for ROS node.
def spin():
  rospy.init_node('mode_operator') # Init the ROS node.
  rate = rospy.Rate(10) # Rate at which the node is going to run.
  rospy.Subscriber("mode", Int32, mode_callback) # Subscribe to the mode flag topic.
  rospy.Subscriber("cmd_twist", Twist, cmd_vel_callback) # Subscribe to the cmd_vel topic to read commands.

  # While ros is ok just continue working...
  while not rospy.is_shutdown():
    # Here you can implement and if statement that checks the operating_mode flag, like:
    # if operating_mode == 0:
    #   mode is Manual
    # elif operating_mode == 1:
    #   mode is Auto
    # ...
    # You have also here access to your cmd variables: vel_x, vel_z.
    rospy.loginfo("Operating mode is: " + str(operating_mode) \
      + " and cmd_vel command is vel_x: " + str(vel_x) + " vel_z: " + str(vel_z))
    rate.sleep() # Let the loop sleep to accomplish the rate wanted.

if __name__ == '__main__':
  except rospy.ROSInterruptException:

Hope it can help you.


edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2020-10-19 20:55:58 -0600

Seen: 28 times

Last updated: Oct 26