Commit c1b0bc4c authored by Claes Sjofors's avatar Claes Sjofors

Merge branch 'master' of pwrcvs:/data1/git/pwr

parents 2df1813b ba39cfbd
......@@ -34,7 +34,18 @@
* General Public License plus this exception.
*/
/* rt_io_m_motioncontrol_usbio.c -- I/O methods for class MotionControl_USBIO. */
/* rt_io_m_arduino_uno.c -- I/O methods for class Arduino_Uno. */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <float.h>
#include <termios.h>
#include "pwr.h"
#include "pwr_basecomponentclasses.h"
......@@ -83,21 +94,13 @@ typedef struct {
ard_eMsgType PendingMsgType;
int AiIntervalCnt;
int AoIntervalCnt;
int Reconnect;
int ReconnectCnt;
io_sChannel *DChanList[D_MAX_SIZE * 8];
io_sChannel *AiChanList[AI_MAX_SIZE * 8];
io_sChannel *AoChanList[AO_MAX_SIZE * 8];
} io_sLocal;
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>
// static FILE *fp;
#if 0
......@@ -130,16 +133,41 @@ typedef struct {
unsigned char data[100] __attribute__ ((aligned(1)));
} ard_sMsg;
static int receive( int fd, int id, ard_sMsg *rmsg, int size)
static unsigned char checksum( unsigned char *buf, int len)
{
int i;
unsigned char sum = 0;
for ( i = 0; i < len; i++)
sum ^= buf[i];
return sum;
}
static void get_tv(struct timeval *tv, float t)
{
if ( t < FLT_EPSILON) {
tv->tv_sec = 1;
tv->tv_usec = 0;
}
else {
tv->tv_sec = t;
tv->tv_usec = (t-(float)tv->tv_sec) * 1000000;
}
}
static int receive( int fd, int id, ard_sMsg *rmsg, int size, float tmo)
{
fd_set rfd;
struct timeval tv = {1,0};
struct timeval tv;
int sts;
int msize;
int ret;
FD_ZERO(&rfd);
FD_SET(fd, &rfd);
while ( 1) {
get_tv( &tv, tmo);
sts = select(fd+1, &rfd, NULL, NULL, &tv);
if ( sts == 0) return ARD__NOMSG;
......@@ -147,12 +175,17 @@ static int receive( int fd, int id, ard_sMsg *rmsg, int size)
msize += read( fd, rmsg, 1);
// logg( "Receive read");
if ( msize == 0) return ARD__NOMSG;
while ( msize < rmsg->size) {
get_tv( &tv, tmo);
sts = select(fd+1, &rfd, NULL, NULL, &tv);
if ( sts == 0) return ARD__NOMSG;
msize += read( fd, (char *)rmsg + msize, rmsg->size - msize);
ret = read( fd, (char *)rmsg + msize, rmsg->size - msize);
if ( ret == 0) return ARD__NOMSG;
msize += ret;
// logg( "Receive read ++");
......@@ -373,6 +406,12 @@ static pwr_tStatus IoCardInit( io_tCtx ctx,
break;
}
tty_attributes.c_iflag &= ~(BRKINT | ICRNL | IMAXBEL);
tty_attributes.c_oflag &= ~(OPOST | ONLCR);
tty_attributes.c_lflag &= ~(ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE);
tty_attributes.c_cc[VMIN] = 1;
tty_attributes.c_cc[VTIME] = 0;
tty_attributes.c_cc[VEOF] = 1;
sts = tcsetattr( local->fd, TCSANOW, &tty_attributes);
if ( sts < 0) {
errh_Error( "IO Init Card '%s', unable to set baud rate on device %s", cp->Name, op->Device);
......@@ -419,7 +458,7 @@ static pwr_tStatus IoCardInit( io_tCtx ctx,
sts = write( local->fd, &msg, msg.size);
sts = receive( local->fd, msg.id, &rmsg, 1);
sts = receive( local->fd, msg.id, &rmsg, 1, op->Timeout);
if ( sts & 1) {
op->Status = rmsg.data[0];
}
......@@ -466,6 +505,10 @@ static pwr_tStatus IoCardRead( io_tCtx ctx,
int skip_ai;
ard_eMsgType mtype;
ard_sMsg msg;
io_sChannel *chanp;
if ( local->Reconnect)
return IO__SUCCESS;
if ( local->AiSize) {
skip_ai = 0;
......@@ -508,7 +551,8 @@ static pwr_tStatus IoCardRead( io_tCtx ctx,
pwr_tFloat32 actvalue;
sts = receive( local->fd, local->DiPollId, &rmsg, local->DiSize + local->AiNum * 2);
sts = receive( local->fd, local->DiPollId, &rmsg, local->DiSize + local->AiNum * 2,
op->Timeout);
op->Status = sts;
if ( EVEN(sts)) {
op->ErrorCount++;
......@@ -519,8 +563,13 @@ static pwr_tStatus IoCardRead( io_tCtx ctx,
for ( i = 0; i < local->DiSize; i++) {
for ( j = 0; j < 8; j++) {
m = 1 << j;
if ( local->DiMask[i] & m)
*(pwr_tBoolean *)local->DChanList[i*8+j]->vbp = ((rmsg.data[i] & m) != 0);
if ( local->DiMask[i] & m) {
chanp = local->DChanList[i*8+j];
pwr_sClass_ChanDi *cop = (pwr_sClass_ChanDi *) chanp->cop;
if ( cop->ConversionOn)
*(pwr_tBoolean *)chanp->vbp = ((rmsg.data[i] & m) != 0);
}
}
}
}
......@@ -534,6 +583,7 @@ static pwr_tStatus IoCardRead( io_tCtx ctx,
pwr_sClass_ChanAi *cop = (pwr_sClass_ChanAi *)chanp->cop;
pwr_sClass_Ai *sop = (pwr_sClass_Ai *)chanp->sop;
if ( cop->ConversionOn) {
if ( cop->CalculateNewCoef)
// Request to calculate new coefficients
io_AiRangeToCoef( chanp);
......@@ -553,6 +603,7 @@ static pwr_tStatus IoCardRead( io_tCtx ctx,
*(pwr_tFloat32 *)chanp->vbp = actvalue;
sop->SigValue = cop->SigValPolyCoef1 * ivalue + cop->SigValPolyCoef0;
sop->RawValue = ivalue;
}
ai_cnt++;
}
}
......@@ -566,7 +617,7 @@ static pwr_tStatus IoCardRead( io_tCtx ctx,
int i, j;
unsigned char m;
sts = receive( local->fd, local->DiPollId, &rmsg, local->DiSize);
sts = receive( local->fd, local->DiPollId, &rmsg, local->DiSize, op->Timeout);
op->Status = sts;
if ( EVEN(sts)) {
op->ErrorCount++;
......@@ -577,8 +628,13 @@ static pwr_tStatus IoCardRead( io_tCtx ctx,
for ( i = 0; i < local->DiSize; i++) {
for ( j = 0; j < 8; j++) {
m = 1 << j;
if ( local->DiMask[i] & m)
*(pwr_tBoolean *)local->DChanList[i*8+j]->vbp = ((rmsg.data[i] & m) != 0);
if ( local->DiMask[i] & m) {
chanp = local->DChanList[i*8+j];
pwr_sClass_ChanDi *cop = (pwr_sClass_ChanDi *) chanp->cop;
if ( cop->ConversionOn)
*(pwr_tBoolean *)chanp->vbp = ((rmsg.data[i] & m) != 0);
}
}
}
}
......@@ -591,7 +647,7 @@ static pwr_tStatus IoCardRead( io_tCtx ctx,
pwr_tInt32 ivalue;
pwr_tFloat32 actvalue;
sts = receive( local->fd, local->DiPollId, &rmsg, local->AiNum * 2);
sts = receive( local->fd, local->DiPollId, &rmsg, local->AiNum * 2, op->Timeout);
if ( EVEN(sts)) {
}
else {
......@@ -604,6 +660,7 @@ static pwr_tStatus IoCardRead( io_tCtx ctx,
pwr_sClass_ChanAi *cop = (pwr_sClass_ChanAi *)chanp->cop;
pwr_sClass_Ai *sop = (pwr_sClass_Ai *)chanp->sop;
if ( cop->ConversionOn) {
if ( cop->CalculateNewCoef)
// Request to calculate new coefficients
io_AiRangeToCoef( chanp);
......@@ -622,6 +679,7 @@ static pwr_tStatus IoCardRead( io_tCtx ctx,
*(pwr_tFloat32 *)chanp->vbp = actvalue;
sop->SigValue = cop->SigValPolyCoef1 * ivalue + cop->SigValPolyCoef0;
sop->RawValue = ivalue;
}
ai_cnt++;
}
}
......@@ -656,6 +714,20 @@ static pwr_tStatus IoCardWrite( io_tCtx ctx,
ard_sMsg msg;
int sts;
int skip_ao;
io_sChannel *chanp;
if ( local->Reconnect) {
local->ReconnectCnt++;
if ( local->ReconnectCnt * ctx->ScanTime > 5.0) {
sts = IoCardClose( ctx, ap, rp, cp);
sts = IoCardInit( ctx, ap, rp, cp);
local = cp->Local;
if ( EVEN(sts)) {
local->Reconnect = 1;
}
}
return IO__SUCCESS;
}
if ( local->AoSize) {
skip_ao = 0;
......@@ -682,7 +754,16 @@ static pwr_tStatus IoCardWrite( io_tCtx ctx,
for ( j = 0; j < 8; j++) {
m = 1 << j;
if ( local->DoMask[i] & m) {
if ( *(pwr_tBoolean *)local->DChanList[i*8+j]->vbp)
chanp = local->DChanList[i*8+j];
pwr_sClass_ChanDo *cop = (pwr_sClass_ChanDo *) chanp->cop;
pwr_tInt32 do_actval;
if ( cop->TestOn != 0)
do_actval = cop->TestValue;
else
do_actval = *(pwr_tInt32 *) chanp->vbp;
if ( do_actval)
msg.data[i] |= m;
}
}
......@@ -717,8 +798,13 @@ static pwr_tStatus IoCardWrite( io_tCtx ctx,
io_AoRangeToCoef( chanp);
if ( cop->TestOn)
value = cop->TestValue * cop->OutPolyCoef1 +
cop->OutPolyCoef0 + 0.49;
else
value = *(pwr_tFloat32 *)chanp->vbp * cop->OutPolyCoef1 +
cop->OutPolyCoef0 + 0.49;
if ( value < 0)
value = 0;
else if (value > 255)
......@@ -747,7 +833,16 @@ static pwr_tStatus IoCardWrite( io_tCtx ctx,
for ( j = 0; j < 8; j++) {
m = 1 << j;
if ( local->DoMask[i] & m) {
if ( *(pwr_tBoolean *)local->DChanList[i*8+j]->vbp)
chanp = local->DChanList[i*8+j];
pwr_sClass_ChanDo *cop = (pwr_sClass_ChanDo *) chanp->cop;
pwr_tInt32 do_actval;
if ( cop->TestOn != 0)
do_actval = cop->TestValue;
else
do_actval = *(pwr_tInt32 *) chanp->vbp;
if ( do_actval)
msg.data[i] |= m;
}
}
......@@ -770,8 +865,13 @@ static pwr_tStatus IoCardWrite( io_tCtx ctx,
io_AoRangeToCoef( chanp);
if ( cop->TestOn)
value = cop->TestValue * cop->OutPolyCoef1 +
cop->OutPolyCoef0 + 0.49;
else
value = *(pwr_tFloat32 *)chanp->vbp * cop->OutPolyCoef1 +
cop->OutPolyCoef0 + 0.49;
if ( value < 0)
value = 0;
else if (value > 255)
......@@ -790,6 +890,12 @@ static pwr_tStatus IoCardWrite( io_tCtx ctx,
sts = write( local->fd, &msg, msg.size);
}
if ( sts < 0) {
/* Connection lost, open device again */
local->Reconnect = 1;
local->ReconnectCnt = 0;
return IO__SUCCESS;
}
if ( op->ErrorCount >= op->ErrorSoftLimit &&
error_count < op->ErrorSoftLimit) {
......
Volume OtherIO $ClassVolume 0.0.250.10
Body SysBody 01-JAN-1970 01:00:00.00
Attr NextOix = "_X304"
Attr NextOix = "_X308"
Attr NextCix = "_X36"
Attr NextTix[0] = "_X11"
EndBody
......@@ -456,13 +456,34 @@ Volume OtherIO $ClassVolume 0.0.250.10
Attr TypeRef = "pwrs:Type-$Mask"
Attr Elements = 1
EndBody
Object OptimizedDiPoll $Bit 169 06-JAN-2011 15:49:48.79
Body SysBody 06-JAN-2011 15:49:55.05
Object OptimizedDiPoll $Bit 308 05-FEB-2012 18:28:28.45
Body SysBody 05-FEB-2012 18:28:28.45
Attr Text = "OptimizedDiPoll"
Attr PgmName = "OptimizedDiPoll"
Attr Value = 1
EndBody
EndObject
Object ConnectionRequest $Bit 305 05-FEB-2012 16:29:04.97
Body SysBody 05-FEB-2012 16:29:08.51
Attr Text = "ConnectionRequest"
Attr PgmName = "ConnectionRequest"
Attr Value = 2
EndBody
EndObject
Object Ao16Bit $Bit 306 05-FEB-2012 16:29:52.15
Body SysBody 05-FEB-2012 16:29:55.69
Attr Text = "Ao16Bit"
Attr PgmName = "Ao16Bit"
Attr Value = 4
EndBody
EndObject
Object Checksum $Bit 307 05-FEB-2012 18:27:48.23
Body SysBody 05-FEB-2012 18:27:51.71
Attr Text = "Checksum"
Attr PgmName = "Checksum"
Attr Value = 8
EndBody
EndObject
EndObject
Object Hilscher_cifX_CommStateEnum $TypeDef 8 11-MAR-2011 09:57:02.13
Body SysBody 11-MAR-2011 09:57:13.54
......@@ -4079,7 +4100,7 @@ Volume OtherIO $ClassVolume 0.0.250.10
Object RtBody $ObjBodyDef 1 13-DEC-2010 21:36:40.01
Body SysBody 13-DEC-2010 21:39:07.38
Attr StructName = "Arduino_Uno"
Attr NextAix = "_X25"
Attr NextAix = "_X28"
EndBody
!/**
! Optional description.
......@@ -4242,6 +4263,25 @@ Volume OtherIO $ClassVolume 0.0.250.10
Attr TypeRef = "pwrs:Type-$UInt32"
EndBody
EndObject
Object StallAction $Attribute 25 05-FEB-2012 16:31:41.97
Body SysBody 05-FEB-2012 16:31:41.97
Attr PgmName = "StallAction"
Attr TypeRef = "pwrb:Type-StallActionEnum"
EndBody
EndObject
Object Timeout $Attribute 26 05-FEB-2012 18:33:56.70
Body SysBody 05-FEB-2012 18:33:53.26
Attr PgmName = "Timeout"
Attr TypeRef = "pwrs:Type-$Float32"
EndBody
EndObject
Object FirmwareVersion $Attribute 27 05-FEB-2012 16:33:33.69
Body SysBody 05-FEB-2012 16:33:52.27
Attr PgmName = "FirmwareVersion"
Attr Flags = 1024
Attr TypeRef = "pwrs:Type-$String40"
EndBody
EndObject
EndObject
Object IoMethods $RtMethod 144 13-DEC-2010 21:36:40.01
Object IoCardInit $Method 145 13-DEC-2010 21:36:40.01
......@@ -4286,12 +4326,13 @@ Volume OtherIO $ClassVolume 0.0.250.10
EndBody
EndObject
Object Template Arduino_Uno 2151907328 01-JAN-1970 01:00:00.00
Body RtBody 09-MAR-2011 15:42:29.56
Body RtBody 05-FEB-2012 16:34:14.13
Attr Process = 1
Attr ErrorSoftLimit = 25
Attr ErrorHardLimit = 50
Attr WatchdogTime = 5.000000e+00
Attr BaudRate = 9600
Attr Timeout = 1.000000e+00
EndBody
EndObject
EndObject
......@@ -6008,7 +6049,9 @@ Volume OtherIO $ClassVolume 0.0.250.10
!*/
Object SPI_Master $ClassDef 35 15-JAN-2012 10:38:09.98
Body SysBody 15-JAN-2012 10:38:18.39
Attr Flags = 10240
Attr Editor = 0
Attr Method = 0
Attr Flags = 10256
EndBody
Object RtBody $ObjBodyDef 1 15-JAN-2012 10:38:27.11
Body SysBody 15-JAN-2012 10:38:27.11
......@@ -6017,11 +6060,19 @@ Volume OtherIO $ClassVolume 0.0.250.10
EndBody
Object Super $Attribute 1 15-JAN-2012 10:38:35.30
Body SysBody 15-JAN-2012 10:40:19.18
Attr Flags = 17170432
Attr PgmName = "Super"
Attr Flags = 393216
Attr TypeRef = "BaseComponent:Class-BaseIORack"
EndBody
EndObject
EndObject
Object Template SPI_Master 2156888064 01-JAN-1970 01:00:00.00
Body RtBody 01-JAN-1970 01:00:00.00
Attr Super.Process = 1
Attr Super.ErrorSoftLimit = 50
Attr Super.ErrorHardLimit = 100
EndBody
EndObject
EndObject
!/**
! @Version 1.0
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment