Ultrasonic sensor with simulink

40 views (last 30 days)
John
John on 17 Sep 2012
I am using an arduino Mega 2560 board with Simulink. I have installed the simulink library for this board using 'targetinstaller'. I have also built a simple system with a hobby rc servo using the predefined Simulink blocks. I am trying to using the Ping Ultrasonic Range Finder from Parallax as a position feedback sensor in my control algorithm. However, I do not see any code blocks written for this in the simulink library. I have found a tutorial from Arduino's website that explains how the distance can be read from the sensor: http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor . Is there any way this code can be turned into a simulink block? I'm not good with writing code so a step by step explaination would be appreciated. I just want a block that provides the object's distance in inches with no input. An option to adjust the sampling time would also be useful. I am running Matlab 2012a on windows 7 professional 64-bit.

Answers (3)

Ryan G
Ryan G on 17 Sep 2012
You're best bet is a s function builder but you probably won't get a lot of assistance here beyond that until you put in a good amount of effort to get the job done yourself.
If you do manage to pull this together I'm sure the Arduino community and file exchange would appreciate having the working block uploaded.
  4 Comments
John
John on 20 Sep 2012
I did some more research on this topic and I'm now thinking I can use Simulink's stateflow chart to implement the logic I need along with the arduino Simulink blocks. I'm still pretty confused though on how the state flow charts actually work. Also, I'm not sure how accurate the timing will be either.
Ryan G
Ryan G on 21 Sep 2012
Simulink generates code for 'real time' operation (again, I'm not the most familiar with the whole arduino part). Depending on what oyu mean about accurate with timing, Simulink does 2 things:
1) At every time step, simulink is going to process everything in the model. This means it will grab the inputs -> follow lines to stateflow ->process stateflow chart -> generate outputs in the s-function for arduino -> send the outputs to arduino
Depending on how you decide to actually order these things.
2) Simulink takes this model and generates code for arduino. It will run as fast as arduino tells it to run given the context of sample time.
As far as implmenting stateflow, it can be tough when first encountered since it is a bit different from base simulink. Stateflow had a major update in 2012b, unfortunately you're on 2012a. There's a lot of demos available in the documentation and videos online, I would check those out to get started. Stateflow can be as complicated or simple as you want depending on how you design the chart.

Sign in to comment.


Rolfe Dlugy-Hegwer
Rolfe Dlugy-Hegwer on 18 Sep 2012
The easiest solution might be to use a separate Uno board with the Ping sensor. Then transfer the value back to the Mega via one of the digital serial pins.

Rolfe Dlugy-Hegwer
Rolfe Dlugy-Hegwer on 19 Sep 2012
Another possible approach might be:
  • Add a MATLAB Function block to the model.
  • Port the logic portion of the Arduino code to MATLAB code
  • Use the coder.ceval function to call standard built-in Arduino functions, such as pulseIn().
  • Use pinMode(), digitalRead(), digitalWrite() (avoid Arduino Digital Input/Output blocks, which do not have pinMode)
  • Adjust the timing of code in MATLAB Function block to fit within the timing of the model.
This would require a fair amount of experimentation.
  2 Comments
John
John on 19 Sep 2012
I was going to use the Matlab support package mentioned here: http://arduino.cc/playground/Interfacing/Matlab . However this requires the program srv.pde to be installed on the arduino. When I create my simulink model this program will go away and be replaced with the simulink program and the Matlab commands such as pinMode() will no longer work. How can I work around this?
Rolfe Dlugy-Hegwer
Rolfe Dlugy-Hegwer on 24 Sep 2012
Edited: Rolfe Dlugy-Hegwer on 25 Sep 2012
You have to choose between the MATLAB Support Package for Arduino (aka ArduinoIO Package) or Simulink Support Package for Arduino Uno and Mega 2560 hardware. Have you tried using pulseIn() with the MATLAB support package?

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!