Ultrasonic sensor with simulink

조회 수: 40 (최근 30일)
John
John 2012년 9월 17일
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.

답변 (3개)

Ryan G
Ryan G 2012년 9월 17일
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
John
John 2012년 9월 20일
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 2012년 9월 21일
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.

댓글을 달려면 로그인하십시오.


Rolfe Dlugy-Hegwer
Rolfe Dlugy-Hegwer 2012년 9월 18일
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 2012년 9월 19일
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
John
John 2012년 9월 19일
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 2012년 9월 24일
편집: Rolfe Dlugy-Hegwer 2012년 9월 25일
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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Modeling에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by