Gazebo connection help with code

asked 2021-03-10 14:48:37 -0500

PGTKing gravatar image

updated 2021-03-10 15:02:14 -0500

tryan gravatar image

Hey community,

I have a gazebo connection file I am using, link. But when I try to use the gazebo pause simulation function provided from the file, the simulation resets itself, I just want it to pause.

Here is my code:

#!/usr/bin/env python3

import rospy
from std_msgs.msg import Float64
import math
import random
from sensor_msgs.msg import JointState
import numpy as np
import time
import threading
from cartpoleinitial import GazeboConnection

start_time = time.time()


class RobotControl():
    def __init__(self):
        rospy.init_node('robot_control_node', anonymous=True)
        self.talker = rospy.Publisher('/rrbot/joint1_position_controller/command', Float64, queue_size=10)
        rospy.Subscriber('/rrbot/joint_states', JointState, self.joint_callback)
        self.force = None  # Generally, it's good to initialize member variables.  What if callback() is called before give_force()?
        self.gc = GazeboConnection()

        rate = rospy.Rate(10)
        while not rospy.is_shutdown():
            self.give_force()
            rate.sleep()


    def give_force(self):
        self.force = random.choice([-2,2])  # Changes force every time, right before publishing
        self.talker.publish(self.force)

    def joint_callback(self, data):
        self.joint = data.position[0]
        reward = None
        if self.observation_check() is True:
            reward = (time.time() - start_time)
            gc.pauseSim()
        else:
            pass

        print(reward)

    def observation_check(self):
        max_height = -3.3
        if self.joint < max_height:
            done = True
        else:
            done = False
        return done


if __name__ == "__main__":
    # Instantiate the class
    my_robot = RobotControl()

Apologies for the formatting, is there a guide for how to format questions on this site? I try to post my code using the symbol but it always turns out garbage.

edit retag flag offensive close merge delete

Comments

I don't know if there's a better way, but you can always correct code formatting with leading spaces.

tryan gravatar image tryan  ( 2021-03-10 15:33:18 -0500 )edit