moriarty / ros-ev3

How to install ROS (ros_comm) on an ev3 with ev3dev
GNU General Public License v2.0
24 stars 19 forks source link

rospy can't run node with ev3dev lib #19

Open FRozanski opened 5 years ago

FRozanski commented 5 years ago

I have RemoteControlsSubscriber.py node:

#!/usr/bin/env python
from __future__ import print_function
import roslib
roslib.load_manifest('learn_network')
import sys
import rospy
from learn_network.msg import RemoteControls
from SteeringController import SteeringController

class RemoteControlsSubscriber:

    def __init__(self, topic_name, steeringController):
        self.subscriber = rospy.Subscriber(topic_name, ImageSteering ,self.get_controls)
        self.steeringController = steeringController

    def run_subscribe(self, subscriber_name):
        rospy.init_node(subscriber_name, anonymous=True)    
        rospy.spin()        

    def get_controls(self, remote_controls):
        self.steeringController.run_to_abs_pos_range(remote_controls.rigth_stick_horizontal_axis)
        sleep(0.2)

if __name__ == '__main__':
    try:
        steeringController = SteeringController('C', 125, 900)
        remoteControlsSubscriber = RemoteControlsSubscriber('remote_controls_topic')
        remoteControlsSubscriber.run_subscribe('remote_controls_subscriber_node')
    except rospy.ROSInterruptException:
        pass      

And SteeringController.py file

#!/usr/bin/env python3
from ev3dev.brickpi import *

class SteeringController:

    def __init__(self, motor_port, max_angle, speed_sp):
        self.motor = LargeMotor(motor_port)
        self.zero_position = self.motor.position
        self.max_angle = max_angle
        self.speed_sp = speed_sp
        self.stop_action = "hold"

    def __del__ (self):
        self.motor.stop()        
        self.motor.reset()
        self.print_motor_info()

    def print_motor_info(self):   
        print("position: " + str(self.motor.position)) 
        print("is_holding: " + str(self.motor.is_holding))
        print("is_overloaded: " + str(self.motor.is_overloaded))
        print("is_ramping: " + str(self.motor.is_ramping))
        print("is_running: " + str(self.motor.is_running))
        print("is_stalled: " + str(self.motor.is_stalled))

    def run_to_rel_pos(self, position_sp):
        if self.motor.position + position_sp > self.zero_position + self.max_angle:
            raise Exception("Too big steering angle") 
        if self.motor.position + position_sp < self.zero_position - self.max_angle:
            raise Exception("Too small steering angle")             
        self.motor.run_to_rel_pos(position_sp=position_sp, speed_sp=self.speed_sp, stop_action=self.stop_action)

    def run_to_abs_pos(self, position_sp):
        if position_sp > self.max_angle:
            raise Exception("Too big steering angle")
        if position_sp < -self.max_angle:
            raise Exception("Too small steering angle")    
        self.motor.run_to_abs_pos(position_sp=position_sp, speed_sp=self.speed_sp, stop_action=self.stop_action)

    def run_to_abs_pos_range(self, range):
        self.run_to_abs_pos(self.max_angle*range)

    def run_to_zero_pos(self):
        self.motor.run_to_abs_pos(position_sp=self.zero_position, speed_sp=self.speed_sp, stop_action=self.stop_action)

RemoteControlsSubscriber.py node import SteeringController.py file. When I run node: rosrun learn_network RemoteControlsSubscriber.py I get:

terminate called after throwing an instance of 'rospack::Exception'
  what():  error parsing manifest of package class_loader at /opt/ros/melodic/share/class_loader/package.xml
find: '': No such file or directory
Traceback (most recent call last):
  File "/home/robot/Tesla/CatkinWorkspaces/src/learn_network/scripts/RemoteControlsSubscriber.py", line 8, in <module>
    from SteeringController import SteeringController
  File "/home/robot/Tesla/CatkinWorkspaces/src/learn_network/scripts/SteeringController.py", line 4, in <module>
    from ev3dev.brickpi import *
ImportError: No module named brickpi

I have another file (SteeringScript.py) whitch also import SteeringController.py. When I run python3 SteeringScript.py everything work fine. When I run python SteeringScript.py i get same error:

Traceback (most recent call last):
  File "SteeringScript.py", line 1, in <module>
    from SteeringController import SteeringController
  File "/home/robot/Tesla/Beta/SteeringController.py", line 4, in <module>
    from ev3dev.brickpi import *
ImportError: No module named brickpi

So rospy run nodes trough python2, is any way of runing nodes trough python3? It is very crucial for me, because a can't use ev3dev library through ROS : /

FRozanski commented 5 years ago

Ok I managed this. Here is explenation https://github.com/OTL/cozmo_driver#super-hack-to-run-rospy-from-python3

AnushRobotics commented 4 years ago

anu@anu:~$ ssh robot@ev3dev.local Password: Linux ev3dev 4.14.117-ev3dev-2.3.4-ev3 #1 PREEMPT Thu May 9 15:13:01 CDT 2019 armv5tejl


| / | | / \ \ / / | \ / ` |/ \ \ / / | /\ V / ) | (| | /\ V / _| _/ |__/ _,|__| \/

Debian stretch on LEGO MINDSTORMS EV3! Last login: Wed Feb 19 20:02:45 2020 from 172.16.3.80 robot@ev3dev:~$ cd brickstrap robot@ev3dev:~/brickstrap$ brickstrap -p ev3dev-jessie -c ev3 create-rootfs -bash: brickstrap: command not found robot@ev3dev:~/brickstrap$

we could not get installed with the brickstrap and even docker...please help me @moriarty