summaryrefslogtreecommitdiff
path: root/Modelica-Arduino-MDD_Arduino_Revised/Resources/src
diff options
context:
space:
mode:
authorSiddharth112352019-09-03 18:09:16 +0530
committerSiddharth112352019-09-03 18:09:16 +0530
commitb4b6aa36e3486a3544acc52419149b5671f841e9 (patch)
tree66c1783158f23e6d21c77324156fc57e18d4ac67 /Modelica-Arduino-MDD_Arduino_Revised/Resources/src
parentf5266f634f4fb4fd39933a83551a01cf446256b8 (diff)
downloadOpenModelica_HIL-master.tar.gz
OpenModelica_HIL-master.tar.bz2
OpenModelica_HIL-master.zip
Pushing entire Modelica HIL Tasks repoHEADmaster
Diffstat (limited to 'Modelica-Arduino-MDD_Arduino_Revised/Resources/src')
-rwxr-xr-xModelica-Arduino-MDD_Arduino_Revised/Resources/src/cmd_analog.c61
-rwxr-xr-xModelica-Arduino-MDD_Arduino_Revised/Resources/src/cmd_analog_volt.c63
-rwxr-xr-xModelica-Arduino-MDD_Arduino_Revised/Resources/src/cmd_digital.c63
-rwxr-xr-xModelica-Arduino-MDD_Arduino_Revised/Resources/src/cmd_encoder_init.c39
-rwxr-xr-xModelica-Arduino-MDD_Arduino_Revised/Resources/src/dcmotor.c117
-rwxr-xr-xModelica-Arduino-MDD_Arduino_Revised/Resources/src/delay.c11
-rwxr-xr-xModelica-Arduino-MDD_Arduino_Revised/Resources/src/ieeesingle2num.c87
-rwxr-xr-xModelica-Arduino-MDD_Arduino_Revised/Resources/src/mfloor.c6
-rwxr-xr-xModelica-Arduino-MDD_Arduino_Revised/Resources/src/modbus.c449
-rwxr-xr-xModelica-Arduino-MDD_Arduino_Revised/Resources/src/nointerrupts.c15
-rwxr-xr-xModelica-Arduino-MDD_Arduino_Revised/Resources/src/serialComm.c156
-rwxr-xr-xModelica-Arduino-MDD_Arduino_Revised/Resources/src/servomotor.c87
-rwxr-xr-xModelica-Arduino-MDD_Arduino_Revised/Resources/src/wakeup.c16
13 files changed, 1170 insertions, 0 deletions
diff --git a/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/cmd_analog.c b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/cmd_analog.c
new file mode 100755
index 0000000..5a8cf2f
--- /dev/null
+++ b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/cmd_analog.c
@@ -0,0 +1,61 @@
+#include <errno.h>
+#include <termios.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h> /* memset */
+#include <stdio.h>
+#include <math.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include "../Include/serial.h"
+#include "../Include/analog.h"
+
+uint16_t cmd_analog_in(int h,int pin_no)
+{
+ char pin[5],v1[2];
+ int stat;
+ int read[3];
+ sprintf(v1,"%c",pin_no+48);
+ strcpy(pin,"A");
+ strcat(pin,v1);
+ int wr=write_serial(h,pin,2);
+ stat=status_serial(h,read);
+ //printf("Read:%d\n",read[0] );
+ while (read[0] < 2)
+ stat=status_serial(h,read);
+ char values[5];
+ int a_rd=read_serial(h,values,2);
+ values[2]='\0';
+ printf("%s\n",values);
+ int l=strlen(values);
+ //printf("%d\n",l );
+ uint8_t temp[l+1];
+ int i;
+ for (i = 0; i < l; ++i)
+ {
+ temp[i]=(uint8_t)(values[i]);
+ }
+ //printf("%u %u\n",temp[0],temp[1]);
+ uint16_t result=(uint16_t)(256*temp[1]+temp[0]);
+ //printf("%u\n",result);
+ return result;
+}
+
+int cmd_analog_out(int h,int pin_no,double val)
+{
+ char v1[2],v2[2];
+ if(val > 255)
+ val = 255;
+ else if(val < 0)
+ val = 0;
+ //printf("%f\n",val);
+ char code_sent[10];
+ strcpy(code_sent,"W");
+ sprintf(v1,"%c",pin_no+48);
+ sprintf(v2,"%c",abs(ceil(val)));
+ strcat(code_sent,v1);
+ strcat(code_sent,v2);
+ //printf("%s\n",code_sent);
+ return write_serial(h,code_sent,3);
+
+} \ No newline at end of file
diff --git a/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/cmd_analog_volt.c b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/cmd_analog_volt.c
new file mode 100755
index 0000000..701c0c0
--- /dev/null
+++ b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/cmd_analog_volt.c
@@ -0,0 +1,63 @@
+#include <errno.h>
+#include <termios.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h> /* memset */
+#include <stdio.h>
+#include <math.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include "../Include/serial.h"
+#include "../Include/analogv.h"
+
+uint16_t cmd_analog_in_volt(int h,int pin_no)
+{
+ char pin[5]="A",v1[2];
+ int stat;
+ int read[3];
+ sprintf(v1,"%c",pin_no+48);
+ //strcpy(pin,"A");
+ strcat(pin,v1);
+ int wr=write_serial(h,pin,2);
+ stat=status_serial(h,read);
+ //printf("Read:%d\n",read[0] );
+ while (read[0] < 2)
+ stat=status_serial(h,read);
+ char values[5];
+ int a_rd=read_serial(h,values,2);
+ values[2]='\0';
+ printf("%s\n",values);
+ int l=strlen(values);
+ //printf("%d\n",l );
+ uint8_t temp[l+1];
+ int i;
+ for (i = 0; i < l; ++i)
+ {
+ temp[i]=(uint8_t)(values[i]);
+ }
+ //printf("%u %u\n",temp[0],temp[1]);
+ uint16_t result=(uint16_t)(256*temp[1]+temp[0]);
+ result/=1023;
+ //printf("%u\n",result);
+ return result;
+}
+
+int cmd_analog_out_volt(int h,int pin_no,double val)
+{
+ char v1[2],v2[2];
+ if(val > 5)
+ val = 5;
+ else if(val < 0)
+ val = 0;
+ val=val*255/5;
+ //printf("%f\n",val);
+ char code_sent[10];
+ strcpy(code_sent,"W");
+ sprintf(v1,"%c",pin_no+48);
+ sprintf(v2,"%c",abs(ceil(val)));
+ strcat(code_sent,v1);
+ strcat(code_sent,v2);
+ //printf("%s\n",code_sent);
+ return write_serial(h,code_sent,3);
+
+} \ No newline at end of file
diff --git a/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/cmd_digital.c b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/cmd_digital.c
new file mode 100755
index 0000000..f19d23b
--- /dev/null
+++ b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/cmd_digital.c
@@ -0,0 +1,63 @@
+#include <errno.h>
+#include <termios.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h> /* memset */
+#include <stdio.h>
+#include <stdlib.h>
+#include "../Include/serial.h"
+#include "../Include/digital.h"
+
+int cmd_digital_out(int h,int pin_no,int val)
+{
+ int wr;
+ char pin[6]="Da";
+ char v[2],temp[2];
+ sprintf(temp,"%c",pin_no+48);
+ strcat(pin,temp);
+ strcat(pin,"1");
+ //printf("%s",pin);
+ wr=write_serial(h,pin,4);
+ if (val > 0.5)
+ val = 1;
+ else
+ val = 0;
+
+ sprintf(v,"%d",val);
+ strcpy(pin,"Dw");
+ strcat(pin,temp);
+ strcat(pin,v);
+ //printf("%s",pin);
+ wr=write_serial(h,pin,4);
+ return wr;
+}
+
+int cmd_digital_in(int h,int pin_no)
+{
+ int value = 0;
+ char pin[6]="Da";
+ char v1[2],v2[2];
+ int wr1, wr2;
+ sprintf(v1,"%c",pin_no+48);
+ strcat(pin,v1);
+ strcat(pin,"0");
+ //printf("%s\n",pin);
+ wr1=write_serial(h,pin,4);
+
+ strcpy(pin,"Dr");
+ sprintf(v2,"%c",pin_no+48);
+ strcat(pin,v2);
+ wr2=write_serial(1,pin,3);
+ //binary transfer
+ int stat;
+ int num_bytes[2];
+ char st[10];
+ stat=status_serial(h,num_bytes);
+ while(num_bytes[0]<1)
+ stat=status_serial(h,num_bytes);
+ char* temp;
+ int wr=read_serial(h,st,1);
+ value=strtod(st,&temp);
+ //printf("%d\n",value);
+ return value;
+}
diff --git a/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/cmd_encoder_init.c b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/cmd_encoder_init.c
new file mode 100755
index 0000000..89a53dd
--- /dev/null
+++ b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/cmd_encoder_init.c
@@ -0,0 +1,39 @@
+#include <errno.h>
+#include <string.h>
+#include <stdio.h>
+#include <math.h>
+#include <stdlib.h>
+#include "../Include/encoder.h"
+#include "../Include/serial.h"
+
+void cmd_encoder_init(int h,int enc_mode,int pin_no_1,int pin_no_2)
+{
+
+ int wr;
+ char code_sent[10]="Ea";
+ char c[2];
+
+ sprintf(c,"%c",48+corresp[block->rpar[2]]);
+ strcat(code_sent,c);
+ if(block.rpar(2)==4)
+ {
+ char c1[2],c2[2],v[10];
+ sprintf(c1,"%c",48+0+corresp[block->rpar[3]]);
+ sprintf(v,"%s",(block->rpar[1]));
+ strcat(code_sent,c1);
+ strcat(code_sent,v);
+ }
+ else
+ {
+ char c2[2],v[10];
+ sprintf(c2,"%c",48+0+corresp[block->rpar[3]]);
+ sprintf(v,"%s",block->rpar[1]);
+ strcat(code_sent,c1);
+ strcat(code_sent,v);
+ }
+ wr=write_serial(1,code_sent,5);
+ strcpy(code_sent,"Ez");
+ sprintf(c,"%c",corresp[block->rpar[2]])
+ strcat(code_sent,c);
+ wr=write_serial(1,code_sent,3)
+} \ No newline at end of file
diff --git a/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/dcmotor.c b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/dcmotor.c
new file mode 100755
index 0000000..ac6775c
--- /dev/null
+++ b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/dcmotor.c
@@ -0,0 +1,117 @@
+#include <errno.h>
+#include <termios.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdio.h>
+#include <math.h>
+#include "../Include/serial.h"
+#include "../Include/dcmotor.h"
+
+#define abs(x) (x>=0?x:-x)
+
+void cmd_dcmotor_setup(int h,int driver_type,int motor_no,int pin_no_1,int pin_no_2)
+{
+ printf("init DCmotor\n");
+ char code_sent[10]="C";
+ int wr;
+ char v1[2],v2[2],motor[2];
+ if(driver_type==1)
+ {
+ sprintf(v1,"%c",pin_no_1+48);
+ sprintf(v2,"%c",pin_no_2+48);
+ sprintf(motor,"%c",motor_no+48);
+ strcat(code_sent,motor);
+ strcat(code_sent,v1);
+ strcat(code_sent,v2);
+ strcat(code_sent,"1");
+ }
+ //code_sent="C"+string(motor_no)+ascii(48+pin_no_1)+ascii(48+pin_no_2)+"1"; //adafruit
+ else if(driver_type==2)
+ {
+ sprintf(v1,"%c",pin_no_1+48);
+ sprintf(v2,"%c",pin_no_2+48);
+ sprintf(motor,"%c",motor_no+48);
+ strcat(code_sent,motor);
+ strcat(code_sent,v1);
+ strcat(code_sent,v2);
+ strcat(code_sent,"1");
+ }
+ //code_sent="C"+string(motor_no)+ascii(48+pin_no_1)+ascii(48+pin_no_2)+"1"; //code pour initialiser L298
+ else if(driver_type==3)
+ {
+ sprintf(v1,"%c",pin_no_1+48);
+ sprintf(v2,"%c",pin_no_2+48);
+ sprintf(motor,"%c",motor_no+48);
+ strcat(code_sent,motor);
+ strcat(code_sent,v1);
+ strcat(code_sent,v2);
+ strcat(code_sent,"0");
+
+ }
+ // code_sent="C"+string(motor_no)+ascii(48+pin_no_1)+ascii(48+pin_no_2)+"0"; //code pour initialiser L293
+ //printf("%s\n",code_sent);
+ wr=write_serial(h,code_sent,5);
+
+ int stat;
+ int num_bytes[2];
+ //Attente que l'arduino reponde OK
+ stat=status_serial(1,num_bytes);
+ while (num_bytes[0] < 2)
+ stat=status_serial(1,num_bytes);
+ char values[5];
+ int dcm_rd=read_serial(1,values,2);
+ if (dcm_rd == 0)
+ printf("Init motor successful\n");
+ else
+ printf("Init motor unsuccessful\n");
+}
+
+void cmd_dcmotor_run(int h,int motor_no,int u1)
+{
+ char code_dir[2];
+ char code_sent[10]="M";
+ char motor[2];
+ int val;
+ char v[2];
+ if(u1>=0)
+ sprintf(code_dir,"%c",49);
+ //code_dir=ascii(49);
+ else
+ sprintf(code_dir,"%c",48);
+ //code_dir=ascii(48);
+ if(abs(u1)>255)
+ val=255;
+ else
+ val=abs(ceil(u1));
+ //printf("%d\n",val);
+ sprintf(motor,"%c",motor_no+48);
+ sprintf(v,"%c",val);
+ strcat(code_sent,motor);
+ strcat(code_sent,code_dir);
+ strcat(code_sent,v);
+ //printf("%s\n",code_sent);
+ //code_sent="M"+ascii(48+motor_no)+code_dir+ascii(val);
+ int wr=write_serial(h,code_sent,4);
+}
+
+void cmd_dcmotor_release(int h,int motor_no)
+{
+ char code_sent[6]="M";
+ char motor[2];
+ char v[2];
+ sprintf(motor,"%c",motor_no+48);
+ sprintf(v,"%c",0);
+ strcat(code_sent,motor);
+ strcat(code_sent,"1");
+ strcat(code_sent,v);
+ //code_sent="M"+ascii(48+motor_no)+"1"+ascii(0);
+ //printf("%s\n",code_sent);
+ int wr=write_serial(h,code_sent,4);
+ strcpy(code_sent,"M");
+ strcat(code_sent,motor);
+ strcat(code_sent,"r");
+ //printf("%s\n",code_sent);
+ //code_sent="M"+ascii(48+motor_no)+"r";
+ wr=write_serial(h,code_sent,3);
+}
diff --git a/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/delay.c b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/delay.c
new file mode 100755
index 0000000..db9c175
--- /dev/null
+++ b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/delay.c
@@ -0,0 +1,11 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <math.h>
+#include <unistd.h>
+
+void delay(long int t)
+{
+ usleep(abs(t*1000));
+}
+
diff --git a/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/ieeesingle2num.c b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/ieeesingle2num.c
new file mode 100755
index 0000000..7331193
--- /dev/null
+++ b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/ieeesingle2num.c
@@ -0,0 +1,87 @@
+#include <errno.h>
+#include <termios.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include <ctype.h>
+#include "../Include/serial.h"
+
+#ifdef NAN
+/* NAN is supported */
+#endif
+#ifdef INFINITY
+/* INFINITY is supported */
+#endif
+
+
+long int hex2dec(char hexadecimal[])
+{
+ long int decimalNumber=0;
+ char hexDigits[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
+ '9', 'A', 'B', 'C', 'D', 'E', 'F'};
+ int i, j, power=0;
+
+ /* Converting hexadecimal number to decimal number */
+ for(i=strlen(hexadecimal)-1; i >= 0; i--) {
+ /*search currect character in hexDigits array */
+ if(isalpha(hexadecimal[i]))
+ hexadecimal[i]=toupper(hexadecimal[i]);
+ for(j=0; j<16; j++){
+ if(hexadecimal[i] == hexDigits[j]){
+ decimalNumber += j*pow(16, power);
+ }
+ // }
+ power++;
+ }
+ }
+ return decimalNumber;
+}
+
+void dec2hex(long int decimalnum,char hexadecimalnum[])
+{
+ long quotient, remainder;
+ int j = 1;
+ quotient = decimalnum;
+
+ while (quotient != 0)
+ {
+ remainder = quotient % 16;
+ if (remainder < 10)
+ hexadecimalnum[j--] = 48 + remainder;
+ else
+ hexadecimalnum[j--] = 55 + remainder;
+ quotient = quotient / 16;
+ }
+ hexadecimalnum[2]='\0';
+}
+
+double ieeesingle2num(char hexa[])
+{
+ long int x=hex2dec(hexa);
+ long int k =(long int)pow(2,31); //pow2(31);
+ int s = (x >= k);
+ if (s)
+ x = x - k;
+ //t = pow2(x,-23);
+ double t = x*pow(2,-23);
+ long int e = floor(t);
+ double f = t - e;
+ double y;
+ if (e == 255)
+ if(f == 0)
+ y = INFINITY;
+ else
+ y = NAN;
+ else if(e > 0)
+ y = (1+f)*pow(2,e-127);
+ //y = (1+f).*2.^(e-127);
+ else
+ y = f*pow(2,-126);
+ //y = f.*2.^-126;
+ if (s)
+ y = -y;
+ return y;
+} \ No newline at end of file
diff --git a/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/mfloor.c b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/mfloor.c
new file mode 100755
index 0000000..3c9e884
--- /dev/null
+++ b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/mfloor.c
@@ -0,0 +1,6 @@
+#include <math.h>
+
+int mfloor(double x)
+{
+ return floor(x);
+} \ No newline at end of file
diff --git a/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/modbus.c b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/modbus.c
new file mode 100755
index 0000000..160bfb5
--- /dev/null
+++ b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/modbus.c
@@ -0,0 +1,449 @@
+#include <errno.h>
+#include <termios.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdio.h>
+#include <math.h>
+#include "../Include/serial.h"
+#include "../Include/ieeesingle2num.h"
+#include "../Include/modbus.h"
+
+char ascii_n(int num)
+{
+ return (char)num;
+}
+
+int ascii_c(char c)
+{
+ return (int)c;
+}
+
+void read_voltage()
+{
+ char arr[9]={ascii_n(1),ascii_n(3),ascii_n(15),ascii_n(86),ascii_n(00),ascii_n(2),ascii_n(39),ascii_n(15),'\0'};
+
+ //printf("%s\n",arr);//debug
+
+ int x,wr,rd;
+ char buf[12];
+ for(x=0;x<5;x++)
+ {
+ wr=write_serial(1,arr,8);
+ rd = read_serial(1,buf,11);
+ usleep(500);
+ }
+ buf[11]='\0';
+
+ //printf("%s\n",buf );//debug
+
+ int b1=0,b2=0,b3=0,b4=0;
+ int myresult[12];
+ int i;
+ for (i = 0; i < 11; ++i)
+ {
+ myresult[i]=ascii_c(buf[i]);
+ //printf("%d",myresult[i]);//debug
+ }
+
+ //printf("%d %d %d %d\n",myresult[4],myresult[5],myresult[6],myresult[7]);//debug
+
+ int a1=myresult[4];
+ if (a1<16)
+ {
+ b1=1;
+ }
+ char v1[3];
+ dec2hex(a1,v1);
+ if (b1)
+ {
+ sprintf(v1,"0%s",v1);
+ }
+ else
+ {
+ sprintf(v1,"%s",v1);
+ }
+ int a2=myresult[5];
+ if (a2<16)
+ {
+ b2=1;
+ }
+ char v2[3];
+ dec2hex(a2,v2);
+ if (b2)
+ {
+ sprintf(v2,"0%s",v2);
+ }
+ else
+ {
+ sprintf(v2,"%s",v2);
+ }
+ int a3=myresult[6];
+ if (a3<16)
+ {
+ b3=1;
+ }
+ char v3[3];
+ dec2hex(a3,v3);
+ if (b3)
+ {
+ sprintf(v3,"0%s",v3);
+ }
+ else
+ {
+ sprintf(v3,"%s",v3);
+ }
+ int a4=myresult[7];
+ if (a4<16)
+ {
+ b4=1;
+ }
+ char v4[3];
+ dec2hex(a4,v4);
+ if (b4)
+ {
+ sprintf(v4,"0%s",v4);
+ }
+ else
+ {
+ sprintf(v4,"%s",v4);
+ }
+ char a6[20]="";
+ strcat(a6,v3);
+ strcat(a6,v4);
+ strcat(a6,v1);
+ strcat(a6,v2);
+ a6[8]='\0';
+
+ //printf("%s",a6);//debug
+
+ int cl=close_serial(1);
+ double p=ieeesingle2num(a6);
+ printf("Voltage(in V)=%f\n",p);
+}
+
+
+void read_current()
+{
+ char arr[9]={ascii_n(1),ascii_n(3),ascii_n(15),ascii_n(88),ascii_n(00),ascii_n(2),ascii_n(70),ascii_n(204),'\0'};
+ //printf("%s\n",arr);//debug
+
+ int x,wr,rd;
+ char buf[12];
+ for(x=0;x<5;x++)
+ {
+ wr=write_serial(1,arr,8);
+ rd = read_serial(1,buf,11);
+ usleep(500);
+ }
+ buf[11]='\0';
+
+ //printf("%s\n",buf );//debug
+
+ int b1=0,b2=0,b3=0,b4=0;
+ int myresult[12];
+ int i;
+ for (i = 0; i < 11; ++i)
+ {
+ myresult[i]=ascii_c(buf[i]);
+ //printf("%d",myresult[i]);//debug
+ }
+
+ //printf("%d %d %d %d\n",myresult[4],myresult[5],myresult[6],myresult[7]);//debug
+
+ int a1=myresult[4];
+ if (a1<16)
+ {
+ b1=1;
+ }
+ char v1[3];
+ dec2hex(a1,v1);
+ if (b1)
+ {
+ sprintf(v1,"0%s",v1);
+ }
+ else
+ {
+ sprintf(v1,"%s",v1);
+ }
+ int a2=myresult[5];
+ if (a2<16)
+ {
+ b2=1;
+ }
+ char v2[3];
+ dec2hex(a2,v2);
+ if (b2)
+ {
+ sprintf(v2,"0%s",v2);
+ }
+ else
+ {
+ sprintf(v2,"%s",v2);
+ }
+ int a3=myresult[6];
+ if (a3<16)
+ {
+ b3=1;
+ }
+ char v3[3];
+ dec2hex(a3,v3);
+ if (b3)
+ {
+ sprintf(v3,"0%s",v3);
+ }
+ else
+ {
+ sprintf(v3,"%s",v3);
+ }
+ int a4=myresult[7];
+ if (a4<16)
+ {
+ b4=1;
+ }
+ char v4[3];
+ dec2hex(a4,v4);
+ if (b4)
+ {
+ sprintf(v4,"0%s",v4);
+ }
+ else
+ {
+ sprintf(v4,"%s",v4);
+ }
+ char a6[20]="";
+ strcat(a6,v3);
+ strcat(a6,v4);
+ strcat(a6,v1);
+ strcat(a6,v2);
+ a6[8]='\0';
+
+ //printf("%s",a6);//debug
+
+ int cl=close_serial(1);
+ double p=ieeesingle2num(a6);
+ printf("Current(in A)=%f\n",p);
+}
+
+void read_active_power()
+{
+ char arr[9]={ascii_n(1),ascii_n(3),ascii_n(15),ascii_n(78),ascii_n(00),ascii_n(2),ascii_n(167),ascii_n(8),'\0'};
+ //printf("%s\n",arr);//debug
+
+ int x,wr,rd;
+ char buf[12];
+ for(x=0;x<5;x++)
+ {
+ wr=write_serial(1,arr,8);
+ rd = read_serial(1,buf,11);
+ usleep(500);
+ }
+ buf[11]='\0';
+
+ //printf("%s\n",buf );//debug
+
+ int b1=0,b2=0,b3=0,b4=0;
+ int myresult[12];
+ int i;
+ for (i = 0; i < 11; ++i)
+ {
+ myresult[i]=ascii_c(buf[i]);
+ //printf("%d",myresult[i]);//debug
+ }
+
+ //printf("%d %d %d %d\n",myresult[4],myresult[5],myresult[6],myresult[7]);//debug
+
+ int a1=myresult[4];
+ if (a1<16)
+ {
+ b1=1;
+ }
+ char v1[3];
+ dec2hex(a1,v1);
+ if (b1)
+ {
+ sprintf(v1,"0%s",v1);
+ }
+ else
+ {
+ sprintf(v1,"%s",v1);
+ }
+ int a2=myresult[5];
+ if (a2<16)
+ {
+ b2=1;
+ }
+ char v2[3];
+ dec2hex(a2,v2);
+ if (b2)
+ {
+ sprintf(v2,"0%s",v2);
+ }
+ else
+ {
+ sprintf(v2,"%s",v2);
+ }
+ int a3=myresult[6];
+ if (a3<16)
+ {
+ b3=1;
+ }
+ char v3[3];
+ dec2hex(a3,v3);
+ if (b3)
+ {
+ sprintf(v3,"0%s",v3);
+ }
+ else
+ {
+ sprintf(v3,"%s",v3);
+ }
+ int a4=myresult[7];
+ if (a4<16)
+ {
+ b4=1;
+ }
+ char v4[3];
+ dec2hex(a4,v4);
+ if (b4)
+ {
+ sprintf(v4,"0%s",v4);
+ }
+ else
+ {
+ sprintf(v4,"%s",v4);
+ }
+ char a6[20]="";
+ strcat(a6,v3);
+ strcat(a6,v4);
+ strcat(a6,v1);
+ strcat(a6,v2);
+ a6[8]='\0';
+
+ //printf("%s",a6);//debug
+
+ int cl=close_serial(1);
+ double p=ieeesingle2num(a6);
+ printf("Active Power(in W)=%f\n",p);
+}
+
+double read_val(int addr_byte)
+{
+ char* arr;
+ if(addr_byte==86)
+ {
+ char code[9]={ascii_n(1),ascii_n(3),ascii_n(15),ascii_n(86),ascii_n(00),ascii_n(2),ascii_n(39),ascii_n(15),'\0'};
+ arr = code;
+ printf("Voltage(in V)=");
+ }
+ else if(addr_byte==88)
+ {
+ char code[9]={ascii_n(1),ascii_n(3),ascii_n(15),ascii_n(88),ascii_n(00),ascii_n(2),ascii_n(70),ascii_n(204),'\0'};
+ arr =code;
+ printf("Current(in A)=");
+ }
+ else if(addr_byte==78)
+ {
+ char code[9]={ascii_n(1),ascii_n(3),ascii_n(15),ascii_n(78),ascii_n(00),ascii_n(2),ascii_n(167),ascii_n(8),'\0'};
+ arr = code;
+ printf("Active Power(in W)=");
+ }
+ //printf("%s\n",arr);//debug
+
+ int x,wr,rd;
+ char buf[12];
+ for(x=0;x<5;x++)
+ {
+ wr=write_serial(1,arr,8);
+ rd = read_serial(1,buf,11);
+ usleep(500);
+ }
+ buf[11]='\0';
+
+ //printf("%s\n",buf );//debug
+
+ int b1=0,b2=0,b3=0,b4=0;
+ int myresult[12];
+ int i;
+ for (i = 0; i < 11; ++i)
+ {
+ myresult[i]=ascii_c(buf[i]);
+ //printf("%d",myresult[i]);//debug
+ }
+
+ //printf("%d %d %d %d\n",myresult[4],myresult[5],myresult[6],myresult[7]);//debug
+
+ int a1=myresult[4];
+ if (a1<16)
+ {
+ b1=1;
+ }
+ char v1[3];
+ dec2hex(a1,v1);
+ if (b1)
+ {
+ sprintf(v1,"0%s",v1);
+ }
+ else
+ {
+ sprintf(v1,"%s",v1);
+ }
+ int a2=myresult[5];
+ if (a2<16)
+ {
+ b2=1;
+ }
+ char v2[3];
+ dec2hex(a2,v2);
+ if (b2)
+ {
+ sprintf(v2,"0%s",v2);
+ }
+ else
+ {
+ sprintf(v2,"%s",v2);
+ }
+ int a3=myresult[6];
+ if (a3<16)
+ {
+ b3=1;
+ }
+ char v3[3];
+ dec2hex(a3,v3);
+ if (b3)
+ {
+ sprintf(v3,"0%s",v3);
+ }
+ else
+ {
+ sprintf(v3,"%s",v3);
+ }
+ int a4=myresult[7];
+ if (a4<16)
+ {
+ b4=1;
+ }
+ char v4[3];
+ dec2hex(a4,v4);
+ if (b4)
+ {
+ sprintf(v4,"0%s",v4);
+ }
+ else
+ {
+ sprintf(v4,"%s",v4);
+ }
+ char a6[20]="";
+ strcat(a6,v3);
+ strcat(a6,v4);
+ strcat(a6,v1);
+ strcat(a6,v2);
+ a6[8]='\0';
+
+ //printf("%s",a6);//debug
+
+ int cl=close_serial(1);
+ double p=ieeesingle2num(a6);
+ printf("%f\n",p);
+ return p;
+}
diff --git a/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/nointerrupts.c b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/nointerrupts.c
new file mode 100755
index 0000000..d8f26dd
--- /dev/null
+++ b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/nointerrupts.c
@@ -0,0 +1,15 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <math.h>
+#include <unistd.h>
+#include </home/mr_robot/arduino-1.8.5/hardware/arduino/avr/cores/arduino/Arduino.h>
+void nointerrupts()
+{
+ noInterrupts();
+}
+
+int main (void)
+{
+ return 0;
+}
diff --git a/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/serialComm.c b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/serialComm.c
new file mode 100755
index 0000000..6fd915a
--- /dev/null
+++ b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/serialComm.c
@@ -0,0 +1,156 @@
+#include <errno.h>
+#include <termios.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h> /* memset */
+#include <stdio.h>
+#include <stropts.h>
+#include "../Include/serial.h"
+#define MAXPORTS 5
+static int fd;
+
+int set_interface_attribs (int fd, int speed, int parity)
+{
+ struct termios tty;
+ memset (&tty, 0, sizeof(tty));
+ if (tcgetattr (fd, &tty) != 0)
+ {
+ return -1;
+ }
+ int posix_baudrate=0;
+
+ switch(speed) {
+ case 115200: posix_baudrate = B115200; break;
+ //default: return -1;
+ }
+ cfsetospeed (&tty, posix_baudrate);
+ cfsetispeed (&tty, posix_baudrate);
+
+ tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
+ // disable IGNBRK for mismatched speed tests; otherwise receive break
+ // as \000 chars
+ tty.c_iflag &= ~IGNBRK; // disable break processing
+ tty.c_lflag = 0; // no signaling chars, no echo,
+ // no canonical processing
+ tty.c_oflag = 0; // no remapping, no delays
+ tty.c_cc[VMIN] = 0; // read doesn't block
+ tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
+
+ tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
+
+ tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
+ // enable reading
+ tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
+ tty.c_cflag |= parity;
+ tty.c_cflag &= ~CSTOPB;
+ tty.c_cflag &= ~CRTSCTS;
+
+ if (tcsetattr (fd, TCSANOW, &tty) != 0)
+ {
+ return -1;
+ }
+ return 0;
+}
+
+
+void set_blocking (int b, int should_block)
+{
+ struct termios tty;
+ memset (&tty, 0, sizeof tty);
+ if (tcgetattr (b, &tty) != 0)
+ {
+ return;
+ }
+
+ tty.c_cc[VMIN] = should_block ? 1 : 0;
+ tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
+
+ //if (tcsetattr (fd, TCSANOW, &tty) != 0)
+ // error_message ("error %d setting term attributes", errno);
+}
+
+int open_serial(int handle,int port, int baudrate){
+ char* portname;
+ //printf("%d\n",port);
+ int OK;
+ switch(port){
+ case 0: portname = "//dev/ttyACM0";break;
+ case 1: portname = "//dev/ttyACM1";break;
+ case 2: portname = "//dev/ttyACM2";break;
+ case 3: portname = "//dev/ttyACM3";break;
+ case 4: portname = "//dev/ttyACM4";break;
+ case 5: portname = "//dev/ttyACM5";break;
+ case 6: portname = "//dev/ttyACM6";break;
+ case 7: portname = "//dev/ttyACM7";break;
+ //default : return 2;
+ }
+ OK = 0;
+ //printf("%s\n",portname);
+ fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
+ //fd = open (portname, O_RDWR | O_NOCTTY); //srikant
+ //printf("%d\n",fd);
+ if (fd < 0)
+ {
+ OK=2;
+ return OK;
+ }
+ set_interface_attribs (fd, baudrate, 0);
+ set_blocking (fd, 0); // set no blocking
+ //printf("Program running with status : %d\n",OK);
+ return OK;
+}
+
+int close_serial(int handle){
+ //usleep(1000);
+ close(fd);
+ //printf("Program closing....\n");
+ return 0;
+}
+
+int write_serial(int handle, char str[],int size){
+ if(write(fd, str, size)!=-1)
+ {
+ //printf("serial write started.\n");
+ //printf("%s\n",str );
+ //printf("%d\n",(int)str[0] );
+ }
+ usleep(size*100);
+ return 0;
+}
+
+
+int status_serial(int handle,int nb[]){
+ nb[0] = 2;
+ nb[1] = 2;
+ int OK=0;
+ //printf("Getting status.....\n");
+ return OK;
+}
+
+int read_serial(int handle,char buf[],int size){
+
+ char readbuf[size+1];
+ //int fl=tcflush(fd,TCOFLUSH);
+ //int fl=ioctl(fd,I_FLUSH,FLUSHW);
+ if(read(fd, readbuf, size)!=-1)
+ {
+ readbuf[size]='\0';
+ int i;
+ for (i = 0; i < size; ++i)
+ {
+ buf[i]=(readbuf[i]);
+ //printf("%u\n",buf[i] );
+ }
+ buf[size]='\0';
+ //printf("Reading has commenced..\n");
+ //printf("%d\n",(int)strlen(readbuf));
+ //printf("%s\n",readbuf);
+ return 0;
+ }
+ else
+ return 2 ;
+}
+/*void read_serial(int *handle,char buf[],int *size){
+ read(fd, buf, *size);
+ printf("Reading has commenced..\n");
+}*/
diff --git a/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/servomotor.c b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/servomotor.c
new file mode 100755
index 0000000..74048a3
--- /dev/null
+++ b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/servomotor.c
@@ -0,0 +1,87 @@
+#include <errno.h>
+#include <termios.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <math.h>
+#include "../Include/serial.h"
+#include "../Include/servomotor.h"
+
+void cmd_servo_attach(int h,int servo_no)
+{
+ char pin[5];
+ int wr;
+ printf("init servo write\n");
+ if(servo_no==1)//servo 1 on pin 9
+ {
+ strcpy(pin,"Sa1");
+ //printf("%s\n",pin);
+ wr=write_serial(h,pin,3);
+ }
+ else if(servo_no==2)//servo 2 on pin 10
+ {
+ strcpy(pin,"Sa2");
+ wr=write_serial(h,pin,3);
+ }
+ else
+ perror("Error.");
+}
+
+
+void cmd_servo_move(int h,int servo_no,int u1)
+{
+ int wr;
+ char servo[2];
+ char v[2];
+ char pin[5]="Sw";
+ if (u1<0)
+ {
+ sprintf(servo,"%c",48+servo_no);
+ sprintf(v,"%c",0);
+ strcat(pin,servo);
+ strcat(pin,v);
+ //printf("%s\n",pin );
+ //pin="Sw"+ascii(48+servo_no)+ascii(0);
+ }
+ else if(u1>180)
+ {
+ sprintf(servo,"%c",48+servo_no);
+ sprintf(v,"%c",180);
+ strcat(pin,servo);
+ strcat(pin,v);
+ //printf("%s\n",pin );
+ } //pin="Sw"+ascii(48+servo_no)+ascii(180);
+ else
+ {
+ sprintf(servo,"%c",48+servo_no);
+ sprintf(v,"%c",(uint8_t)u1);
+ strcat(pin,servo);
+ strcat(pin,v);
+ //printf("%s\n",pin );
+ } //pin="Sw"+ascii(48+servo_no)+ascii(uint8(u1));
+ wr=write_serial(1,pin,4);
+}
+
+
+void cmd_servo_detach(int h,int servo_no)
+{
+ int wr;
+ char pin[5]="Sd";
+ if(servo_no==1) //servo 1 on pin 10
+ {
+ strcat(pin,"1");
+ //pin="Sd1"
+ wr=write_serial(h,pin,3);
+ }
+ else if(servo_no==2) //servo 2 on pin 9
+ {
+ strcat(pin,"2");
+ //pin="Sd2"
+ wr=write_serial(h,pin,3);
+ }
+ else
+ perror("Error.");
+
+} \ No newline at end of file
diff --git a/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/wakeup.c b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/wakeup.c
new file mode 100755
index 0000000..43ceb48
--- /dev/null
+++ b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/wakeup.c
@@ -0,0 +1,16 @@
+#include <stdio.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "../Include/serial.h"
+
+
+
+void wakeup()
+{
+ /* Set signal to this function. */
+ signal(SIGALRM, wakeup);
+ /* Set alarm clock for 5 seconds. */
+ alarm(2);
+}
+