ev3dev / ev3dev-lang-python

Pure python bindings for ev3dev
MIT License
428 stars 145 forks source link

ev3 Multiplexer running different sensors #182

Closed Master-Matt closed 8 years ago

Master-Matt commented 8 years ago

I am trying to set up the ev3 multiplexer with 2 touch sensors and one color sensor. I'm having trouble setting each port to analog or digital, and then setting what device is on each port then reading values off the port. Any documentation on how to do this in python? I'm trying to use core.py as my base, but still having issues. Thanks ~Matt

dlech commented 8 years ago

No python docs. Just this.

I don't see anything in the python docs, but there should be a Port class that lets you manipulate the ports.

Master-Matt commented 8 years ago

I'm not sure on the syntax for using a port. with the touch sensor directly connected I can do something like ts=ev3.TouchSensor('in2'), but with the ports can I do something like this? mux2=ev3.LegoPort('in2:12c82:mux2')

*my brick just ran out of batteries and my rechargeable pack is in the mail, so I can't test more until i go grab some double-A's...

dlech commented 8 years ago

Yeah, it should be something like that.

Master-Matt commented 8 years ago

Ok, when trying above, making sure the address is valid, I get this error. FileNotFoundError: [Errno 2] No such file or directory: '/sys/class/lego_port'

rhempel commented 8 years ago

@Master-Matt - are you using the latest (0.7.0~rc1) version of the Python binding (python3-ev3dev)? The reason it's still ~rc1 is that there may still be some corner cases for device classes that are not working as expected.

Master-Matt commented 8 years ago

I'm not sure. I thought it was the 0.6.0, but how do I check in python or just on the brick? *I also can't anything to work using LegoPort

Master-Matt commented 8 years ago

Python allows me to do this, but I don't seem to get any attributes that are useful to me mux3=ev3.Device('lego-port', driver_name=['ms-ev3-smux-port'], address='in1:i2c82:mux3', mode='analog') This doesn't really do anything, as checking my connection always gives me false.

ddemidov commented 8 years ago

183 adds documentation for LegoPort class: http://python-ev3dev.readthedocs.io/en/latest/other.html#lego-port

dlech commented 8 years ago

FileNotFoundError: [Errno 2] No such file or directory: '/sys/class/lego_port'

Looks like a bug. Should be lego-port, not lego_port. However, this has been fixed since 0.6.0 already.

I'm not sure. I thought it was the 0.6.0

Unless you are using a nightly ev3dev image from 2016-07-26 or later, you probably are using 0.6.0.

To get 0.7.0rc1, run sudo apt-get install python3-ev3dev. This also requires that you upgrade your kernel sudo apt-get install linux-latest-ev3. Or grab a nightly image and re-flash your SD card. Download

As you might guess by the name, the new package is python 3 only.

Master-Matt commented 8 years ago

I did check, and yes I'm using 0.6.0. I will run the update and try again. I also have the absoluteIMU from mindsensors, and was wondering how I would communicate with it. Would it be the normal sensor class or the special sensor class?

Also, I'm ready to start testing elements of my program on my actual robot. Up untill now I've just connected everything in a lab setting. What is the fastest/easiest way to load my program from my comp to the brick, then run it from the brick?

Thanks for your help guys. Once I get a good test run in, I'll show you my M.O.O.C.A.

dlech commented 8 years ago

absoluteIMU

There is a driver for that one. You can find a list of all supported sensors here.

What is the fastest/easiest way to load my program from my comp to the brick, then run it from the brick?

Assuming your computer is Windows, Filezilla or WinSCP. Save the file to /home/robot on the EV3. Also be sure to check set the executable permission on the file. Then you can start the program from the file browser on the EV3. You can also use a remote ssh terminal to start the program.

Master-Matt commented 8 years ago

Okay, I guess It's just a naming convention thing because when I try s=ev3.AbsIMU('in3') or s=ev3.ms-absolute-imu('in3') it doesn't work. is it all caps like s=ev3.MsAbsoluteImu('in3')? or do I have to use a base-class then load the driver? or would this work s=ev3.LegoSensor('in3', driver_name='ms-absolute-imu')

ddemidov commented 8 years ago

The correct syntax is

gyro = ev3.Sensor(driver_name='ms-absolute-imu')
assert gyro.connected
gyro.mode = 'TILT'
print(gyro.value(0), gyro.value(1), gyro.value(2))
Master-Matt commented 8 years ago

Sweet. Do you recommend cycling through the modes to take a reading off each? 'ALL' mode seems a bit trickier to parse the data.

