<< return to Pixycam.com

Simulink S-function for Reading Pixy Outputs in Arduino

I am trying to create an S-function in Simulink for reading the Pixy Outputs over SPI on the Arduino Uno.

The idea is to develop a control system on Simulink and run it on the Arduino. Pixy is my sensor and I need to develop a Simulink block to output the data in pixy.blocks[i].

I tried following this youtube video: https://www.youtube.com/watch?v=_OLctOFjjYQ and even though the code compiled and downloaded to the Arduino, there was no output when I ran the model in external mode.

Anyone has experience in this area?

Problem solved. If you are encountering similar issues:

  1. Copy all the files from the Pixy Arduino library to your Matlab working directory

  2. Copy SPI.h and SPI.cpp from your “Arduino install folder”\hardware\arduino\avr\libraries\SPI\src to your Matlab working directory

  3. Follow the procedure in the video I linked in my original post

  4. Stuff to include:
    #ifndef MATLAB_MEX_FILE
    #include <Arduino.h>
    #include “SPI.h”
    #include “SPI.cpp”
    #include “Pixy.h”
    Pixy pixy;

  5. Custom Start Code
    #ifndef MATLAB_MEX_FILE

  6. Output Function, edit as necessary to pull out whatever variables you need to the outputs of the S-function block
    #ifndef MATLAB_MEX_FILE
    uint16_t blocks;
    blocks = pixy.getBlocks();
    if (blocks){

else {

1 Like

I tried to follow your steps but it didn’t work. Can you please send my the model or can I send you my model so you can check it ?

did you figure it out ?

@bentay I’m trying to use SPI.h in a very basic Simulink project, but the compiler always chokes on it. I’ve included it just like you have in your example above. Any tips for getting it past the compiler? The first error it gives me is:

E:/Documents/MATLAB/Autotune/SPI.h: In member function 'void     SPISettings::init_AlwaysInline(uint32_t, uint8_t, uint8_t)':
E:/Documents/MATLAB/Autotune/SPI.h:114:20: error: 'F_CPU' was not declared in this scope
       if (clock >= F_CPU / 2) {

It looks like a header file is missing that includes the constant F_CPU. What platform are you using? (Arduino, Raspberry Pi?)