using Simulink to control an ADC (MCP3008) on Raspberry Pi SPI
6 views (last 30 days)
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');
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: