Ultrasonic sensor with simulink

35 次查看(过去 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?

请先登录,再进行评论。

类别

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