ROS2 Basics in 5 Days (Python), Part 4 Services, quiz, self.get_logger()

Hello,

I have the display of the direction

self.get_logger().info('direction is '+ str(request.direction))

I’m trying to display the angular_velocity

I thought abut something like this:

self.get_logger().info('time is %d' %(request.time))

I have tried some other syntax (with int, with +request.time+ etc)

Well, it doesn’t work

Any ideas?

Thank you

Hello @TAURD ,

Try converting it into a string with str(request.time)

Since we are using Python 3. I am a big fan of the newest string formatting, called f-strings. You just add f before the ‘…’ and directly type the variables’ name between {} wherever in the string you want. For sure an example is much simpler to understand than my explanation.

>>> recommended_sleep_hours = 8
>>> f'The recommended sleeping hours for an average adult are {recommended_sleep_hours}. Rest is essential!'
'The recommended sleeping hours for an average adult are 8. Rest is essential!'

If you ever forget about f-string formatting, just go to your search bar and ask for fstring.help

1 Like

Hello

I’ve tried it with this code

self.get_logger().info('time is ' + str(request.time))

process has died

It killed the server (launch file) in shell #1

Sorry for that.

Do you have any other ideas?

Thank you

I’ve tried it with this code

self.get_logger().info(f'time is {request.time}')

process has died

It killed the server (launch file) in shell #1

Sorry for that.

Do you have any other ideas?

Thank you

Then it is most likely not a sting formatting issue, we have to dig deeper.

Does it simply stop, or does it report back something? If so, could you share what it prints?

Can you also share your code please?

Hello,

How can I share the files in this forum?

So for my robot spins left or right with the angular velocity given

e.g.

for

ros2 launch services_quiz services_quiz_server.launch.py

in shell #1 and for

ros2 run services_quiz services_quiz_client right 10.0 1

the robot turn right with 10.0 rad/sec.

I still need to input the third variable for time cause my function expect it but it does nothing.

I still need to crack the time issue.

Thank you

Hello

In a different QU of mine you replied with

I give partial quote here

( f"I've been called for {request.time}s...

see the " sign

in a previous answer of yours in this thread you wrote with ' sign

see picture below

What say you, what is the proper form?

I would try it myself but for some time noe the shell is not available

Thank you

In python both ’ and " quotation marks (QM) can be used to represent a string, it makes no difference. I personally prefer using the singe QM, but again, it makes no difference. In that partial quote you mention I used double QM because I wanted to use the single QM in the sentence. See it underscored below

“I ve been called for {re…”

You can find great resources online on python strings.

PS: Have you tried killing the shell pressing on the :x:? It will automatically restart a fresh new one.

Of course

and I gave it few more minutes but now it is not better

No IDE
No Notebook

TheConstruct, please advise if you see this msg.

Hello

This is the solution (originally from GasPatxo) here

self.get_logger().info(f"I will spin {request.direction} around Z axis for {request.time}[sec] at {request.angular_velocity}[rad/sec]")

Just copied the entire code line and changed the text.

1 Like