ddemidov commented 8 years ago

This used to work for me:

gyro.mode = 'ALL'
v = gyro.bin_data('<bbbhhhhhhhhhh')
print(v)
Master-Matt commented 8 years ago

Im trying to run sudo apt-get install linux-latest-ev3 and i get this message E: Unable to locate package linux-latest-ev3

dlech commented 8 years ago

that's because I told you the wrong thing. it is linux-image-ev3dev-ev3

Master-Matt commented 8 years ago

Well the nightly flash went fine, but now I can't seem to connect to the internet no matter what I do... Any tips on how to get this brick to stop being a brick? Also- the date is set to January 1 1970 I've tried DHCP, Load Windows Defaults, and entering in Manual static info that meshes with my LAN. I get Putty going fine but sudo apt-get update never connects

dlech commented 8 years ago

Any tips on how to get this brick to stop being a brick?

Disable Internet connection sharing on windows and enable it again.

Also- the date is set to January 1 1970

This will be corrected when you connect to the internet. There is no battery backup for the clock.

Master-Matt commented 8 years ago

Grrr... I hate overlooking the simple things... Thanks!

Master-Matt commented 8 years ago

Thanks a lot guys. Mux is working great. Is it recommended to assert after assigning any sensor? I haven't had any issues so far without it, but I'm wondering what the benefits are.

ddemidov commented 8 years ago

I guess there are no benefits except you terminate early if the required sensor is not connected (launched program for wrong robot?).

Master-Matt commented 8 years ago

Is there any way to add RGB-RAW mode to the color sensor when used in the multiplexer? I want to use the separate values in my calculations.

Master-Matt commented 8 years ago

@ddemidov when using mode 'ALL' on the AbsoluteIMU does the data read left to right as it's displayed up to down on the ev3dev site>

ddemidov commented 8 years ago

Yes, I believe so. It should 3 values for TILT, 3:ACCEL, a COMPASS, 3:MAG, and 3:GYRO.

Master-Matt commented 8 years ago

So, I'm planning on using the absolute imu to estimate position and velocity by integrating the acceleration vector. To do so this I need good info on the sampling rate of the sensor data. Do you think the best way would be importing the time module and tacking on a time stamp for every sample?

WasabiFan commented 8 years ago

@Master-Matt Although I can't help you with the sensor details, I'll warn you that integrating IMUs like that is a very inaccurate way of finding position. You will probably get large drift rates and your position readings will be wildly wrong within a matter of seconds. This is due to the nature of such an operation; I expect that you would need an update rate that is much higher than what the EV3 can provide in software to achieve reliable position tracking with an IMU. Usually, when systems are built that do this, they use a hardware loop or software on a dedicated chip running at very high clock speeds.

rhempel commented 8 years ago

I'll agree with @WasabiFan here - you need something like a Kalman filter and another way of estimating and correcting position. You'll be integrating twice accel->velocity->position and paradoxically, the faster your sample rate the worse it gets due to round-off and accumulated error.

On the other hand, this is a great opportunity to learn just how hard it is to make this work - so go ahead and do the experiment but then go back to student mode and read about some of the math involved in getting better results.

Master-Matt commented 8 years ago

Yeah, I have been doing some research into this, and although there are some nice algorithms out there that can do this, It seems like the results wouldn't be very reliable. I might have a way around this by only using this as one part of my position tracking package. My project is an ev3 powered robotic lawn-mower, and I am planning on using the sumoeyes for obstacle detection, and I also have an xv-11 LIDAR that will be used. My plan for the IMU is just to fill in the gaps when the lidar is not in a range that it will provide reliable data. Mainly for the middle parts of a lawn. I also want to keep my integration period at or less than a second, depending on the sample rates I can achieve with the EV3. I might have to switch over to c++ though, since both my Quaternion class I've made and the xv-11 code I've seen is written in that.

Master-Matt commented 8 years ago

I probably should have just spent the extra money and gotten drive motors with Quadrature encoders, but I always like a good challenge!

Master-Matt commented 8 years ago

Quick question. What's the shortest way to check if a button is pressed? I see any, and proccess, but I'm unsure how to use them. I've Tried ev3.ButtonBase.check_buttons==True and I keep getting true, I think I'm just checking that check_buttons is an attribute of the ButtonBase class hah...

ddemidov commented 8 years ago

This should work:

btn = ev3.Button()
if btn.up:
  print("Up button is pressed")

Also, see http://python-ev3dev.readthedocs.io/en/latest/other.html#button