-2

I'm following a tutorial on understanding writing python publisher in ROS2. This is an example that I'm creating. The code does not seem to have any errors but in vscode, the self word is underlined in red and when I hover the mouse it shows that "self" is not defined. How can I resolve this issue in vscode?

I will add the code here

#!/usr/bin/env python3
import rclpy
from rclpy.node import Node
 
from example_interfaces.msg import String
 
class RobotNewsStationNode(Node):   #modify name
   def __init__(self):
       super().__init__("robot_news_station")  #modify name
 
       self.publisher_ = self.create_publisher(String, "robot_news", 10)
 
 
   def publish_news(self):
       msg = String()
       msg.data = "Hello"
       self.publisher_.publish(msg)
 
      
def main(args=None):
   rclpy.init(args=args)
   node = RobotNewsStationNode()   #modify name
   rclpy.spin(node)
   rclpy.shutdown()
  
if __name__ == "__main__":
   main()

This is the code error im having in vscode enter image description here

Dinoj
  • 17
  • 6
  • seems like an indentation error. try ctrl+shift+I to format your file. If this doies not work, delete everything between the comment `#modify name` and `self.publisher...` and press enter to make a corrected new line – Sandwichnick Oct 17 '22 at 12:10

1 Answers1

0

As the error mentions, you are most likely mixing spaces/tabs.

Try delete all indentation untill that line, then use "tab" to indent your code, and be consistent about it i.e dont mix tabs and space.

CutePoison
  • 4,679
  • 5
  • 28
  • 63
  • thank you for the quick reply on my issue. I also tried it but it still shows the same issue. I tested the code and it works correctly as well. – Dinoj Oct 17 '22 at 12:13
  • Thank you, Your answer helped me to solve my issue. I did not down vote for your answer. I think it happened automatically – Dinoj Oct 17 '22 at 12:19