s-Function Builder Help

12 visualizaciones (últimos 30 días)
Guillermo
Guillermo el 30 de Abr. de 2013
Comentada: lin jie el 19 de Abr. de 2016
Hello all, I'm trying to create the S-function for the MCP4725 DAC. I've got it working on the Arduino IDE and the code looks like this (extracted from the Adafruit drivers to eliminate the need to copy the Adafruit Includes in the working directory):
****************************************
#include Wire.h
#include "math.h" // include the Math Library
#define DAC_ADD (0x60)
#define WriteOnly (0x40) //command to update AO only
#define WriteAndStore (0x60) //command to update AO and save AO setting to EEPROM
int a;
void setup(void) {
Serial.begin(9600);
Serial.println("Hello!");
Serial.println("DAC tester");
Serial.flush();
Wire.begin(); //join i2c bus as master
}
void loop () {
Serial.println("Enter value for leg 'a' from 0 - 4095, Press ENTER"); //asks for user input at serial monitor
while (Serial.available() == 0) ; // Wait here until input buffer has a character
{
//user input
a = Serial.parseInt(); // new command in 1.0 forward
Serial.print("a = "); Serial.println(a, DEC); //echos user input at the serial monitor
int twbrback = TWBR; //i2c specific and don't understand it
TWBR = 12; // 400 khz //i2c specific and it sets the comm freq.
Wire.beginTransmission(DAC_ADD); //tell the i2c device that data is comming to it
Wire.write(WriteOnly); // first byte in message for MPC4725
Wire.write(a / 16); //Second byte
Wire.write((a % 16) << 4); //third byte
Wire.endTransmission(); //Tell device all instruction shave been trasnmitted
TWBR = twbrback; //i2c specific
}
}
****************************************
NOTE that comments have been wrapped to the next line here. The code works and I can update the Analog Output of the board by entering a count value (0 - 4095) at the serial monitor.
I've tried to create the s function with s function builder but keep getting the following error:
****************************************
Error MCP4725_Output_v1.c: 82 missing parameter type
Error MCP4725_Output_v1.c: 196 type error in argument 2 to `MCP4725_Output_v1_Update_wrapper'; found `pointer to double' expected `int'
Error MCP4725_Output_v1.c: 196 insufficient number of arguments to `MCP4725_Output_v1_Update_wrapper' 3 errors, 0 warnings
C:\PROGRA~2\MATLAB\R2013A\BIN\MEX.PL: Error: Compile of 'MCP4725_Output_v1.c' failed.
****************************************
I put the following in the different s-function tabs
Discrete Update: if (xD[0]!=1) {
# ifndef MATLAB_MEX_FILE
Wire.begin(); // start i2c bus
# endif
// initialization done
xD[0]=1;
}
Outputs: // wait until after initialization is done if (xD[0]==1) {
# ifndef MATLAB_MEX_FILE
int twbrback = TWBR;
TWBR = 0x0c; // 400 khz
Wire.beginTransmission(DAC_ADD);
Wire.write(WriteOnly);
Wire.write(DAC_in[0] / 16);
Wire.write((DAC_in[0 % 16) << 4);
Wire.endTransmission();
TWBR = twbrback;
# endif
}
Libraries (include...):
#ifndef MATLAB_MEX_FILE
#include Arduino.h
#include math.h
// Include the following i2c libraries // pulled from Arduino ver 1.0
#include Wire.h
#include Wire.cpp
#include twi.h
#include twi.c
// DAC Address Defines
#define DAC_ADD 0x60
#define WriteOnly 0x40
#define WriteAndStore 0x60
  1. endif
Data Properties:
INPUT: DAC_in (1-D, 1, , real, off)
OUTPUT: none
PARAMETERS: none
DATA TYPE ATTR...: In_1: DAC_in (uint16, 12, checked, 3, 2^-3, 0)
For the data type I've tried all the settings available and none worked
I've also copied all the libraries to the working directory and I know this is working because I was able to run the Adxl345 I2c Driver for Arduino Mega that this thread inspired....
What am I doing wrong???
Thanks for the help...
  2 comentarios
Jordan Monthei
Jordan Monthei el 13 de Mayo de 2013
Your errors reference lines 82 and 196, what is the code on each of those lines?
Guillermo
Guillermo el 15 de Mayo de 2013
OK, I got it working.
The problem is that for some reason the s-function needs a parameter. So I added a parameter named Par type int8 and initialized to 1. Why it needs this I don't know but it fixed the problem.
@ Jordan, at line 82 (missing parameter type error) there were two comas right after the other. " ...code... , , .....more code ..."
I tried to remove one of the comas and adding a type to the ....more code... which was the input but apparently the space between the comas needs another argument which turns out to be the parameter.
I'll paste the new code, also note that this editor removes the "<" and ">" from the posts so the libraries are listed wrong in the original post.

Iniciar sesión para comentar.

Respuesta aceptada

Guillermo
Guillermo el 15 de Mayo de 2013
Init tab: Number of discrete states: 1 Sample mode: Discrete Sample time value: 0.1
Data Properties tab:
  1. Input ports: (u0, 1-D, 1, , real, off)
  2. No output ports
  3. Parameter: (par, int8, real) in the S-function parameters the value was initialized to 1
  4. Datatype attributes: (In_1: u0, int16, 8, unchecked, 9, 0.125, 0)
Libraries tab:
# ifndef MATLAB_MEX_FILE
#include <Arduino.h>
#include <math.h>
#include <Wire.cpp>
#include <Wire.h>
#include <twi.h>
#include <twi.c>
// DAC Address Defines
#define DAC_ADD 0x60
# endif
Outputs tab:
if (xD[0] == 1){
# ifndef MATLAB_MEX_FILE
int twbrback = TWBR; //i2c specific and don't understand it
TWBR = 12; // 400 khz //i2c specific and it sets the comm freq.
Wire.beginTransmission(DAC_ADD); //tell the i2c device that data is comming to it
Wire.write(64); // first byte in message for MPC4725
Wire.write(u0[0] / 16); //Second byte
Wire.write((u0[0] % 16) << 4); //third byte
Wire.endTransmission(); //Tell device all instruction shave been trasnmitted
TWBR = twbrback; //i2c specific
# endif
}
Discrete Update tab:
if (xD[0] != 1){
# ifndef MATLAB_MEX_FILE
Wire.begin(); //join i2c bus as master
# endif
xD[0] = 1;
}
NOTES:
  1. the libraries need to be co-located in the same folder where the driver s-function is located (There's got to be a better way to manage this)
  2. don't forget to change the *wrapper.c file to .cpp
  3. edit the *wrapper.cpp file to add extern "C" before the void lines (find the s-function builder tutorial, it explains a good deal.)
  2 comentarios
Guillermo
Guillermo el 15 de Mayo de 2013
Next on this is to try to get it to work on the Raspberry Pi substituting the Arduino Wire library with WiringPi libraries.
Crossing fingers.
Barza Nisar
Barza Nisar el 4 de Ag. de 2015
Editada: Barza Nisar el 4 de Ag. de 2015
Hey Guillermo, I am trying to run (and/or deploy) your sFunction on Arduino Mega 2560. I have the arduino support package and also included the Wire.h,Wire.cpp,twi.h and twi.c files in the same folder as the s function. I also changed the wrapper file extension to .cpp and added extern "C" before void wrapper functions. However, I receive the following error to which I have not yet found any solution: The call to realtime_make_rtw_hook, during the after_make hook generated the following error:
The build failed with the following message: "D:/MATLABSupportPack/arduino-1.0.5/hardware/tools/avr/bin/avr-gcc" -I"D:/MATLABSupportPack/arduino/include" -I"D:/MATLABSupportPack/arduino/blocks/sfcn/include" -I"D:/DACsFuncSimulink/MPC4725_toggle_rtt" -I"D:/DACsFuncSimulink" -I"D:/NewMatlab/extern/include" -I"D:/NewMatlab/simulink/include" -I"D:/NewMatlab/rtw/c/src" -I"D:/NewMatlab/rtw/c/src/ext_mode/common" -I"D:/NewMatlab/rtw/c/ert" -I"D:/NewMatlab/toolbox/coder/rtiostream/src/utils" -I"D:/MATLABSupportPack/arduino-1.0.5/hardware/arduino/cores/arduino" -I"D:/MATLABSupportPack/arduino-1.0.5/hardware/arduino/variants/mega" -I"D:/MATLABSupportPack/arduino-1.0.5/libraries/Servo" -I"D:/NewMatlab/rtw/c/src/ext_mode/serial" -I"D:/MATLABSupportPack/arduinomega2560/src" -mmcu=atmega2560 -ffunction-sections -fdata-sections -std=gnu99 -Wall -Wstrict-prototypes -g -Os -D"MODEL=MPC4725_toggle" -D"NUMST=3" -D"NCSTATES=0" -D"HAVESTDIO=" -D"ON_TARGET_WAIT_FOR_START=1" -D"ONESTEPFCN=0" -D"EXT_MODE=1" -D"TERMFCN=1" -D"MAT_FILE=0" -D"MULTI_INSTANCE_CODE=0" -D"INTEGER_CODE=0" -D"MT=0" -D"CLASSIC_INTERFACE=0" -D"ALLOCATIONFCN=0" -D"TID01EQ=0" -D"F_CPU=16000000" -D"ARDUINO=105" -D"_RUNONTARGETHARDWARE_BUILD_=" -D"_ROTH_MEGA2560_=" -D"_RTT_NUMSERVOS_=0" -D"EXIT_FAILURE=1" -D"EXTMODE_DISABLEPRINTF=" -D"EXTMODE_DISABLETESTING=" -D"EXTMODE_DISABLE_ARGS_PROCESSING=1" -c -x none ./MPC4725_S_Function_wrapper.cpp ./MPC4725_toggle.c ./MPC4725_toggle_data.c ./ert_main.c ./HardwareSerial.cpp ./IPAddress.cpp ./Print.cpp ./WInterrupts.c ./WMath.cpp ./WString.cpp ./new.cpp ./wiring.c ./wiring_analog.c ./wiring_digital.c ./io_wrappers.cpp ./rtiostream_serial.cpp ./ext_svr.c ./ext_work.c ./updown.c ./ext_serial_pkt.c ./ext_svr_serial_transport.c ./rtiostream_serial_interface.c ./rtiostream_utils.c
cc1plus.exe: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
cc1plus.exe: warning: command line option "-std=gnu99" is valid for C/ObjC but not for C++
In file included from ./MPC4725_S_Function_wrapper.cpp:18:
D:/DACsFuncSimulink/twi.h: In member function 'uint8_t TwoWire::requestFrom(uint8_t, uint8_t, uint8_t)':
D:/DACsFuncSimulink/twi.h:47: error: too many arguments to function 'uint8_t twi_readFrom(uint8_t, uint8_t*, uint8_t)'
D:/DACsFuncSimulink/Wire.cpp:85: error: at this point in file
D:/DACsFuncSimulink/twi.h: In member function 'uint8_t TwoWire::endTransmission(uint8_t)':
D:/DACsFuncSimulink/twi.h:48: error: too many arguments to function 'uint8_t twi_writeTo(uint8_t, uint8_t*, uint8_t, uint8_t)'
D:/DACsFuncSimulink/Wire.cpp:140: error: at this point in file
D:/DACsFuncSimulink/Wire.cpp: In member function 'virtual size_t TwoWire::write(const uint8_t*, size_t)':
D:/DACsFuncSimulink/Wire.cpp:195: error: invalid conversion from 'const uint8_t*' to 'uint8_t*'
D:/DACsFuncSimulink/Wire.cpp:195: error: initializing argument 1 of 'uint8_t twi_transmit(uint8_t*, uint8_t)'
In file included from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_blocks_abstraction.h:8,
from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_digitaloutput_lct.h:11,
from ./MPC4725_toggle.h:31,
from ./MPC4725_toggle.c:17:
D:/MATLABSupportPack/arduino-1.0.5/hardware/arduino/cores/arduino/Arduino.h:25:1: warning: "true" redefined
In file included from ./MPC4725_toggle.h:24,
from ./MPC4725_toggle.c:17:
./rtwtypes.h:27:1: warning: this is the location of the previous definition
In file included from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_blocks_abstraction.h:8,
from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_digitaloutput_lct.h:11,
from ./MPC4725_toggle.h:31,
from ./MPC4725_toggle.c:17:
D:/MATLABSupportPack/arduino-1.0.5/hardware/arduino/cores/arduino/Arduino.h:26:1: warning: "false" redefined
In file included from ./MPC4725_toggle.h:24,
from ./MPC4725_toggle.c:17:
./rtwtypes.h:23:1: warning: this is the location of the previous definition
In file included from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_blocks_abstraction.h:8,
from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_digitaloutput_lct.h:11,
from ./MPC4725_toggle.h:31,
from ./MPC4725_toggle_data.c:17:
D:/MATLABSupportPack/arduino-1.0.5/hardware/arduino/cores/arduino/Arduino.h:25:1: warning: "true" redefined
In file included from ./MPC4725_toggle.h:24,
from ./MPC4725_toggle_data.c:17:
./rtwtypes.h:27:1: warning: this is the location of the previous definition
In file included from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_blocks_abstraction.h:8,
from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_digitaloutput_lct.h:11,
from ./MPC4725_toggle.h:31,
from ./MPC4725_toggle_data.c:17:
D:/MATLABSupportPack/arduino-1.0.5/hardware/arduino/cores/arduino/Arduino.h:26:1: warning: "false" redefined
In file included from ./MPC4725_toggle.h:24,
from ./MPC4725_toggle_data.c:17:
./rtwtypes.h:23:1: warning: this is the location of the previous definition
In file included from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_blocks_abstraction.h:8,
from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_digitaloutput_lct.h:11,
from ./MPC4725_toggle.h:31,
from ./ert_main.c:17:
D:/MATLABSupportPack/arduino-1.0.5/hardware/arduino/cores/arduino/Arduino.h:25:1: warning: "true" redefined
In file included from ./MPC4725_toggle.h:24,
from ./ert_main.c:17:
./rtwtypes.h:27:1: warning: this is the location of the previous definition
In file included from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_blocks_abstraction.h:8,
from D:/MATLABSupportPack/arduino/blocks/sfcn/include/arduino_digitaloutput_lct.h:11,
from ./MPC4725_toggle.h:31,
from ./ert_main.c:17:
D:/MATLABSupportPack/arduino-1.0.5/hardware/arduino/cores/arduino/Arduino.h:26:1: warning: "false" redefined
In file included from ./MPC4725_toggle.h:24,
from ./ert_main.c:17:
./rtwtypes.h:23:1: warning: this is the location of the previous definition
cc1plus.exe: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
cc1plus.exe: warning: command line option "-std=gnu99" is valid for C/ObjC but not for C++
./HardwareSerial.cpp: In function 'void store_char(unsigned char, ring_buffer*)':
./HardwareSerial.cpp:98: warning: comparison between signed and unsigned integer expressions
./HardwareSerial.cpp: In function 'void __vector_25()':
./HardwareSerial.cpp:127: warning: unused variable 'c'
./HardwareSerial.cpp: In function 'void __vector_36()':
./HardwareSerial.cpp:153: warning: unused variable 'c'
./HardwareSerial.cpp: In function 'void __vector_51()':
./HardwareSerial.cpp:168: warning: unused variable 'c'
./HardwareSerial.cpp: In function 'void __vector_54()':
./HardwareSerial.cpp:183: warning: unused variable 'c'
./HardwareSerial.cpp: In member function 'void HardwareSerial::begin(long unsigned int, byte)':
./HardwareSerial.cpp:368: warning: unused variable 'current_config'
./HardwareSerial.cpp: In member function 'virtual size_t HardwareSerial::write(uint8_t)':
./HardwareSerial.cpp:467: warning: comparison between signed and unsigned integer expressions
cc1plus.exe: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
cc1plus.exe: warning: command line option "-std=gnu99" is valid for C/ObjC but not for C++
cc1plus.exe: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
cc1plus.exe: warning: command line option "-std=gnu99" is valid for C/ObjC but not for C++
./Print.cpp: In member function 'size_t Print::print(const __FlashStringHelper*)':
./Print.cpp:44: warning: '__progmem__' attribute ignored
cc1plus.exe: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
cc1plus.exe: warning: command line option "-std=gnu99" is valid for C/ObjC but not for C++
cc1plus.exe: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
cc1plus.exe: warning: command line option "-std=gnu99" is valid for C/ObjC but not for C++
cc1plus.exe: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
cc1plus.exe: warning: command line option "-std=gnu99" is valid for C/ObjC but not for C++
cc1plus.exe: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
cc1plus.exe: warning: command line option "-std=gnu99" is valid for C/ObjC but not for C++
./io_wrappers.cpp: In function 'void Serial_read(int, int, uint8_t*, int16_t*)':
./io_wrappers.cpp:59: warning: 'libFcnOutput' may be used uninitialized in this function
cc1plus.exe: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
cc1plus.exe: warning: command line option "-std=gnu99" is valid for C/ObjC but not for C++
./rtiostream_serial.cpp: In function 'int rtIOStreamOpen(int, void**)':
./rtiostream_serial.cpp:28: warning: unused variable 'init_complete'
./ext_work.c:248: warning: 'displayUsage' defined but not used
make: *** [MPC4725_S_Function_wrapper.o] Error 1
If you know where I am going wrong, please correct me. Will be very grateful to you. Thank you!

Iniciar sesión para comentar.

Más respuestas (2)

Mirko
Mirko el 25 de Mayo de 2014
Hello, I'm trying to make a connection between raspberry I2C (Master) and Arduino (slave). I edited this S-Function, I can read data from the slave, but I do not know how to write to the slave.
LIBRARIAS
# ifndef MATLAB_MEX_FILE
// must run command:
// gpio load i2c
// on RPI to start I2C
// path to files on the local RPi:
#include </home/pi/wiringPi/wiringPi/wiringPiI2C.h>
#include </home/pi/wiringPi/wiringPi/wiringPiI2C.c>
#include </home/pi/wiringPi/wiringPi/wiringPi.c>
#include </home/pi/wiringPi/wiringPi/piHiPri.c>
//----> MPU6050 Info: <-----
// setup defines: Register definitions
#define Ardunio_RG 0x6B
// Device identification:
//#define MPU6050_RA_WHO_AM_I 0x75
// Arduino Read registers:
#define Arduino_XOUT_H 0x43
#define Arduino_XOUT_L 0x44
// global variables for address, high and low bits for data
int fd, gxh, gxl;
#endif
OUTPUT
if(xD[0] == 1)
{
#ifndef MATLAB_MEX_FILE
// Read high byte, low byte, then combine:
gxh = wiringPiI2CReadReg8(fd,Arduino_XOUT_H) ;
gxl = wiringPiI2CReadReg8(fd,Arduino_XOUT_L) ;
y0[0] = ((gxh<<8)|gxl);;
#endif
}
DRISCRETE UPDATE
if(xD[0] != 1){
# ifndef MATLAB_MEX_FILE
// Controllo se Arduino è connesso
fd = wiringPiI2CSetup(0x36); // 0x36 indirizzo Arduino Uno
wiringPiI2CWriteReg8(fd, Ardunio_RG , 0b00000010);
#endif
//done with initialization
xD[0] = 1;
}
I would like to create an S-Function that performs the writing on the slave,have you something to suggest? Thanks

mohammad morad
mohammad morad el 13 de Abr. de 2016
Editada: mohammad morad el 13 de Abr. de 2016
Dear all I am trying to run Arduino code in S-Function Builder . I have the Arduino support package and also included the library files in the same folder as the s function.but when I build my s-function I receive the following error: test2_wrapper.c C:\Users\Novin Pendar\Desktop\sfunction\sht10\SHT1x 2\test2_wrapper.c(15) : error C2143: syntax error : missing '{' before 'constant' C:\Users\Novin Pendar\Desktop\sfunction\sht10\SHT1x 2\test2_wrapper.c(15) : error C2059: syntax error : '<Unknown>' .......................
I attach my Arduino and Simulink file in this question
please help me
Your answers can be a great help to me
thanks a lot!
  1 comentario
lin jie
lin jie el 19 de Abr. de 2016
我想知道,如何加载用这些模块,用了,总是编译通不过。请指导

Iniciar sesión para comentar.

Categorías

Más información sobre Applications en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by