iCub-main
fakeBoard.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 RobotCub Consortium
3  * Author: Lorenzo Natale
4  * CopyPolicy: Released under the terms of the GNU GPL v2.0.
5  *
6  */
7 
8 #include "fakeBoard.h"
9 #define CAN_BCAST_POSITION 0x001
10 
11 #include <iostream>
12 #include <stdlib.h>
13 
14 using namespace std;
15 
16 FakeBoard::FakeBoard(int id, int p):yarp::os::PeriodicThread((double)p/1000.0)
17 {
18  canId=id;
19  outMessages=0;
20 }
21 
23 {
24 
25 }
26 
28 {
29  //pop from list of messages
30  inMessages.lock();
31  MsgIt it=inMessages.begin();
32 
33  while(it!=inMessages.end())
34  {
35  FCMSG &m=(*it);
36  if ((m.id&0x0f)==canId)
37  {
38  // fprintf(stderr, "%d rec %d %d\n", canId, (*it).id, (*it).data[0]);
39  //got one message
40  FCMSG reply;
41  // int ch = (m.data[0]&&0x80)?1:0;
42 
43  reply.id=0;
44  reply.id=(canId<<4);
45  reply.len=8;
46 
47  reply.data[0]=m.data[0];
48 
49 #if 0
50  if ((m.data[0]&&0xef)==CAN_GET_P_GAIN)
51  {
52 
53  }
54  if ((m.data[0]&&0xef)==CAN_GET_D_GAIN)
55  {
56 
57  }
58  if ((m.data[0]&&0xef)==CAN_GET_I_GAIN)
59  {
60 
61  }
62  if ((m.data[0]&&0xef)==CAN_GET_ILIM_GAIN)
63  {
64 
65  }
66  if ((m.data[0]&&0xef)==CAN_GET_SCALE_GAIN)
67  {
68 
69  }
70  if ((m.data[0]&&0xef)==CAN_GET_OFFSET_GAIN)
71  {
72 
73  }
74  if ((m.data[0]&&0xef)==CAN_GET_TLIM_GAIN)
75  {
76 
77  }
78 #endif
79 
80  outMessages->lock();
81  outMessages->push_back(reply);
82  outMessages->unlock();
83  }
84  it++;
85 
86  //TODO:push replies
87  }
88 
89  inMessages.clear();
90  inMessages.unlock();
91 
92 
93  // bcast
94  FCMSG reply;
95  int c=0;
96  outMessages->lock();
97 
98  reply.id=canId<<4;
99  reply.id=reply.id|0x100; //bcast
100  reply.id=reply.id|CAN_BCAST_POSITION; //position
101  reply.data[0]=static_cast<char>((100*(1-0.5*rand()/(double)RAND_MAX)+0.5));
102  reply.data[1]=0;
103  reply.data[2]=0;
104  reply.data[3]=0;
105 
106  reply.data[4]=static_cast<char>((100*(1-0.5*rand()/(double)RAND_MAX)+0.5));
107  reply.data[5]=0;
108  reply.data[6]=0;
109  reply.data[7]=0;
110  reply.len=8;
111 
112  outMessages->push_back(reply);
113 
114  //status
115  reply.id=0;
116  reply.id=canId<<4;
117  reply.id=reply.id|0x100; //bcast
118  reply.id=reply.id|0x003; //status message
119  reply.len=8;
120  for(int k=0;k<8;k++)
121  reply.data[k]=0;
122  outMessages->push_back(reply);
123  //fprintf(stderr, "%d %.4x\n", canId, reply.id);
124  outMessages->unlock();
125 }
126 
128 {
129  fprintf(stderr, "Starting board %d\n", canId);
130  return true;
131 }
132 
134 {
135  fprintf(stderr, "Stopping board %d\n", canId);
136 }
bool threadInit()
Definition: fakeBoard.cpp:127
void threadRelease()
Definition: fakeBoard.cpp:133
void run()
Definition: fakeBoard.cpp:27
FakeBoard(int id=0, int p=100)
Definition: fakeBoard.cpp:16
void lock()
Definition: msgList.h:24
void unlock()
Definition: msgList.h:25
#define CAN_BCAST_POSITION
Definition: fakeBoard.cpp:9
std::list< FCMSG >::iterator MsgIt
Definition: msgList.h:17
Copyright (C) 2008 RobotCub Consortium.
fprintf(fid,'\n')
unsigned char data[8]