Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplification #17

Open
laurensvalk opened this issue Apr 1, 2018 · 11 comments
Open

Simplification #17

laurensvalk opened this issue Apr 1, 2018 · 11 comments

Comments

@laurensvalk
Copy link
Owner

laurensvalk commented Apr 1, 2018

I'm currently working on a new version of the code which is a bit more pythonic. Feel free to give it a try if you're interested.

The whole control loop is now about 10 lines of code. The full code is here.

For now only the EV3 gyro is supported. I haven't scaled for battery voltage yet, so the tuning may not work for you right away. But with simpler code, it's hopefully easier to modify on your end.

# Main loop
while True:
    # Loop start
    start = time()

    # Body angle
    rate = highpass.filter(gyro.rate)
    angle = integrator.integral(rate)

    # Wheel translation
    average_motor_degrees = (right.position + left.position)/2
    distance = average_motor_degrees * cm_per_degree
    speed = differentiator.derivative(distance)

    # Control signal
    duty = gain_angle*angle + gain_rate*rate + \
        + gain_distance*distance + gain_speed*speed

    # Motor actuation
    left.duty(duty)
    right.duty(duty)

    # Sleep until loop complete
    while time()-start < looptime:
        sleep(0.002)

The code uses ev3devlight to interface with the hardware, which is a python3 and micropython compatible library. See this for installation instructions.

It doesn't have all the ev3dev functionality, but it takes only a few seconds to load. It's also faster for reading sensors and controlling motors.

@laurensvalk
Copy link
Owner Author

@pipothy
Copy link

pipothy commented Apr 1, 2018

Alas, I don't have access to the robot now. I can try it when I do, but that could be a long time. The code looks lovely!

@BertLindeman
Copy link

After the install the link fails:
ln -s /home/robot/.micropython/lib/ev3devlight /home/robot/.local/lib/python3.5/site-packages/ev3devlight
results in:
ln: failed to create symbolic link '/home/robot/.local/lib/python3.5/site-packages/ev3devlight': No such file or directory
Assumed I need to create the base directories:
mkdir -p /home/robot/.local/lib/python3.5/site-packages/

This helps.

Is there a way to use the HiTechnic Gyro in ev3devlight?

@laurensvalk
Copy link
Owner Author

Is there a way to use the HiTechnic Gyro in ev3devlight?

There is now support for analog sensors. Repeat the installation instructions to update.

To use the HiTechnic for now, mount the sensor like this, and replace:

# Configure and calibrate LEGO Gyro
gyro = Gyro('in2', read_rate=True, read_angle=False, calibrate=True)
initial_bias = 0

With:

# Configure and calibrate HiTechnic Gyro
class HiTechnicGyro(Analog):

    """Interface for HiTechnic Gyro."""
    def __init__(self, port):
        """Initialize as analog device with scaling factor."""
        Analog.__init__(self, port, scaling=-4.8)

    @property
    def rate(self):
        """Return gyro rate."""
        return self.output

gyro = HiTechnicGyro('in2')
calibrator = Calibrator()
for i in range(20):
    calibrator.add_sample(gyro.rate)
    sleep(0.02)
initial_bias = calibrator.average

In the future I might add the HiTechnicGyro class to ev3devlight. For now I intended to support just LEGO sensors.

@laurensvalk
Copy link
Owner Author

After the install the link fails:

Thanks. I recommend to use micropython, though. Running a program takes just seconds.

@laurensvalk
Copy link
Owner Author

I should also add that this is quite experimental so far, and just intended for those who like to code segway robots 😄

@BertLindeman
Copy link

In the future I might add the HiTechnicGyro class to ev3devlight. For now I intended to support just LEGO sensors.

If there is a workarond - fine by me. Will try possibly today.

Thanks. I recommend to use micropython, though. Running a program takes just seconds.

Will even look at micropython (so we get all the speed we can get out of the little baby) and by your latest comment, I am well at my place here :-)

I should also add that this is quite experimental so far, and just intended for those who like to code segway robots 😄

Thanks Laurens,

@laurensvalk
Copy link
Owner Author

All you'd need to do is:

 ./segway.py

or run it from vscode. Because of the comment at the top:

#!/usr/bin/env micropython

micropython is automatically used in both cases.

@laurensvalk
Copy link
Owner Author

@antonvh.

@BertLindeman
Copy link

Got the EV3-gyro sensor and tried your current code including the remote steering.

First testing on soft-floor (easier for the bot to survive a hard drop).
Few minutes and jerky movements (Dutch: schokkerig).
Maybe I still have too many services running.

Stopped WIFI and these services:
nmbd.service, smbd.service, bluetooth.service, ev3-bluetooth.service, avahi-daemon.service
cron.service atd.service ntp.service bluetooth.service

Ran on hard floor (Vynyl) for about 15 minutes.
Less jerky, looking stable, sometimes recovering a bit jerky but mostly stable. When I was not looking it dropped. the ev3devlight/segway.py.err.log log contains:

Traceback (most recent call last):
  File "/home/robot/ev3devlight/segway.py", line 77, in <module>
  File "/home/robot/.micropython/lib/ev3devlight/sensors.py", line 117, in rate
  File "/home/robot/.micropython/lib/ev3devlight/fileio.py", line 17, in read_int
OSError: 6

Maybe this error occurs after the drop.

After dinner will look at services still running and try again.
Was tempted to stop brickman.service but that makes running the test rather difficult ;-)

@laurensvalk
Copy link
Owner Author

laurensvalk commented Apr 17, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants