using Simulink to control an ADC (MCP3008) on Raspberry Pi SPI
4 views (last 30 days)
Show older comments
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 Comment
Answers (1)
Esther
on 17 Dec 2015
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 Comment
Abdul Khairi Abdul Rahman
on 8 May 2016
Edited: Abdul Khairi Abdul Rahman
on 8 May 2016
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;
}
See Also
Categories
Find more on Raspberry Pi Hardware in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!