AnimatLab  2
Test
HiSpike2.cpp
1 // HiSpike2.cpp: implementation of the HiSpike2 class.
2 //
4 
5 #include "StdAfx.h"
6 #include <stdarg.h>
7 #include "HiSpike2.h"
8 
9 namespace HybridInterfaceSim
10 {
11  namespace Robotics
12  {
13 
15 // Construction/Destruction
17 
18 HiSpike2::HiSpike2()
19 {
20  m_iPortNumber = 3;
21  m_iCounter = 0;
22  m_iInternalData = 0;
23  m_fltData = 0;
24 }
25 
26 HiSpike2::~HiSpike2()
27 {
28  try
29  {
30  }
31  catch(...)
32  {Std_TraceMsg(0, "Caught Error in desctructor of HiSpike2\r\n", "", -1, false, true);}
33 }
34 
35 void HiSpike2::PortNumber(int iPort)
36 {
37  Std_IsAboveMin((int) 0, iPort, true, "PortNumber", true);
38  m_iPortNumber = iPort;
39 }
40 
41 int HiSpike2::PortNumber() {return m_iPortNumber;}
42 
43 #pragma region DataAccesMethods
44 
45 float *HiSpike2::GetDataPointer(const std::string &strDataType)
46 {
47  std::string strType = Std_CheckString(strDataType);
48 
49  if(strType == "DATA")
50  return &m_fltData;
51 
52  return RemoteControl::GetDataPointer(strDataType);
53 }
54 
55 bool HiSpike2::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
56 {
57  std::string strType = Std_CheckString(strDataType);
58 
59  if(RemoteControl::SetData(strDataType, strValue, false))
60  return true;
61 
62  if(strType == "PORTNUMBER")
63  {
64  PortNumber((int) atoi(strValue.c_str()));
65  return true;
66  }
67 
68  //If it was not one of those above then we have a problem.
69  if(bThrowError)
70  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
71 
72  return false;
73 }
74 
75 void HiSpike2::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
76 {
77  RemoteControl::QueryProperties(aryProperties);
78 
79  aryProperties.Add(new TypeProperty("Data", AnimatPropertyType::Integer, AnimatPropertyDirection::Get));
80 
81  aryProperties.Add(new TypeProperty("PortNumber", AnimatPropertyType::Integer, AnimatPropertyDirection::Set));
82 }
83 
84 #pragma endregion
85 
87 {
88  OpenIO();
89 
90  StartIOThread();
91 
92  RemoteControl::Initialize();
93 }
94 
96 {
97  RemoteControl::ResetSimulation();
98 
99  m_iCounter = 0;
100  m_iInternalData = 0;
101  m_fltData = 0;
102 }
103 
104 bool HiSpike2::OpenIO()
105 {
106  //Open spike 2 communications ports here
107  return true;
108 }
109 
110 void HiSpike2::CloseIO()
111 {
112  //Close spike 2 communications ports here
113 }
114 
116 {
117  if(m_bEnabled && !m_lpSim->Paused())
118  {
119 
120  //This is just test code to demonstrate how it will only fire when m_fltData matches
121  //a speicfic value. You just need to set m_fltData to the unsigned int value that you
122  //get from spike 2. We are using floats here instead of ints because of the generic
123  //method I use within animatlab to get data.
124  m_fltData = 0;
125  m_iCounter++;
126 
127  if(m_iCounter == 10)
128  {
129  m_iInternalData++;
130  m_fltData = (float) m_iInternalData;
131  m_iCounter = 0;
132  }
133 
134  if(m_iInternalData == 10)
135  m_iInternalData = 0;
136  }
137 
138  //Temp code to slow this down like there was real communication
139  boost::this_thread::sleep(boost::posix_time::microseconds(1000));
140 
141  RemoteControl::StepIO();
142 }
143 
145 {
146  RemoteControl::Load(oXml);
147 
148  oXml.IntoElem();
149  PortNumber(oXml.GetChildInt("PortNumber", m_iPortNumber));
150  oXml.OutOfElem();
151 }
152 
153 
154  } // Robotics
155 } //HybridInterfaceSim
156 
virtual void StepIO()
This method is called from within the IO thread. It calls StepIO for each part.
Definition: HiSpike2.cpp:115
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.
Definition: HiSpike2.cpp:55
Simulator * m_lpSim
The pointer to a Simulation.
Definition: AnimatBase.h:43
virtual bool Paused()
Gets whether the Simulation is paused.
Definition: Simulator.cpp:347
virtual bool IntoElem()
Goes into the next element where the cursor is located.
Definition: StdXml.cpp:42
virtual void Load(StdUtils::CStdXml &oXml)
Loads the item using an XML data packet.
Definition: HiSpike2.cpp:144
virtual float * GetDataPointer(const std::string &strDataType)
Returns a float pointer to a data item of interest in this object.
Definition: HiSpike2.cpp:45
virtual int GetChildInt(std::string strElementName)
Gets an integer value from the element with the specified name.
Definition: StdXml.cpp:456
bool Std_IsAboveMin(int iMinVal, int iVal, bool bThrowError, std::string strParamName, bool bInclusiveLimit)
Tests if a number is above a minimum value.
A standard xml manipulation class.
Definition: StdXml.h:19
virtual void Initialize()
Initializes this object.
Definition: HiSpike2.cpp:86
void Std_TraceMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bLogToFile, bool bPrintHeader)
Traces a message to the debugger window.
bool m_bEnabled
Tells if this item is enabled or not. If it is not enabled then it is not run.
Definition: AnimatBase.h:40
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
virtual void ResetSimulation()
Resets the simulation back to time 0.
Definition: HiSpike2.cpp:95