Jim

[EV3DEV] Venturing into the World of EV3Dev and Python

Recommended Posts

Also for one of the motors? Not both. I will check it out, thanks.

I can’t wait for my metal axles and other stuff (big battery, screens) to arrive so I can rebuild my robot. Working on version 6.2 and I have a good feeling about this one :thumbup:

Share this post


Link to post
Share on other sites
6 hours ago, Jim said:

Also for one of the motors? Not both. I will check it out, thanks.

You can pass set_polarity() a list of motor objects to tweak

Share this post


Link to post
Share on other sites
6 hours ago, dwalton76 said:

You can pass set_polarity() a list of motor objects to tweak

Can you maybe send me an example, because I am trying to figure out how to call the method. I can't seem to get the syntax right :hmpf_bad:

Share this post


Link to post
Share on other sites
On 12/18/2018 at 1:42 PM, Jim said:

Can you maybe send me an example, because I am trying to figure out how to call the method. I can't seem to get the syntax right :hmpf_bad:

 

from ev3dev2.motor import MoveTank, OUTPUT_A, OUTPUT_B, LargeMotor, SpeedRPM

robot = MoveTank(OUTPUT_A, OUTPUT_B)
robot.set_polarity(LargeMotor.POLARITY_INVERSED, [robot.left_motor,])

This should do the trick. The second argument to set_polarity() is a list of motors that you want to change the polarity for.

Share this post


Link to post
Share on other sites

I have been trying to get the polarity to work, but I am still drawing a blank. Since I am working with a stacked BrickPi, my syntax is a bit different. I can't use the "spi0.1MC" string in the polarity function. 

wheel_a = MoveTank("spi0.1:MC", "spi0.1:MH")
wheel_b = MoveTank("spi0.1:ME", "spi0.1:MB")
wheel_c = MoveTank("spi0.1:MF", "spi0.1:MG")
wheel_d = MoveTank("spi0.1:MA", "spi0.1:MD")

wheel_a.set_polarity(LargeMotor.POLARITY_INVERSED, ["spi0.1:MC",])

 

After tinkering a bit, this seems to work:

motor_a = LargeMotor('spi0.1:MA')
motor_b = LargeMotor('spi0.1:MB')
motor_c = LargeMotor('spi0.1:MC')
motor_d = LargeMotor('spi0.1:MD')
motor_e = LargeMotor('spi0.1:ME')
motor_f = LargeMotor('spi0.1:MF')
motor_g = LargeMotor('spi0.1:MG')
motor_h = LargeMotor('spi0.1:MH')

wheel_a = MoveTank("spi0.1:MC", "spi0.1:MH")
wheel_b = MoveTank("spi0.1:ME", "spi0.1:MB")
wheel_c = MoveTank("spi0.1:MF", "spi0.1:MG")
wheel_d = MoveTank("spi0.1:MA", "spi0.1:MD")

wheel_a.set_polarity(LargeMotor.POLARITY_INVERSED, [motor_c,])
wheel_b.set_polarity(LargeMotor.POLARITY_INVERSED, [motor_e,])
wheel_c.set_polarity(LargeMotor.POLARITY_INVERSED, [motor_f,])
wheel_d.set_polarity(LargeMotor.POLARITY_INVERSED, [motor_a,])

Share this post


Link to post
Share on other sites

When I am running a test program, I sometimes have issues with the wait function. Instead of waiting for one second, it seems to be waiting forever, making the robot go berserk. I decided to implement a touch sensor the abandon the program when things go south. But my robot doesn't recognize my sensor. I have tried every port, multiple touch sensors, but it says that the sensor is not connected. This is my code:

from ev3dev2.motor import LargeMotor, Motor, SpeedPercent, MoveTank 
from ev3dev2.sensor import INPUT_1, INPUT_2
from ev3dev2.sensor.lego import TouchSensor

touch_1 = TouchSensor("spi0.1:S2")

When I used INPUT_2, instead of "spi0.1:S2", I got an error message stating that "spi0.1.S2" was not connected, so I reckoned I could use the stacked BrickPi notation.

Maybe I need to wait a bit before posting it here, but it might help others with the same issues. I have found the solution. You need to define which port it is, before creating the touch sensor, so this works:

port_2 = ev3.LegoPort("spi0.1:S2")
port_2.mode ="ev3-analog"
port_2.set_device = "lego-ev3-touch"

touch_1 = TouchSensor("spi0.1:S2")

 

Share this post


Link to post
Share on other sites

The next issue I am facing is that sometimes my program has a rather erratic flow and hangs somewhere. This results in the motors turning forever. But before I bother you with this one, I will put some time in debugging the issue. Version 6.2 of my robot is done and I can start fiddling around with Python again.

