MegaPirateNG / ardupilot-mpng

MegaPirateNG
GNU General Public License v3.0
116 stars 105 forks source link

Sonar Support #32

Open mfc123 opened 10 years ago

mfc123 commented 10 years ago

I have the HC-SR04 ultrasonic module on the crius AIO v2 and I'm trying to mess with your code to try make it work but no luck :/ On your 2.9.1b addons it works good but I have the mottor twitch bug :/ today throttle was at 0% and when disconnecting the battery it ripped the flesh out of my fingers :( now back to 2.8r3... Can you help please?

smurfy commented 10 years ago

@kilker12 seams to work on support for this, at least he has related commits in his fork. But i don't know the status or quality of his changes.

mfc123 commented 10 years ago

Donwloaded the latest source and flashe and no luck getting the sonar to work!

Can someone help?

smurfy commented 10 years ago

Than his code is not ready, hence no pull request. Lets wait and see. As stated before by SirAlex, Sonar support is not in yet.

kilker12 commented 10 years ago

Honestly its a blind shot at getting it working. Im still learning the HAL library and how it works. Currently my quad is awaiting repair parts to ship from HK so I havent done any hardware tests yet.

kilker12 commented 10 years ago

Ive read that the HC-SR04 sonar could not work with Ardupilot because of its need for a large amount of CPU power to do the calculations and wait for the sonar to respond. Other sonars which the DIYDrones version supports all do these calculations on the sonar themselves and feed the value to the APM instead of the APM doing all the work. In 2.9 of MPNG he used what I think are some libraries from Arduino which Ardupilot sought to get rid of. I think Im going to leave this work to @SirAlex because I dont know enough to make it work.

smurfy commented 10 years ago

If there is a problem with performance, i'm currently thinking about creating a small arduino sketch for an attiny 45/85 to handle the sonar triggering and then displaying the result via an analog pin.

Then it should be possible without code changes to handle the sonar.

smurfy commented 10 years ago

After looking in the code and how the hc-sr04 sensor works i think it's not a wise idea to use the sensor directly with arducopter.

The problem is PulseIn it causes the the code to hang. PulseIn is needed, because the hc-sr04 works that way, that it switches the state of the echo pin as soon as the reply to the trigger receives the sensor. The range is then calculated based on the time it took. So i think the only solution performance wise to support the sensor would be offloading the "calculation" to another hardware. the attiny for example. i have two attiny85 around so i will do some tests.

dkillion commented 10 years ago

I've not been following this very closely, but I've been playing with the HC-SR04 as well with a mind towards using it on my quadcopter.

I quickly discovered the stock code for it for Arduino is horrible, and was very frustrated until I found this bit of code from Tim Eckel:

http://playground.arduino.cc/Code/NewPing

His code samples the sonar successfully at up to 20 Hz and works fine on my Leonardo. It uses a delay() loop, but that's only for the time between pings. A simple ranging sketch that outputs the sensed range to Serial is ~3k.

Have you given this a look yet?

smurfy commented 10 years ago

@dkillion thanks for the lib. i will do some tests with the lib on my arduino mega and also in code, i have some ideas :)

SirAlex commented 10 years ago

MPNG 2.x has very efficient Sonar driver. It uses interrupts to catch Echo pulse width. Also it uses timer overflow for initial Trigger signal. But, MPNG 3.1 uses TIMER5 for micors and millis functions so we can't use it for other purposes. So now, problem not in performance. The problem in available timers (we have no suitable timer). So maybe external sonar with I2C bus will be good solution.

sufferpriest commented 10 years ago

"So maybe external sonar with I2C bus will be good solution." It's really a bad news for I use this mode in MWC before.

smurfy commented 10 years ago

@sufferpriest: i think something like this, except using i2c could be the solution. I try to get something like this working, but i need to order some resistors first.

The pricing for the components should be $3 tops. (excl. the sensor itself)

bektorkhan commented 10 years ago

