How to combine 2 script files with eachother

asked 2021-12-15 05:44:15 -0500

jeroen1604 gravatar image

updated 2021-12-15 06:59:17 -0500

osilva gravatar image

Hello I have a question about 2 script files.

I need to run my waypoint script file with an other obstacle file. So if the obstacle distance is greater dan 1 i need to have my other file instead of driving forward in x = 0.5. How can I do this i am new to programming in Python and couldnt come up with al solution.

Obstacle file:

! /usr/bin/env python

import rospy
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist

def callback(msg):

#If the distance to an obstacle in front of the robot is bigger than 1 meter, the robot will move forward
  if msg.ranges[0] > 1:
      move.linear.x = 0.5
      move.angular.z = 0.0

#If the distance to an obstacle in front of the robot is smaller than 1 meter, the robot will stop
  print "Number of ranges: ", len(msg.ranges)
  print "Reading at position 0:", msg.ranges[0]
  if msg.ranges[0] < 1:
      move.linear.x = 0.0
      move.angular.z = 0.0

  pub.publish(move)

rospy.init_node('rotw5_node')
sub = rospy.Subscriber('/scan', LaserScan, callback) #We subscribe to the laser's topic
pub = rospy.Publisher('/cmd_vel', Twist)
move = Twist()

rospy.spin()

Waypoint script:

!/usr/bin/env python

import rospy
import tf
import numpy as np
import matplotlib.pyplot as plt
from geometry_msgs.msg import Twist
from nav_msgs.msg import Odometry
from math import pi, sqrt, atan2

WAYPOINTS = [[0.5,0],[1,0],[1,0],[1,0.5],[1,1],[1,1],[0.5,1],[0,1],[0,1],[0,0.5],[0,0]]

class PID:
    """
    Discrete PID control
    """
    def __init__(self, P=0.0, I=0.0, D=0.0, Derivator=0, Integrator=0, Integrator_max=10, Integrator_min=-10):
        self.Kp = P
        self.Ki = I
        self.Kd = D
        self.Derivator = Derivator
        self.Integrator = Integrator
        self.Integrator_max = Integrator_max
        self.Integrator_min = Integrator_min
        self.set_point = 0.0
        self.error = 0.0

    def update(self, current_value):
        self.error = self.set_point - current_value
        if self.error > pi:  # specific design for circular situation
            self.error = self.error - 2*pi
        elif self.error < -pi:
            self.error = self.error + 2*pi
        self.P_value = self.Kp * self.error
        self.D_value = self.Kd * ( self.error - self.Derivator)
        self.Derivator = self.error
        self.Integrator = self.Integrator + self.error
        if self.Integrator > self.Integrator_max:
            self.Integrator = self.Integrator_max
        elif self.Integrator < self.Integrator_min:
            self.Integrator = self.Integrator_min
        self.I_value = self.Integrator * self.Ki
        PID = self.P_value + self.I_value + self.D_value
        return PID

    def setPoint(self, set_point):
        self.set_point = set_point
        self.Derivator = 0
        self.Integrator = 0

    def setPID(self, set_P=0.0, set_I=0.0, set_D=0.0):
        self.Kp = set_P
        self.Ki = set_I
        self.Kd = set_D

class turtlebot_move():
    def __init__(self):
        rospy.init_node('turtlebot_move', anonymous=False)
        rospy.loginfo("Press CTRL + C to terminate")
        rospy.on_shutdown(self.stop)

        self.x = 0.0
        self.y = 0.0
        self.theta = 0.0
        self.pid_theta = PID(0,0,0)  # initialization

        self.odom_sub = rospy.Subscriber("odom", Odometry, self.odom_callback)
        self.vel_pub = rospy.Publisher('cmd_vel ...
(more)
edit retag flag offensive close merge delete

Comments

In general, you should think of it as a flow of new information from the LaserScan through a sensor value condition that modifies the velocities given to the robot. If the conditions are met (<1m), gradually change the speeds to zero in each control iteration, otherwise continue PID control between waypoints.

This is the first "knee-jerk reaction" thought, so take it with a pinch of salt.

ljaniec gravatar image ljaniec  ( 2021-12-15 12:34:49 -0500 )edit

Could u maybe write the comment as a script? I am a mechanical engineer so never programmed before. Is it with a if then else function in the waypoints and then import the laserscan>

jeroen1604 gravatar image jeroen1604  ( 2021-12-16 08:23:10 -0500 )edit

So that you don't wait too long, this could be your base. The entire site next to official ROS2 documentation website is great but I think basic programming is a must there :(

ljaniec gravatar image ljaniec  ( 2021-12-17 07:58:04 -0500 )edit