Alex exoskeleton
ALEX SoftwareDocumentation
CO_master.c
Go to the documentation of this file.
1 /*
2  * CANopen master functions
3  *
4  * @file CO_master.c
5  * @author Janez Paternoster
6  * @copyright 2015 Janez Paternoster
7  *
8  * This file is part of CANopenSocket, a Linux implementation of CANopen
9  * stack with master functionality. Project home page is
10  * <https://github.com/CANopenNode/CANopenSocket>. CANopenSocket is based
11  * on CANopenNode: <https://github.com/CANopenNode/CANopenNode>.
12  *
13  * CANopenSocket is free and open source software: you can redistribute
14  * it and/or modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation, either version 2 of the
16  * License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see <http://www.gnu.org/licenses/>.
25  */
26 
27 
28 #include "CO_master.h"
29 
30 
31 /******************************************************************************/
33  CO_SDOclient_t *SDOclient,
34  uint8_t nodeID,
35  uint16_t idx,
36  uint8_t subidx,
37  uint8_t *dataRx,
38  uint32_t dataRxSize,
39  uint32_t *dataRxLen,
40  uint32_t *SDOabortCode,
41  uint16_t SDOtimeoutTime,
42  uint8_t blockTransferEnable)
43 {
44  int err = 0;
45 
46  /* stay here, if CAN is not configured */
47  pthread_mutex_lock(&CO_CAN_VALID_mtx);
48 
49 
50  /* Setup client. */
51  if(CO_SDOclient_setup(SDOclient, 0, 0, nodeID) != CO_SDOcli_ok_communicationEnd) {
52  err = 1;
53  }
54 
55  /* Initiate upload. */
56  if(err == 0){
57  if(CO_SDOclientUploadInitiate(SDOclient, idx, subidx, dataRx,
58  dataRxSize, blockTransferEnable) != CO_SDOcli_ok_communicationEnd)
59  {
60  err = 1;
61  }
62  }
63 
64  /* Upload data. Loop in 10 ms intervals. */
65  if(err == 0){
66  CO_SDOclient_return_t ret;
67  uint16_t timer1msPrev;
68  struct timespec sleepTime;
69 
70  timer1msPrev = CO_timer1ms;
71  sleepTime.tv_sec = 0;
72  sleepTime.tv_nsec = 10000000;
73 
74  do {
75  uint16_t timer1ms, timer1msDiff;
76 
77  /* Calculate time difference */
78  timer1ms = CO_timer1ms;
79  timer1msDiff = timer1ms - timer1msPrev;
80  timer1msPrev = timer1ms;
81 
82  ret = CO_SDOclientUpload(SDOclient, timer1msDiff, SDOtimeoutTime, dataRxLen, SDOabortCode);
83  nanosleep(&sleepTime, NULL);
84  } while(ret > 0);
85 
86  CO_SDOclientClose(SDOclient);
87 
88  }
89 
90  pthread_mutex_unlock(&CO_CAN_VALID_mtx);
91 
92  return err;
93 }
94 
95 
97  CO_SDOclient_t *SDOclient,
98  uint8_t nodeID,
99  uint16_t idx,
100  uint8_t subidx,
101  uint8_t *dataTx,
102  uint32_t dataTxLen,
103  uint32_t *SDOabortCode,
104  uint16_t SDOtimeoutTime,
105  uint8_t blockTransferEnable)
106 {
107  int err = 0;
108 
109  /* stay here, if CAN is not configured */
110  pthread_mutex_lock(&CO_CAN_VALID_mtx);
111 
112 
113  /* Setup client. */
114  if(CO_SDOclient_setup(SDOclient, 0, 0, nodeID) != CO_SDOcli_ok_communicationEnd) {
115  err = 1;
116  }
117 
118  /* Initiate download. */
119  if(err == 0){
120  if(CO_SDOclientDownloadInitiate(SDOclient, idx, subidx, dataTx,
121  dataTxLen, blockTransferEnable) != CO_SDOcli_ok_communicationEnd)
122  {
123  err = 1;
124  }
125  }
126 
127  /* Download data. Loop in 5 ms intervals. */
128  if(err == 0){
129  CO_SDOclient_return_t ret;
130  uint16_t timer1msPrev;
131  struct timespec sleepTime;
132 
133  timer1msPrev = CO_timer1ms;
134  sleepTime.tv_sec = 0;
135  sleepTime.tv_nsec = 5000000;
136 
137  do {
138  uint16_t timer1ms, timer1msDiff;
139 
140  /* Calculate time difference */
141  timer1ms = CO_timer1ms;
142  timer1msDiff = timer1ms - timer1msPrev;
143  timer1msPrev = timer1ms;
144 
145  ret = CO_SDOclientDownload(SDOclient, timer1msDiff, SDOtimeoutTime, SDOabortCode);
146  nanosleep(&sleepTime, NULL);
147  } while(ret > 0);
148 
149  CO_SDOclientClose(SDOclient);
150 
151  }
152 
153  pthread_mutex_unlock(&CO_CAN_VALID_mtx);
154 
155  return err;
156 }
pthread_mutex_t CO_CAN_VALID_mtx
Testing ExoRobot new classes.
Definition: main.cpp:39
char ret[STRING_BUFFER_SIZE]
Definition: application.cpp:15
unsigned char uint8_t
Definition: CO_command.h:39
unsigned short uint16_t
Definition: CO_command.h:35
unsigned int uint32_t
Definition: CO_command.h:31
int sdoClientDownload(CO_SDOclient_t *SDOclient, uint8_t nodeID, uint16_t idx, uint8_t subidx, uint8_t *dataTx, uint32_t dataTxLen, uint32_t *SDOabortCode, uint16_t SDOtimeoutTime, uint8_t blockTransferEnable)
Definition: CO_master.c:96
volatile uint32_t CO_timer1ms
Definition: main.cpp:70
int sdoClientUpload(CO_SDOclient_t *SDOclient, uint8_t nodeID, uint16_t idx, uint8_t subidx, uint8_t *dataRx, uint32_t dataRxSize, uint32_t *dataRxLen, uint32_t *SDOabortCode, uint16_t SDOtimeoutTime, uint8_t blockTransferEnable)
Definition: CO_master.c:32