@smurfy I would also like to contribute, I am still in MPNG 2.8.3 with a SR-04 like you. How far have you come with Tiny85 Sonar>I2C code?! Not wasting time on it if your'e hacking away! I have some electronics design experience and working with Tiny85 currently, I could make a quick SMD PCB in Eagle and post somewhere and I have already used NewPing to monitor our well, brutally putting the depth of water on internet. (HC-SR04 > UNO > LCD > JunkWebcam > openwrt tl-mr3220 router > directional antenna > my house/server)

smurfy commented 10 years ago

@bektorkhan i just did some research, if you have time and knowlege just start hacking. i will start the earliest next week.

I think the code i linked before is a good starting point. (mainly needs the i2c code and some wireing documentation)

bektorkhan commented 10 years ago

I think the TinySonarBoard should have booth PWM analog and I2C output to make it compatible with current software. I'll start hacking, is there any thoughts on I2C adress to use, I am new to I2C..

smurfy commented 10 years ago

@bektorkhan it should be an i2c slave and the address as a constant. i'm not sure what we should use as address, we should use one which does not conflict with gps.

an alternative could be simulating the existing but not in use MaxsonarI2CXL (scaling and address...)

About PWM, the problem is like all atmel chips we can only output pwm, not a real analog voltage like the original maxsonar class uses for input. i found some description for a diy analog voltage out but i'm not sure if this is better than using i2c

bektorkhan commented 10 years ago

@smurfy PWM and is about the same with an RC filter as, shown in the links you pointed to, as an analog output, a buffer is not needed hence the load an input pin is very low. Noise wise it would be best if the RC filter was on the FC board. The resolution will suffer though, it will probably be 2-3 cm with analog. The NewPing lib gives very good readings on a flat surface, the ground will not be so smooth. I will use the integral function in NewPing averaging about 10 samples. For energy saving and for wildlife preservation I'll implement the take_reading() function as in RangeFinder_MaxSonar.

smurfy commented 10 years ago

@bektorkhan: i would focus on i2c, because it gives us more flexibility and the correct resolution.

For energy saving and for wildlife preservation I'll implement the take_reading() function as in RangeFinder_MaxSonar.

i guess you mean the i2c version, the thing is, the class is currently not used in the code and i'm not sure about how to implement this cleanly. also you still have a delay after requesting and getting the data. so i think an auto update every x ms would be better.

i would suggest instead of having a "now take reading i2c command" a command to set the update rate to the sensor. i'm not 100% sure, if that works but i think it is possible to even make the option available as a parameter to mission planner. also the min and max cm of the sensor. (look at the analog rangefinder code)

Black6spdZ commented 10 years ago

why use the digital I/O at all with the MaxBotix sensors? wouldn't it be simpler to just sample the PWM or analog output from it? The scaling is already documented and a simple calibration could be added to fine tune the reading.

smurfy commented 10 years ago

@Black6spdZ the discussion is not about the maxsonar sensors, they should be already supported by the current version of mpng. The discussion is primarly about support for cheap sensors like the HC-SR04. The HC-SR04 is not longer supported by mpng because the way the sensor is accessed is not longer supported. We now discuss a cheap way to still use the HC-SR04 sensor.

Black6spdZ commented 10 years ago

ah.. I see.. well would a simple picaxe be able to bridge the gap and emulate a MaxBotix PWM output? found this snipit of code for the picaxe:

ping: pulsout 7,1 ; send a 10uS trigger pulse on output 7 pulsin 1,1,w0 ; read the pulse on input 1 and store the result in w0 pause 10 ; SRF05 required 10mS recharge period after ranging completes return

Calculating distance based on the duration of the pulse is a fairly straightforward affair. If the pulse is known in microseconds it can be divided by 58 to yield the approximate distance in centimeters. PULSIN on the PICAXE returns a pulse measured in 10 microsecond increments - a PULSIN result of 15 equals 150 microseconds. Therefore to make a distance calculation on a PICAXE continuing the code sample above:

w0 = w0 * 10 / 58 ; distance in centimeters

so the MaxBotix outputs 147uS/inch or approximately 58uS/cm

http://picaxe.hobbizine.com/srf05.html

