using Simulink to control an ADC (MCP3008) on Raspberry Pi SPI

조회 수: 4 (최근 30일)
VCOpll
VCOpll 2015년 5월 7일
i have an ADC (MCP3008) with an Input coming from a voltage divider circuit. The Output of the ADC is then passed to the SPI MOSI of RPi. This digital Signal is converted to its analog value by doing some calculations.
i created a Matlab Function Block:
function voltage = MCP3008(channel_ADC) %#codegen
rpi = raspi('raspberrypi-VfDGtegqgZ','pi','raspberry');
clear mcp3008 mcp3008 = spidev(rpi, 'CE0');
data_in = bitor((bitshift(1,7)),(bitshift(channel_ADC,4))); data_to_ADC = uint16(writeRead(mcp3008,[1,data_in, 0])); voltage = 0; %initialize to define size of output port
highbits = bitand(data_to_ADC(2), bin2dec('11')); voltage = double(bitor(bitshift(highbits, 8), data_to_ADC(3))); voltage = (3.3/1024) * voltage;
This function block is used in the attached Simulink Model
Here are the Errors I got:
1. Base class 'matlab.mixin.CustomDisplay' cannot be loaded.
Function 'raspi.m' (#41.3176.7592), line 102, column 9: "function obj = raspi(hostname, username, password, port)" Launch diagnostic report. Component: MATLAB Function | Category: Coder error
2. Parsing failed.
Function 'MATLAB Function' (#35.57.62), line 4, column 7: "raspi" Launch diagnostic report. Component: MATLAB Function | Category: Coder error
3. Errors occurred during parsing of MATLAB function 'MATLAB Function' Component: MATLAB Function | Category: Coder error
4. Errors occurred during parsing of MATLAB function 'MATLAB Function' Component: Simulink | Category: Block error
5. Error in port widths or dimensions. Output port 1 of 'MCP3008/MATLAB Function/channel_ADC' is a one dimensional vector with 1 elements. Component: Simulink | Category: Block error
Can someone please Point me into the right direction? How can i eliminate These Errors?
How can I use the raspi function to this block model? I tried to add the port number also to the raspi function but it does not work
rpi = raspi('raspberrypi-VfDGtegqgZ','pi','raspberry', '18267');
  댓글 수: 1
wietjes
wietjes 2015년 12월 4일
Hey, i'm having the same problem. Were you able to solve this?

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

답변 (1개)

Esther
Esther 2015년 12월 17일
I don't think the raspi functions from the MATLAB support package for Raspberry Pi are supported for code generation in a MATLAB Function block.
You need to write a driver using the S-Function builder block.
This File Exchange submission may help:
  댓글 수: 1
Abdul Khairi Abdul Rahman
Abdul Khairi Abdul Rahman 2016년 5월 8일
편집: Abdul Khairi Abdul Rahman 2016년 5월 8일
thanks for the link Esther. i am currently developing active power filter controller using raspberry pi and i need to obtained at least 2 analoge signal using mcp3008. is it possible for me to use more then 1 channel for the mcp3008 and how am i going to declare or call the pin channel in the s-function. bellow is coding for output
if(xD[0] == 1)
{
#ifndef MATLAB_MEX_FILE
y0[0] = analogRead(100+chan(0);
#endif
}
this my coding for Discrete Update
if(xD[0] != 1){
# ifndef MATLAB_MEX_FILE
wiringPiSetup() ;
mcp3004Setup(100, 0) ;
#endif
//done with initialization
xD[0] = 1;
}

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

카테고리

Help CenterFile Exchange에서 Raspberry Pi Hardware에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by