Dragon50

hub module run_to_position method

Recommended Posts

Thank you fore reading this, my first posting and I have searched the forum for an answer.

The MotorPair class doesn't have a a run_to-position method and so I've been experimenting with the MicroPython hub module run_to_position method but the positioning of the motors is quite erratic so I must not be using the class correctly.

This is what I've tried:

motor_left = port.B.motor
motor_right = port.F.motor

motor_pair = motor_left.pair(motor_right)

motor_pair.run_to_position(0, 0)

A kind person from the FaceBook group suggested that I try adding the following in the setup section before the code runs to set the absolute position:

motor_left.mode(0)
motor_right.mode(0)

However, this doesn't seem to work either.

What I'd like is the equivalent to "run_to_position(0, direction='shortest path')" but for a pair of motors.

By the way, I've not been able to find any example code for the hub module, just the documentation.

 

Share this post


Link to post
Share on other sites

Hi,

what if you use two commands, one for each motor ?

11 hours ago, Dragon50 said:

By the way, I've not been able to find any example code for the hub module, just the documentation.

 

The documentation contains many examples

https://docs.pybricks.com/en/stable/

 

Share this post


Link to post
Share on other sites

Thank you Lok24 for your reply. The problem is that the run_to_position method is blocking which means that the next instruction won't start until the motor has finished moving. I'd like both motors to move simultaneously. I note that Pybricks gets around this problem with a don't wait option.

The last time I checked Pybricks is not an option for Linux users, so Pybrick's documentation doesn't really help me at all at the moment.

Edited by Dragon50
Corrected a typo

Share this post


Link to post
Share on other sites

I'm not entirely sure what software we are talking about, but anyway:

You can try (if the port is not port A change the letter of course...) hub.Port.A.motor.run_to_position() 

Source:

https://antonsmindstorms.com/2021/01/14/advanced-undocumented-python-in-spike-prime-and-mindstorms-hubs/

Pybricks does work on Linux, take a look here:

http://pybricks.com/install/technic-boost-city/#installing-a-compatible-browser

Share this post


Link to post
Share on other sites

Thank you Gimmick for your reply.  I have, as you can see in my original post, that I have already tried your suggestion. I'm trying to have two motors (a pair of motors) move to position 0 simultaneously. The software that I'm using is the MicroPython hub API. https://lego.github.io/MINDSTORMS-Robot-Inventor-hub-API/class_motorpair.html?fbclid=IwAR2dxMRRsGTPTg_wlBTHbN_1nFPRN6NgGQ-XbIj87XzjpeYF0_q5pFDZ2To

Pybricks wasn't working using chromium if it was installed as a snap package. That may have been fixed by now. There were plans to have dual boot, I wonder if that has been implemented yet? I'm a bit hesitant to wipe out the original Lego firmware.

Share this post


Link to post
Share on other sites
2 hours ago, Dragon50 said:

Thank you Gimmick for your reply.  I have, as you can see in my original post, that I have already tried your suggestion. I'm trying to have two motors (a pair of motors) move to position 0 simultaneously. The software that I'm using is the MicroPython hub API. https://lego.github.io/MINDSTORMS-Robot-Inventor-hub-API/class_motorpair.html?fbclid=IwAR2dxMRRsGTPTg_wlBTHbN_1nFPRN6NgGQ-XbIj87XzjpeYF0_q5pFDZ2To

Pybricks wasn't working using chromium if it was installed as a snap package. That may have been fixed by now. There were plans to have dual boot, I wonder if that has been implemented yet? I'm a bit hesitant to wipe out the original Lego firmware.

Sry, looks like I wasn't fully awake x(

Did you enable the Experimental Web Platform Feature in Chrome? Edit: Which linux distri are you using exactly?

While there are threading and async libs for micropython, I'm not sure if they work with the hub.

But it seems like @David Lechner ( pybricks) wrote a workaround example using yield

https://gist.github.com/dlech/fa48f9b2a3a661c79c2c5880684b63ae

 

Edited by Gimmick

Share this post


Link to post
Share on other sites

I met the same limitation with a First Lego League Spike Prime robot I am working on.

My solution was to program my own program to control the robot. Instead of synchronising the motors based on angles I choose to use the built in gyro instead. I also made code to ramp up the speed of the motors in the beginning and slow them down again near the target angle.

The code is currently embedded into a large library, that is very much a work-in-process. I will try to isolate the motor control method for you and will upload it here on EB - this however might take some days.
A small hint of caution beforehand is, that my program probability still needs some tweaking if it is to be matched against  the awesome solutions of pybricks.

Edited by Munchkin255
Using dictionary after posting - bad idea

Share this post


Link to post
Share on other sites

Thank you for your replies.

I have a very slow Internet connection at my present location which also keeps dropping out so I'm not sure what this reply is going to look like.

Gimmick. No need to be sorry, any reply is better than no reply. I tried to use Chromium (not Google's Chrome) and I did enable the experimental mode and install the udev rules file for Mindstorms. I was in contact with one of Pybrick's developers via Github some time ago and he confirmed that chromium won't work if it's been installed as a snap package and that a fix was in the pipeline. I'm using Kubuntu 21.04. I'm reluctant to experiment with Pybricks if there's a chance that I'll lose the Lego firmware. It took a long time to download the latest Android Lego update.

Munchkin255. Regarding the simulations movement of a pair of motors. It's not a big deal to have to move the motors one at a time, I was just trying to convert the block code for the Charlie skier project. What I have skies correctly, it just doesn't bring the arms down to the zero start position at the same time before the skiing loop begins. I'm always interested to see how others have solved a problem. Are you using Pybricks? I'm new to Lego and I haven't played with the hub gyro yet.

Share this post


Link to post
Share on other sites
3 hours ago, Dragon50 said:

Thank you for your replies.

I have a very slow Internet connection at my present location which also keeps dropping out so I'm not sure what this reply is going to look like.

Gimmick. No need to be sorry, any reply is better than no reply. I tried to use Chromium (not Google's Chrome) and I did enable the experimental mode and install the udev rules file for Mindstorms. I was in contact with one of Pybrick's developers via Github some time ago and he confirmed that chromium won't work if it's been installed as a snap package and that a fix was in the pipeline. I'm using Kubuntu 21.04. I'm reluctant to experiment with Pybricks if there's a chance that I'll lose the Lego firmware. It took a long time to download the latest Android Lego update.

Just to make it clear: The example above is for the original firmware.

I use Chrome on Lubuntu, but not as snap package.

Share this post


Link to post
Share on other sites

Hi Gimmick

I am using Spike Prime with the LEGO Education Spike software.

I guess my code will not really help you.
The program I have is for the driving wheels of a robot. For this I am actually not using the absolute angle measurement from the servos. Instead I use the degrees turned measurements. Furthermore my motors aren't really synchronized instead I use the gyro input to keep the robot moving in a straight line.

An option - if you want the arms to move with the skies could be to do something like:

motorpair.start()

While not a desired angle:
    Do nothing or maybe use the yield command to do other stuff

When at desired angle:
motorpair.stop()

Share this post


Link to post
Share on other sites
On 9/4/2021 at 4:56 PM, Munchkin255 said:

An option - if you want the arms to move with the skies could be to do something like:

motorpair.start()

While not a desired angle:
    Do nothing or maybe use the yield command to do other stuff

When at desired angle:
motorpair.stop() 

Thank you Munchkin255.

This won't solve my current problem because I need to reach the angle via the shortest path and there's no way of knowing beforehand if the motor has to rotate clockwise or anticlockwise. I started another thread yesterday describing the problem.

I've a mental note of your suggestion and it may well be useful for a future project.

 

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.