what PWM frequency is used by the MB sensors? does it really matter? looks like a a little math and that measuerd pulseout/in on the SR04 could be converted to a usable PWM signal for the APM?

smurfy commented 10 years ago

@Black6spdZ that was the idea but instead of the picaxe using a attiny85.

ralphwu commented 10 years ago

I got a SR04 Sonar with an add on board that are currently using in APM. I think I can also be used in MegaPirateNG.

smurfy commented 10 years ago

addon board

which board do you using?

smurfy commented 10 years ago

@JonathancalderonIL

Got my code for sonar on my attiny85 (currently on a digispark) working. I will adapt the ardupilot code to get i2c working as-well, but i have small problems using the maxsonar i2c code so i currently working on a separate class for it.

Once finished i will do some tests with ardurover and then copter.

The way maxsonars code works is it requests the current range via a i2c command and the sonar (attiny in my case) answers. Of course the answer is cached on the attiny side. The sonar requests the range all the time and "instantly" sends the last known range back.

I'm not sure about the performance, of request -> answer or if there is another better way like the attiny sends the range every x ms or every second. Maybe @SirAlex could give me a hint.

smurfy commented 10 years ago

Ok, here is my code for the attiny85: https://github.com/smurfy/hcsr04_i2c_attiny85

I will do more tests with the sonar and ardupilot code. I just tested with an arduino mega and the test example.

nilsheidorn commented 10 years ago

Hello smurfy :-) Great thanks, i just ordered some attiny85 ! Could you shortly describe where / how to connect the sonar & i2c pins ? Sorry if that seems daft bit i simply dont know it and there does not seem to be a description of the electrical wiring in the .ino

Thank you !

smurfy commented 10 years ago

@nilsheidorn sure i will add a wiring diagram soon.

Also there are still some changes needed to the RangeFinder code to use "AP_RangeFinder_MaxsonarI2CXL" instead of the analog variants.

nilsheidorn commented 10 years ago

Thanks !

smurfy commented 10 years ago

Wow take a look here: http://www.megapirateng.com/forum/viewtopic.php?f=4&t=47

smurfy commented 10 years ago

@SirAlex i just noticed you are active in the linkend discussion. i'm quite interested how my emulated Maxsonar i2c XL performs :)

smurfy commented 10 years ago

OK i'm quite bad in using fritzing it seams: https://i.imgur.com/DGqlW79.png Hope i did no error :)

nilsheidorn commented 10 years ago

Looks good enough :-) While waiting for my attiny85: You are saying that the current code is "good enough" to convince a MegapirateNG or APM board to see this solution as Maxsonar, right ? Or do i need to patch the APM source to make it work ? (You linked to that other thread in which it seems like that but that was another solution, right ?)

Thanks for your effort !

Nils

smurfy commented 10 years ago

The current version of ardupilot and mpng only support ANALOG sonar sensors. There is a I2C version of Maxsonar out there but the code is not used yet. Also the I2C code is not 100% compatible with MPNG. The forum thread i linked contains a version of the Maxsonar I2C code integrated to MPNG. It should work with my attiny85 version too. (instead of a real MaxSonar I2C sensor).

nilsheidorn commented 10 years ago

Ah okay, thanks for clearing that up ! WIll try with a MGPNG first, if it works i will recompile the stuff for my original APM.

Greetings,

Nils

JonathancalderonIL commented 10 years ago

Hi Nils and smurfy i am looking to acomplish the same thing. HC-SR04 to work with MPNG on multiwiipro (red HK board) it looks like you are on the path of solving this but its hard for me to anderstand how you did this. do i need to buy a second attiny85 board ? and what kind ? do i need to flash my board again with canges to the MP code ?

please add some details... thanks Jonathan

nilsheidorn commented 10 years ago

Hi Jonathan, i am not there yet as i did not get my attiny85 (and i need time, talk to my two sons for that ;-)

As far as i understand it there are 2 major sorts of attiny85: the ones flashable via USBASP and some with USB connection on board (easier but more expensive). I ordered the simple chips as i have a solder iron and an USBASP, trying to stay inexpensive.

