Skip to content

Commit

Permalink
fixes for multiple pdos of same data direction and for undeclared IO …
Browse files Browse the repository at this point in the history
…variables in PLC program
  • Loading branch information
rainer kordmaa committed Nov 19, 2023
1 parent ed34bef commit 4dc18a4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/bin/ethercat_src_test",
"args": ["${workspaceFolder}/conf/ethercatcfg.txt"],
"args": ["${workspaceFolder}/build/ethercat.cfg"],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"miDebuggerPath": "/home/pi/gdb.sh",
"MIMode": "gdb",
"setupCommands": [
{
Expand Down
31 changes: 25 additions & 6 deletions src/ecat_handler/ecat_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,19 @@ int ConfigureSlave(EcatConfig *config, SlaveConfig *slave, ec_slave_config_t *sc
rxp[i].index = rxpdo.index;
rxp[i].n_entries = 1;
ss[rxpdo.sm].dir = EC_DIR_OUTPUT;
/*
if for example
22:52:44 Sync master 0 EC_DIR_OUTPUT
22:52:46 PDO index 1/1 0x1A00 entryindex 0x3101 subindex 1 bitlen 8
22:52:56 Sync master 1 EC_DIR_OUTPUT
22:53:03 PDO index 1/1 0x1A01 entryindex 0x3101 subindex 2 bitlen 8
then assuming RxPDO entries are in correct order
if one entry sm0, another sm1 and then again sm0, then problem
*/
if (ss[rxpdo.sm].n_pdos == 0)ss[rxpdo.sm].pdos = rxp + i;
ss[rxpdo.sm].n_pdos++;
ss[rxpdo.sm].pdos = rxp;
if(RegisterRxInDomain(slave, &rxe[i], &rxpdo)){
log_error("Failed to register RxPDO in domain");
return -1;
Expand All @@ -189,8 +200,8 @@ int ConfigureSlave(EcatConfig *config, SlaveConfig *slave, ec_slave_config_t *sc
txp[i].index = txpdo.index;
txp[i].n_entries = 1;
ss[txpdo.sm].dir = EC_DIR_INPUT;
if (ss[txpdo.sm].n_pdos == 0)ss[txpdo.sm].pdos = txp + i;
ss[txpdo.sm].n_pdos++;
ss[txpdo.sm].pdos = txp;
if(RegisterTxInDomain(slave, &txe[i], &txpdo)){
log_error("Failed to register TxPDO in domain");
return -1;
Expand Down Expand Up @@ -558,10 +569,14 @@ int EtherCATcyclic(int buffersize,
}
} else if(pdo.bitlength == 8){
uint8_t *ptr = byte_input(i);
*ptr = config->config_only_flag ? 0 : EC_READ_U8(domain1_pd + pdo.offset);
if(!config->config_only_flag && ptr){
*ptr = EC_READ_U8(domain1_pd + pdo.offset);
}
} else if(pdo.bitlength == 16){
uint16_t *ptr = word_input(i);
*ptr = config->config_only_flag ? 0 : EC_READ_U16(domain1_pd + pdo.offset);
if(!config->config_only_flag && ptr){
*ptr = EC_READ_U16(domain1_pd + pdo.offset);
}
}
}

Expand All @@ -579,10 +594,14 @@ int EtherCATcyclic(int buffersize,
}
} else if(pdo.bitlength == 8){
uint8_t *ptrout = byte_output(i);
if(!config->config_only_flag) EC_WRITE_U8(domain1_pd + pdo.offset, *ptrout);
if(!config->config_only_flag && ptrout){
EC_WRITE_U8(domain1_pd + pdo.offset, *ptrout);
}
} else if(pdo.bitlength == 16){
uint16_t *ptrout = word_output(i);
if(!config->config_only_flag) EC_WRITE_U16(domain1_pd + pdo.offset, *ptrout);
if(!config->config_only_flag && ptrout){
EC_WRITE_U16(domain1_pd + pdo.offset, *ptrout);
}
}
}
}
Expand Down
18 changes: 17 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "ecat_handler.h"
#include "log.h"
#include "ethercat_src.h"
#include <time.h>


#define BUFFER_SIZE 1024
/****************************************************************************/
Expand All @@ -35,6 +37,18 @@ uint16_t *int_input_call_back(int a){ return &longvar; }
uint16_t *int_output_call_back(int a){ return &longvar; }
void logger_callbackf(unsigned char *msg){ printf("PLC log: %s", msg); }

//-----------------------------------------------------------------------------
// Helper function - Makes the running thread sleep for the ammount of time
// in milliseconds
//-----------------------------------------------------------------------------
void sleepms(int milliseconds)
{
struct timespec ts;
ts.tv_sec = milliseconds / 1000;
ts.tv_nsec = (milliseconds % 1000) * 1000000;
nanosleep(&ts, NULL);
}

int main (int argc, char **argv){
type_logger_callback logger = logger_callbackf;
ethercat_configure(argv[1], logger);
Expand All @@ -45,14 +59,16 @@ int main (int argc, char **argv){
int16var_call_back int_input_callback = int_input_call_back;
int16var_call_back int_output_callback = int_output_call_back;

for (int i = 0; i < 10; i++){
for (int i = 0; i < 100; i++){
ethercat_callcyclic(BUFFER_SIZE,
bool_input_callback,
bool_output_callback,
byte_input_callback,
byte_output_callback,
int_input_callback,
int_output_callback);

sleepms(100);
}

ethercat_terminate_src();
Expand Down

0 comments on commit 4dc18a4

Please sign in to comment.