Commit 81cccadc authored by Claes's avatar Claes

Arduino, PWM Ao added

parent a5f9c58c
......@@ -39,6 +39,8 @@ int i;
int j;
int aiList[32];
int aiCnt;
int aoList[32];
int aoCnt;
byte msgType;
byte msgId;
byte msgSize;
......@@ -52,9 +54,10 @@ const int debug = 0;
const int MSG_TYPE_WRITE = 1;
const int MSG_TYPE_DIREAD = 2;
const int MSG_TYPE_AIREAD = 3;
const int MSG_TYPE_CONFIGURE = 4;
const int MSG_TYPE_STATUS = 5;
const int MSG_TYPE_DEBUG = 6;
const int MSG_TYPE_AOWRITE = 4;
const int MSG_TYPE_CONFIGURE = 5;
const int MSG_TYPE_STATUS = 6;
const int MSG_TYPE_DEBUG = 7;
const int ARD__SUCCESS = 1;
const int ARD__DICONFIG = 2;
......@@ -79,14 +82,14 @@ void sendDebug( byte sts)
//
void resetOutput()
{
if ( msgSize == doSize) {
for ( i = 0; i < doSize; i++) {
for ( j = 0; j < 8; j++) {
if ( ((1 << j) & doMask[i]) != 0)
digitalWrite( i * 8 + j, LOW);
}
}
}
for ( i = 0; i < aoCnt; i++)
analogWrite( aoList[i], 0);
}
//
......@@ -176,6 +179,19 @@ void loop()
sts = ARD__COMMERROR;
}
}
else if ( msgType == MSG_TYPE_AOWRITE) {
// Write analog outputs
if ( msgSize == aoSize) {
for ( i = 0; i < aoCnt; i++)
analogWrite( aoList[i], msgData[i]);
sts = ARD__SUCCESS;
}
else {
sts = ARD__COMMERROR;
}
}
else if ( msgType == MSG_TYPE_DIREAD) {
// Read Digital inputs
smsg[0] = diSize + 3;
......@@ -296,6 +312,17 @@ void loop()
}
}
}
// Create list of configured Ao
aoCnt = 0;
for ( i = 0; i < aoSize; i++) {
for ( j = 0; j < 8; j++) {
if ( ((1 << j) & aoMask[i]) != 0) {
aoList[aoCnt] = i * 8 + j;
aoCnt++;
}
}
}
}
// Send configuration status
......
......@@ -51,6 +51,7 @@ typedef struct {
int DiPollId;
int DiPendingPoll;
int AiIntervalCnt;
int AoIntervalCnt;
io_sChannel *DChanList[D_MAX_SIZE * 8];
io_sChannel *AiChanList[AI_MAX_SIZE * 8];
io_sChannel *AoChanList[AO_MAX_SIZE * 8];
......@@ -86,9 +87,10 @@ typedef enum {
ard_eMsgType_Write = 1,
ard_eMsgType_DiRead = 2,
ard_eMsgType_AiRead = 3,
ard_eMsgType_Configure = 4,
ard_eMsgType_Status = 5,
ard_eMsgType_Debug = 6
ard_eMsgType_AoWrite = 4,
ard_eMsgType_Configure = 5,
ard_eMsgType_Status = 6,
ard_eMsgType_Debug = 7
} ard_eMsgType;
#define ARD__SUCCESS 1
......@@ -501,6 +503,7 @@ static pwr_tStatus IoCardRead( io_tCtx ctx,
if ( EVEN(sts)) {
}
else {
int ai_cnt = 0;
for ( i = 0; i < local->AiSize; i++) {
for ( j = 0; j < 8; j++) {
m = 1 << j;
......@@ -513,7 +516,7 @@ static pwr_tStatus IoCardRead( io_tCtx ctx,
// Request to calculate new coefficients
io_AiRangeToCoef( chanp);
ivalue = rmsg.data[(i*8+j)*2] * 256 + rmsg.data[(i*8+j)*2+1];
ivalue = rmsg.data[ai_cnt*2] * 256 + rmsg.data[ai_cnt*2+1];
io_ConvertAi( cop, ivalue, &actvalue);
// Filter
......@@ -527,6 +530,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++;
}
}
}
......@@ -582,6 +586,62 @@ static pwr_tStatus IoCardWrite( io_tCtx ctx,
sts = write( local->fd, &msg, msg.size);
}
if ( local->AoSize) {
int skip_ao = 0;
if ( op->AoScanInterval > 1) {
skip_ao = local->AoIntervalCnt;
local->AoIntervalCnt++;
if ( local->AoIntervalCnt >= op->AoScanInterval)
local->AoIntervalCnt = 0;
}
if ( !skip_ao) {
ard_sMsg msg;
int value;
memset( &msg, 0, sizeof(msg));
msg.size = local->AoNum + 3;
msg.id = local->IdCnt++;
msg.type = ard_eMsgType_AoWrite;
int ao_cnt = 0;
for ( i = 0; i < local->AoSize; i++) {
for ( j = 0; j < 8; j++) {
m = 1 << j;
if ( local->AoMask[i] & m) {
io_sChannel *chanp = local->AoChanList[i*8+j];
pwr_sClass_ChanAo *cop = (pwr_sClass_ChanAo *)chanp->cop;
pwr_sClass_Ao *sop = (pwr_sClass_Ao *)chanp->sop;
if ( cop->CalculateNewCoef)
// Request to calculate new coefficients
io_AoRangeToCoef( chanp);
value = *(pwr_tFloat32 *)chanp->vbp * cop->OutPolyCoef1 +
cop->OutPolyCoef0 + 0.49;
if ( value < 0)
value = 0;
else if (value > 255)
value = 255;
msg.data[ao_cnt] = value;
sop->SigValue = cop->SigValPolyCoef1 * *(pwr_tFloat32 *)chanp->vbp +
cop->SigValPolyCoef0;
sop->RawValue = value;
ao_cnt++;
}
}
}
// logg( "Write Ao");
sts = write( local->fd, &msg, msg.size);
}
}
if ( op->ErrorCount >= op->ErrorSoftLimit &&
error_count < op->ErrorSoftLimit) {
errh_Warning( "IO Card ErrorSoftLimit reached, '%s'", cp->Name);
......
......@@ -2485,7 +2485,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 = "_X24"
Attr NextAix = "_X25"
EndBody
!/**
! Optional description.
......@@ -2634,6 +2634,20 @@ Volume OtherIO $ClassVolume 0.0.250.10
Attr TypeRef = "pwrs:Type-$UInt32"
EndBody
EndObject
!/**
! Specifies, in relation to the scantime of the thread, how often
! the Ao channels of the board are handled. If AoScanInterval is 1 or 0,
! the Ao are handled every scan. If AoScanInterval is for example 10, they
! are handled every 10'th scan, i.e. the scantime for the module
! will be AoScanInterval times the scantime of the thread.
!*/
Object AoScanInterval $Attribute 24 19-JAN-2011 18:24:34.75
Body SysBody 19-JAN-2011 18:24:35.83
Attr PgmName = "AoScanInterval"
Attr Flags = 16777216
Attr TypeRef = "pwrs:Type-$UInt32"
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
......
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