The things to do as i understand it: -Flash the attiny85 with smurfys code -patch and flash MegaPiratesNG / APM with the patches smurfy linked to to enable i2c MaxSonar. -Connect and have fun

Will report once i got the hardware and sometime !

Nils

JonathancalderonIL commented 10 years ago

thanks alot. can you send a link for the attiny85 you got,,, do you mean buy only the attiny85 chip? and then solder the USBASP and flash the code ? this part sound simple... do you know where do i need to connect this in the red HK MULTIWII after flashing everything ?

nilsheidorn commented 10 years ago

Hi ! Yes, i only ordered the chip. search for "flash attiny85" and you see where to solder usbasp or other methods. smurf posted the "final" connection diagram above. No more to say as i did not try myself yet :-)

Nils

JonathancalderonIL commented 10 years ago

thanks. i have paied for one now hope to get it soon.. china post is slow... will update as soon as it gets here

smurfy commented 10 years ago

I flashed my attiny85 with my arduino mega. I also suggest using an socket for the tiny for easy removal, and reflashing/updates if needed. I currently have everything on a breadboard.

JonathancalderonIL commented 10 years ago

thanks smurfy... was planning to solder to the chip but your post stopped me. got few welding boards and few 8 pins chip base. my plan is to make: 1 board for the connection to the programmer (USBASP) 1 board that would be sitting on my multiwii board (RED HK) i hope to have good results from this project so i dont have to move my attiny85 chip too much.

is there a simple guide for how to flash the attiny85 ?

this is the items i orderd and should be here in few days... would send some more pictures after i solder everything in place... product_332_774_0x0 product_321_2551_0x0

smurfy commented 10 years ago

is there a simple guide for how to flash the attiny85

i used my arduino, that was quite simple, arduino ide also knows usbasp so i guess that works too.

Only problem i'm currently facing is, that i have problems running the code on a attiny85 directly (digispark works).

I flashed my software successfully, the sonar does it thing (i can hear it) but somehow the i2c connection won't work.

Its a bit work to reflash the tiny, because i use my arduino mega to flash and to test so i have to rewire and reflash everything ^^

I will do some more tests, i'm new to arduino programming myself so only thing i currently can think of is that its has something to do with attiny's internal clock speed.

JonathancalderonIL commented 10 years ago

i see... ok then i hope to have everything here soon to try testing. i re-fritz your schmatics on fritzing... what kind of 4.7K resistor is needed ? why do we need them actually ? please post some photos of your wiring.

Jonathan

sonar project_schem

smurfy commented 10 years ago

Be aware in your, and my schematic the tiny is flipped: attiny pinout (pin numbers are correct, just the sides are flipped) (Pin8 is VCC and Pin4 is GRD)

Also i use metal 4.7k as pullup resistors you could use carbon resistors too, metal was just the ones i had lying around.

JonathancalderonIL commented 10 years ago

going to get few 4.7k resistors thanks for the link on the pullup resistors.

is this schematic looks better ? sonar project_schem3

SirAlex commented 10 years ago

Are you sure, external pullup is needed? Why not use internal and even not use (I think, mainboard FC already has pullups on)

bektorkhan commented 10 years ago

Don't forget decoupling capacitor of 47-100nF across the power close to the attiny85! I might get around to adapt a board I've done for this.. cheap pcb fab suggestions?

smurfy commented 10 years ago

@JonathancalderonIL no, at the first glance don't connect vcc with pin1 (i dont have such a connection in my ugly schema :))

@SirAlex not completly sure, the digispark did not work without pull ups. I will do another test with the attiny directly and an arduino mega just to test the i2c connection. I found another tutorial for i2c and an atiny85 without those pullups, but to be on the safe side i will retest without them again too.

@bektorkhan what do you mean with the capacitor? (i'm quite new to the whole arduino/atmel stuff and a bit confused why it works on the digispark perfectly and on the native attiny it simple does not

JonathancalderonIL commented 10 years ago

updated the schematic at the above post https://github.com/MegaPirateNG/ardupilot-mpng/issues/32#issuecomment-49151115