Share this post


Link to post
Share on other sites
On 12/29/2018 at 8:41 AM, Jim said:

I have been trying to get the polarity to work, but I am still drawing a blank. Since I am working with a stacked BrickPi, my syntax is a bit different. I can't use the "spi0.1MC" string in the polarity function. 


wheel_a = MoveTank("spi0.1:MC", "spi0.1:MH")
wheel_b = MoveTank("spi0.1:ME", "spi0.1:MB")
wheel_c = MoveTank("spi0.1:MF", "spi0.1:MG")
wheel_d = MoveTank("spi0.1:MA", "spi0.1:MD")

wheel_a.set_polarity(LargeMotor.POLARITY_INVERSED, ["spi0.1:MC",])

 

FYI all of the spi strings are defined:

ddwalton@ddwalton-mbp[_platform]# grep spi *.py
brickpi3.py:OUTPUT_A = 'spi0.1:MA'
brickpi3.py:OUTPUT_B = 'spi0.1:MB'
brickpi3.py:OUTPUT_C = 'spi0.1:MC'
brickpi3.py:OUTPUT_D = 'spi0.1:MD'
brickpi3.py:OUTPUT_E = 'spi0.1:ME'
brickpi3.py:OUTPUT_F = 'spi0.1:MF'

You can import them via

from ev3dev2.motor import OUTPUT_A, OUTPUT_B, OUTPUT_C

Same goes for the INPUT strings

On 12/29/2018 at 8:41 AM, Jim said:

I have been trying to get the polarity to work, but I am still drawing a blank. Since I am working with a stacked BrickPi, my syntax is a bit different. I can't use the "spi0.1MC" string in the polarity function. 


wheel_a = MoveTank("spi0.1:MC", "spi0.1:MH")
wheel_b = MoveTank("spi0.1:ME", "spi0.1:MB")
wheel_c = MoveTank("spi0.1:MF", "spi0.1:MG")
wheel_d = MoveTank("spi0.1:MA", "spi0.1:MD")

wheel_a.set_polarity(LargeMotor.POLARITY_INVERSED, ["spi0.1:MC",])

 

After tinkering a bit, this seems to work:


motor_a = LargeMotor('spi0.1:MA')
motor_b = LargeMotor('spi0.1:MB')
motor_c = LargeMotor('spi0.1:MC')
motor_d = LargeMotor('spi0.1:MD')
motor_e = LargeMotor('spi0.1:ME')
motor_f = LargeMotor('spi0.1:MF')
motor_g = LargeMotor('spi0.1:MG')
motor_h = LargeMotor('spi0.1:MH')

wheel_a = MoveTank("spi0.1:MC", "spi0.1:MH")
wheel_b = MoveTank("spi0.1:ME", "spi0.1:MB")
wheel_c = MoveTank("spi0.1:MF", "spi0.1:MG")
wheel_d = MoveTank("spi0.1:MA", "spi0.1:MD")

wheel_a.set_polarity(LargeMotor.POLARITY_INVERSED, [motor_c,])
wheel_b.set_polarity(LargeMotor.POLARITY_INVERSED, [motor_e,])
wheel_c.set_polarity(LargeMotor.POLARITY_INVERSED, [motor_f,])
wheel_d.set_polarity(LargeMotor.POLARITY_INVERSED, [motor_a,])

