austinbv / dino

Dino is a ruby gem that helps you bootstrap prototyping with an Arduino
MIT License
388 stars 84 forks source link

Adding support for HC-SR04 sensor #64

Closed vickash closed 1 year ago

vickash commented 10 years ago

@rosscdh, test as follows:

  1. Download this branch.
  2. From the project dir, run ./bin/dino generate-sketch serial.
  3. Upload the generated dino-serial sketch to your board.
  4. Customize and run the example in 'examples/hcsr04/hcsr04.rb'. It should return microsecond vlaues.

Not sure how you convert microseconds to distance for these, but that's just a matter of adding functionality to the Ruby class, so you can submit a pull against this one.

rosscdh commented 10 years ago

ok heres what iv done.

git co hcsr04
gem build dino.gemspec
gem install dino-0.12.0.gem

then

dino generate-sketch serial

and installed the ino file

then run

ruby hc-sr04.rb

and i get

/Users/rosscdh/.rvm/gems/ruby-1.9.3-p392/gems/dino-0.12.0/lib/dino/components/hcsr04.rb:5:in `_read': undefined method `ulstrasonic_read' for #<Dino::Board:0x007fae3aad0c38> (NoMethodError)
    from /Users/rosscdh/.rvm/gems/ruby-1.9.3-p392/gems/dino-0.12.0/lib/dino/components/core/base_input.rb:19:in `read'
    from hc-sr04.rb:10:in `<main>'
vickash commented 10 years ago

That's a typo. Just pushed a fix so you can pull it down.

Make sure you build and upload the sketch from this branch. Installing the gem is not necessary if you run the example from the working copy.

On Tuesday, October 8, 2013 at 11:13 AM, rosscdh wrote:

ok heres what iv done. git co hcsr04 gem build dino.gemspec gem install dino-0.12.0.gem
then run ruby hc-sr04.rb
and i get /Users/rosscdh/.rvm/gems/ruby-1.9.3-p392/gems/dino-0.12.0/lib/dino/components/hcsr04.rb:5:in _read': undefined methodulstrasonic_read' for #Dino::Board:0x007fae3aad0c38 (NoMethodError) from /Users/rosscdh/.rvm/gems/ruby-1.9.3-p392/gems/dino-0.12.0/lib/dino/components/core/base_input.rb:19:in read' from hc-sr04.rb:10:in

'

— Reply to this email directly or view it on GitHub (https://github.com/austinbv/dino/pull/64#issuecomment-25898772).

rosscdh commented 10 years ago

Thanks seems to get some feedback now :) Ill faff around with it for a bit! and give you some feedback!

Thanks for your efforts!

vickash commented 10 years ago

It's printing the readings?

On Tuesday, October 8, 2013 at 11:19 AM, rosscdh wrote:

Thanks seems to get some feedback now :) Ill faff around with it for a bit! and give you some feedback! Thanks for your efforts!

— Reply to this email directly or view it on GitHub (https://github.com/austinbv/dino/pull/64#issuecomment-25899338).

rosscdh commented 10 years ago

Hey there, sorry i had to head home! I'll give you some feedback tomorrow.

The microseconds will be used in calculating distance:

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
vickash commented 10 years ago

I meant, does the example print the millisecond readings?

On Tuesday, October 8, 2013 at 1:37 PM, rosscdh wrote:

Hey there, sorry i had to head home! I'll give you some feedback tomorrow. The microseconds will be used in calculating distance: long microsecondsToCentimeters(long microseconds) { // The speed of sound is 340 m/s or 29 microseconds per centimeter. // The ping travels out and back, so to find the distance of the // object we take half of the distance travelled. return microseconds / 29 / 2; }

— Reply to this email directly or view it on GitHub (https://github.com/austinbv/dino/pull/64#issuecomment-25911075).

masha256 commented 10 years ago

I have a couple of these sensors and am so glad there is active word to support them in dino! One question about this patch though - all the examples online for arduino and HC-SR04 show two digital pin connections to the aruduino; one for triggering and one for sensing the input. This patch assumes there is only one digital pin connected to the arduino. Is this a valid HC-SR04 circuit? If so, which pin on the SR04 gets the digital pin connection, Trig or Echo?

masha256 commented 10 years ago

After a bit more research, I saw you can enable "single pin" mode of the HC-SR04 by putting a 1.8k resistor between Trig and Echo and connecting the arduino to the Trig pin, as documented here: http://letsmakerobots.com/node/36885

As it stands, this would be required for this patch, so I would highly recommend documenting that somewhere!

rosscdh commented 10 years ago

@machadolab thats a great tip and you are 100% right :+1: thanks

vickash commented 10 years ago

Sorry. The code is based on: http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor, which looks like a version of this sensor designed to work with only one wire, probably has the resistor built into the PCB.

It's easy to add support for either type of connection, so I'll do that, but you can use it this way in the meantime. Is it putting out valid readings though?

masha256 commented 10 years ago

Ah yes, I used to have a Ping)) but not anymore. I have the actual HC-SR04, which has 4 pins, instead of 3. I added a 2k resistor and its working and the readings seem pretty accurate.

I was having trouble using the sensor.poll method though, which looks like it creates it own thread. Does that mean if you have other devices, such as buttons, those would stop working?

I ended up starting my own thread just for the distance sensor and did sensor.read in an infinite loop.

I haven't tried to reproduce it lately though, so it might have just been a fluke.

vickash commented 10 years ago

Yes, #poll creates a new thread to send commands to the board. This actually frees up the main thread so you can work with other components, not the other way around. It's meant to be a neat wrapper for what you ended up doing.

Can you post the code using #poll, and explain what the problem was?

austinbv commented 10 years ago

@vickash is this good to merge or do you want more time on it?

vickash commented 10 years ago

Need to make it work with 2 pins instead of 1, or probably both ways.

I have a few other branches that need to go into 0.12 that have half finished work in them. Will try to wrap everything up as soon as possible and get that out. Too many good features in 0.12 that people using rubygems still don't have access to.

Can you gem push master in the meantime though? It's updated to 0.11.3 with some important bugs squashed.