S-Function to code Interrupts in Mega2560 fails for unknown reasons. Help requested.
Show older comments
Running S-Function to incorporate Arduino interrupts into Simulink for encoders. But the program errors out. It works fine at driving the motors but fails at getting encoder data when the S-Function is put in.
%%S-Function Parameters%%:
enc uint8 0
pinA uint 8 18
pinB uint8 19
%%Libraries%%:
# ifndef MATLAB_MEX_FILE
# include <Arduino.h>
typedef struct { int pinA; int pinB; int pos; int del;} Encoder;
volatile Encoder Enc[1] = {0,0,0,0};
/* auxiliary function to handle encoder attachment */
int getIntNum(int pin) {
/* returns the interrupt number for a given interrupt pin
see http://arduino.cc/it/Reference/AttachInterrupt */
switch(pin) {
case 2:
return 0;
case 3:
return 1;
case 21:
return 2;
case 20:
return 3;
case 19:
return 4;
case 18:
return 5;
default:
return -1;
}
}
/* Interrupt Service Routine: change on pin A for Encoder 0 */
void irsPinAEn0(){
Enc[0].pos++; /* going clockwise: increment */
} /* end ISR pin A Encoder 0 */
/* Interrupt Service Routine: change on pin B for Encoder 0 */
void isrPinBEn0(){
Enc[0].pos--; /* going counterclockwise: decrement */
}
/* Interrupt Service Routine: change on pin A for Encoder 1 */
void irsPinAEn1(){
Enc[1].pos++;
} /* end ISR pin A Encoder 1 */
/* Interrupt Service Routine: change on pin B for Encoder 1 */
void isrPinBEn1(){
Enc[1].pos--; */
} /* end ISR pin B Encoder 1 */
# endif
%%Outputs:%%
/* wait until after initialization is done */
if (xD[0]==1) {
/* don't do anything for mex file generation */
# ifndef MATLAB_MEX_FILE
/* get encoder position and set is as output */
pos[0]=Enc[enc[0]].pos;
# endif
}
%%Discrete Update%%:
if (xD[0]!=1) {
/* don't do anything for MEX-file generation */
# ifndef MATLAB_MEX_FILE
/* enc[0] is the encoder number and it can be 0,1 or 2 */
/* if other encoder blocks are present in the model */
/* up to a maximum of 3, they need to refer to a */
/* different encoder number */
/* store pinA and pinB in global encoder structure Enc */
/* they will be needed later by the interrupt routine */
/* that will not be able to access s-function parameters */
Enc[enc[0]].pinA=pinA[0]; /* set pin A */
Enc[enc[0]].pinB=pinB[0]; /* set pin B */
/* set encoder pins as inputs */
/*pinMode(Enc[enc[0]].pinA, INPUT);
pinMode(Enc[enc[0]].pinB, INPUT); */
/* turn on pullup resistors */
/*digitalWrite(Enc[enc[0]].pinA, HIGH);
digitalWrite(Enc[enc[0]].pinB, HIGH); */
/* attach interrupts */
switch(enc[0]) {
case 0:
attachInterrupt(getIntNum(Enc[0].pinA), irsPinAEn0, RISING);
attachInterrupt(getIntNum(Enc[0].pinB), isrPinBEn0, RISING);
break;
case 1:
attachInterrupt(getIntNum(Enc[1].pinA), irsPinAEn1, RISING);
attachInterrupt(getIntNum(Enc[1].pinB), isrPinBEn1, RISING);
break;
}
# endif
/* initialization done */
xD[0]=1;
}
%%ERRORMESSAGE%%:
The call to realtime_make_rtw_hook, during the after_make hook generated the following error:
The build failed with the following message: "C:/MATLAB/SupportPackages/R2015a/arduino-1.0.5/hardware/tools/avr/bin/avr-gcc" -I"C:/MATLAB/SupportPackages/R2015a/arduinobase/toolbox/realtime/targets/arduino/include" -I"C:/MATLAB/SupportPackages/R2015a/arduinobase/toolbox/realtime/targets/arduino/blocks/sfcn/include" -I"C:/Users/Zeerek/SkyDrive/School Important/.Spring 2015/ME 107/final/Arduino/motorTest_rtt" -I"C:/Users/Zeerek/SkyDrive/School Important/.Spring 2015/ME 107/final/Arduino" -I"C:/Program Files/MATLAB/R2015a/extern/include" -I"C:/Program Files/MATLAB/R2015a/simulink/include" -I"C:/Program Files/MATLAB/R2015a/rtw/c/src" -I"C:/Program Files/MATLAB/R2015a/rtw/c/src/ext_mode/common" -I"C:/Program Files/MATLAB/R2015a/rtw/c/ert" -I"C:/Program Files/MATLAB/R2015a/toolbox/coder/rtiostream/src/utils" -I"C:/MATLAB/SupportPackages/R2015a/arduino-1.0.5/hardware/arduino/cores/arduino" -I"C:/MATLAB/SupportPackages/R2015a/arduino-1.0.5/hardware/arduino/variants/mega" -I"C:/MATLAB/SupportPackages/R2015a/arduino-1.0.5/libraries/Servo" -I"C:/Program Files/MATLAB/R2015a/rtw/c/src/ext_mode/serial" -I"C:/MATLAB/SupportPackages/R2015a/arduino/toolbox/realtime/targets/arduinomega2560/src" -mmcu=atmega2560 -ffunction-sections -fdata-sections -std=gnu99 -Wall -Wstrict-prototypes -g -Os -D"MODEL=motorTest" -D"NUMST=2" -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=1" -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 ./HardwareSerial.cpp ./IPAddress.cpp ./Print.cpp ./WInterrupts.c ./WMath.cpp ./WString.cpp ./new.cpp ./wiring.c ./wiring_analog.c ./wiring_digital.c ./rtiostream_serial.cpp ./io_wrappers.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 ./ert_main.c ./motorTest.c ./motorTest_data.c ./sfcn_encoder_wrapper.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++
./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++
./rtiostream_serial.cpp: In function 'int rtIOStreamOpen(int, void**)':
./rtiostream_serial.cpp:28: warning: unused variable 'init_complete'
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:70: warning: 'libFcnOutput' may be used uninitialized in this function
./ext_work.c:259: warning: 'displayUsage' defined but not used
In file included from C:/MATLAB/SupportPackages/R2015a/arduinobase/toolbox/realtime/targets/arduino/blocks/sfcn/include/arduino_blocks_abstraction.h:8,
from C:/MATLAB/SupportPackages/R2015a/arduinobase/toolbox/realtime/targets/arduino/blocks/sfcn/include/arduino_analogoutput_lct.h:11,
from ./motorTest.h:36,
from ./ert_main.c:21:
C:/MATLAB/SupportPackages/R2015a/arduino-1.0.5/hardware/arduino/cores/arduino/Arduino.h:25:1: warning: "true" redefined
In file included from ./motorTest.h:29,
from ./ert_main.c:21:
./rtwtypes.h:31:1: warning: this is the location of the previous definition
In file included from C:/MATLAB/SupportPackages/R2015a/arduinobase/toolbox/realtime/targets/arduino/blocks/sfcn/include/arduino_blocks_abstraction.h:8,
from C:/MATLAB/SupportPackages/R2015a/arduinobase/toolbox/realtime/targets/arduino/blocks/sfcn/include/arduino_analogoutput_lct.h:11,
from ./motorTest.h:36,
from ./ert_main.c:21:
C:/MATLAB/SupportPackages/R2015a/arduino-1.0.5/hardware/arduino/cores/arduino/Arduino.h:26:1: warning: "false" redefined
In file included from ./motorTest.h:29,
from ./ert_main.c:21:
./rtwtypes.h:27:1: warning: this is the location of the previous definition
./ert_main.c: In function '__vector_50':
./ert_main.c:47: warning: implicit declaration of function 'rt_OneStep'
./ert_main.c: At top level:
./ert_main.c:54: warning: function declaration isn't a prototype
./ert_main.c:71: warning: conflicting types for 'rt_OneStep'
./ert_main.c:47: warning: previous implicit declaration of 'rt_OneStep' was here
./ert_main.c: In function 'rt_OneStep':
./ert_main.c:107: warning: 'eventFlags[1]' may be used uninitialized in this function
In file included from C:/MATLAB/SupportPackages/R2015a/arduinobase/toolbox/realtime/targets/arduino/blocks/sfcn/include/arduino_blocks_abstraction.h:8,
from C:/MATLAB/SupportPackages/R2015a/arduinobase/toolbox/realtime/targets/arduino/blocks/sfcn/include/arduino_analogoutput_lct.h:11,
from ./motorTest.h:36,
from ./motorTest.c:21:
C:/MATLAB/SupportPackages/R2015a/arduino-1.0.5/hardware/arduino/cores/arduino/Arduino.h:25:1: warning: "true" redefined
In file included from ./motorTest.h:29,
from ./motorTest.c:21:
./rtwtypes.h:31:1: warning: this is the location of the previous definition
In file included from C:/MATLAB/SupportPackages/R2015a/arduinobase/toolbox/realtime/targets/arduino/blocks/sfcn/include/arduino_blocks_abstraction.h:8,
from C:/MATLAB/SupportPackages/R2015a/arduinobase/toolbox/realtime/targets/arduino/blocks/sfcn/include/arduino_analogoutput_lct.h:11,
from ./motorTest.h:36,
from ./motorTest.c:21:
C:/MATLAB/SupportPackages/R2015a/arduino-1.0.5/hardware/arduino/cores/arduino/Arduino.h:26:1: warning: "false" redefined
In file included from ./motorTest.h:29,
from ./motorTest.c:21:
./rtwtypes.h:27:1: warning: this is the location of the previous definition
In file included from C:/MATLAB/SupportPackages/R2015a/arduinobase/toolbox/realtime/targets/arduino/blocks/sfcn/include/arduino_blocks_abstraction.h:8,
from C:/MATLAB/SupportPackages/R2015a/arduinobase/toolbox/realtime/targets/arduino/blocks/sfcn/include/arduino_analogoutput_lct.h:11,
from ./motorTest.h:36,
from ./motorTest_data.c:21:
C:/MATLAB/SupportPackages/R2015a/arduino-1.0.5/hardware/arduino/cores/arduino/Arduino.h:25:1: warning: "true" redefined
In file included from ./motorTest.h:29,
from ./motorTest_data.c:21:
./rtwtypes.h:31:1: warning: this is the location of the previous definition
In file included from C:/MATLAB/SupportPackages/R2015a/arduinobase/toolbox/realtime/targets/arduino/blocks/sfcn/include/arduino_blocks_abstraction.h:8,
from C:/MATLAB/SupportPackages/R2015a/arduinobase/toolbox/realtime/targets/arduino/blocks/sfcn/include/arduino_analogoutput_lct.h:11,
from ./motorTest.h:36,
from ./motorTest_data.c:21:
C:/MATLAB/SupportPackages/R2015a/arduino-1.0.5/hardware/arduino/cores/arduino/Arduino.h:26:1: warning: "false" redefined
In file included from ./motorTest.h:29,
from ./motorTest_data.c:21:
./rtwtypes.h:27:1: warning: this is the location of the previous definition
In file included from ./sfcn_encoder_wrapper.c:17:
C:/MATLAB/SupportPackages/R2015a/arduino-1.0.5/hardware/arduino/cores/arduino/Arduino.h:25:1: warning: "true" redefined
In file included from ./sfcn_encoder_wrapper.c:11:
./rtwtypes.h:31:1: warning: this is the location of the previous definition
In file included from ./sfcn_encoder_wrapper.c:17:
C:/MATLAB/SupportPackages/R2015a/arduino-1.0.5/hardware/arduino/cores/arduino/Arduino.h:26:1: warning: "false" redefined
In file included from ./sfcn_encoder_wrapper.c:11:
./rtwtypes.h:27:1: warning: this is the location of the previous definition
./sfcn_encoder_wrapper.c:20: warning: missing braces around initializer
./sfcn_encoder_wrapper.c:20: warning: (near initialization for 'Enc[0]')
./sfcn_encoder_wrapper.c:45: warning: function declaration isn't a prototype
./sfcn_encoder_wrapper.c:52: warning: function declaration isn't a prototype
./sfcn_encoder_wrapper.c:57: warning: function declaration isn't a prototype
./sfcn_encoder_wrapper.c:63: warning: function declaration isn't a prototype
./sfcn_encoder_wrapper.c: In function 'isrPinBEn1':
./sfcn_encoder_wrapper.c:64: error: expected expression before '/' token
make: *** [HardwareSerial.o] Error 1
Answers (2)
Zeerek Ahmad
on 15 Apr 2015
1 Comment
Anubhab Pal
on 4 Dec 2016
I am trying to do the same for few days. Can you please share the step by step procedure?
mrydnz
on 28 Apr 2017
0 votes
volatile Encoder Enc[1] = {0,0,0,0}; You need to initialize for 'Enc[0]'
Categories
Find more on Simulink Supported 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!