So the motor_a, etc objects that you are creating are not going to be used by MoveTank, MoveTank is going to create its own LargeMotor objects.  So what you have above will work but this is going down a path that could be very confusing to troubleshoot later :(  Something like the following would be much cleaner

 

wheel_a = MoveTank(OUTPUT_A, OUTPUT_B)
wheel_c = MoveTank(OUTPUT_C, OUTPUT_D)

wheel_a.set_polarity(LargeMotor.POLARITY_INVERSED, [wheel_a.left_motor,])
wheel_c.set_polarity(LargeMotor.POLARITY_INVERSED, [wheel_c.left_motor,])

 

On 12/29/2018 at 9:41 AM, Jim said:

When I am running a test program, I sometimes have issues with the wait function. Instead of waiting for one second, it seems to be waiting forever, making the robot go berserk. I decided to implement a touch sensor the abandon the program when things go south. But my robot doesn't recognize my sensor. I have tried every port, multiple touch sensors, but it says that the sensor is not connected. This is my code:


from ev3dev2.motor import LargeMotor, Motor, SpeedPercent, MoveTank 
from ev3dev2.sensor import INPUT_1, INPUT_2
from ev3dev2.sensor.lego import TouchSensor

touch_1 = TouchSensor("spi0.1:S2")

When I used INPUT_2, instead of "spi0.1:S2", I got an error message stating that "spi0.1.S2" was not connected, so I reckoned I could use the stacked BrickPi notation.

Maybe I need to wait a bit before posting it here, but it might help others with the same issues. I have found the solution. You need to define which port it is, before creating the touch sensor, so this works:


port_2 = ev3.LegoPort("spi0.1:S2")
port_2.mode ="ev3-analog"
port_2.set_device = "lego-ev3-touch"

touch_1 = TouchSensor("spi0.1:S2")

 

brickpi cannot auto detect the sensors so you have to use set_device() to specify what is connected on what ports.  I don't have a code example handy but if you search in the ev3dev-lang-python repo for set_device() you should be able to find an example (there may be an example in the docs also...not sure).

Can you post the code you are using where the motors go beserk?

Share this post


Link to post
Share on other sites
7 minutes ago, dwalton76 said:

brickpi cannot auto detect the sensors so you have to use set_device() to specify what is connected on what ports.  I don't have a code example handy but if you search in the ev3dev-lang-python repo for set_device() you should be able to find an example (there may be an example in the docs also...not sure).

Can you post the code you are using where the motors go beserk?

I solved the sensor issue (see my comments).

I will do some more testing with motors and program flow and when I face errors, I will post them here. I haven't done enough testing to properly judge whether I have made mistakes myself.

11 minutes ago, dwalton76 said:

 


wheel_a = MoveTank(OUTPUT_A, OUTPUT_B)
wheel_c = MoveTank(OUTPUT_C, OUTPUT_D)

wheel_a.set_polarity(LargeMotor.POLARITY_INVERSED, [wheel_a.left_motor,])
wheel_c.set_polarity(LargeMotor.POLARITY_INVERSED, [wheel_c.left_motor,])

 

Thanks!  That looks great. I had some troubles with the "left_motor" not being recognizes. But it does seem to compile this way.

I will invest some time to write a proper program and I will post it here, so you can take a look.

Share this post


Link to post
Share on other sites
1 hour ago, pasquentmax said:

Hello, what are the advantages of programming in python? does this give other possibilities to the ev3 lego?

You mean the default graphical EV3 software (EV3-G)?

Python is a full blown programming languages with all the bells and whistles. If you are comfortable with writing computer software, using Python will give you better options to structure your project and implement functionality. When projects grow bigger, maintaining a EV3-G program can be cumbersome.

Share this post


Link to post
Share on other sites
10 hours ago, pasquentmax said:

Hello, what are the advantages of programming in python? does this give other possibilities to the ev3 lego?

Like Jim said, python, java, C or any other high level languages may allow big structered projects but don't give "per se" other possibilities to EV3. In some cases you may even lack some EV3 possibilities (like daisy chaning, still being addressed by ev3dev project). But when used on a full operating system like ev3dev then lots of other possibilities arise.

For a start, you have the full network stack. So you can use standard network protocols with your EV3. And also other linux stacks, like BueZ (Bluetooth) and ALSA (Audio). So you can pair your EV3 with a bluetooth speaker or an USB audio card and get decent sound. Or use your EV3 as a MIDI device.

Then you have the device support from the kernel. That means some LEGO devices that Linux still supports (like the old LEGOCam and the original WeDo 1.0) can be used with your EV3. And some NXT devices that no longer work with the current EV3 firmware can still be used as well (like the RFID sensor or the IRLink). And lots of non-LEGO devices - like USB fingerprint readers.

Now mixing all this can be difficult. And that's where I think python shines, not because it's a better language but because there is an huge amount of libraries available so you don't need to re-invent the wheel. Like complex mathematics or image and audio processing.

Share this post


Link to post
Share on other sites

I was able to do all the steps as listed on the EV3Dev.org website. I think I did all the steps correctly. I was able to do the hello.py program to run on my EV3. But, when I try something more advanced like LED or motors I get an error message that says it is unable to import 'ev3dev.motor' [E0401] I tried following the notes to change how the editor does line feeds from CR to LF but I still get the error message. any help would be greatly appreciated.

Mike,

Share this post


Link to post
Share on other sites

Interestingly, although I get an error message stating that it cannot find the ev3dev.motor to import. When I run the program from the EV3 brick it works????

I redid all the steps to change the CR to LF behavior but still have the error message or messages depending on how many imports I use.

Share this post


Link to post
Share on other sites

@MichaelWareman I have merged your question with my Python topic. I am not sure why it isn't working for you. I need to check my code, to compare it with my program. I can do this later.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.