Now you can download a copy of these docs so you can use them offline! Download now
SendableGyro.cpp
00001 /*----------------------------------------------------------------------------*/ 00002 /* Copyright (c) FIRST 2011. All Rights Reserved. */ 00003 /* Open Source Software - may be modified and shared by FRC teams. The code */ 00004 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ 00005 /*----------------------------------------------------------------------------*/ 00006 00007 #include "SmartDashboard/SendableGyro.h" 00008 00009 #include "NetworkTables/NetworkTable.h" 00010 #include "WPIErrors.h" 00011 00012 #include <taskLib.h> 00013 00015 static const double kDefaultTimeBetweenUpdates = 0.2; 00016 static const char *kAngle = "angle"; 00017 00024 SendableGyro::SendableGyro(UINT8 moduleNumber, UINT32 channel) : 00025 Gyro(moduleNumber, channel), 00026 m_offset(0.0), 00027 m_period(kDefaultTimeBetweenUpdates), 00028 m_table(NULL), 00029 m_publisher("SendableGyroPublisher", (FUNCPTR)InitPublishTask), 00030 m_runPublisher(false) 00031 { 00032 } 00033 00041 SendableGyro::SendableGyro(UINT32 channel) : 00042 Gyro(channel), 00043 m_offset(0.0), 00044 m_period(kDefaultTimeBetweenUpdates), 00045 m_table(NULL), 00046 m_publisher("SendableGyroPublisher", (FUNCPTR)InitPublishTask), 00047 m_runPublisher(false) 00048 { 00049 } 00050 00057 SendableGyro::SendableGyro(AnalogChannel* channel): 00058 Gyro(channel), 00059 m_offset(0.0), 00060 m_period(kDefaultTimeBetweenUpdates), 00061 m_table(NULL), 00062 m_publisher("SendableGyroPublisher", (FUNCPTR)InitPublishTask), 00063 m_runPublisher(false) 00064 { 00065 } 00066 00067 SendableGyro::~SendableGyro() 00068 { 00069 if (m_table != NULL) 00070 { 00071 // Stop the task 00072 m_runPublisher = false; 00073 while(m_publisher.Verify()) 00074 taskDelay(10); 00075 00076 // Stop listening to the table 00077 m_table->RemoveChangeListener(kAngle, this); 00078 00079 delete m_table; 00080 m_table = NULL; 00081 } 00082 } 00083 00084 float SendableGyro::GetAngle() 00085 { 00086 return m_offset + Gyro::GetAngle(); 00087 } 00088 00089 void SendableGyro::Reset() 00090 { 00091 m_offset = 0.0; 00092 Gyro::Reset(); 00093 } 00094 00100 void SendableGyro::SetUpdatePeriod(double period) 00101 { 00102 if (period <= 0.0) 00103 wpi_setWPIErrorWithContext(ParameterOutOfRange, "period <= 0.0"); 00104 else 00105 m_period = period; 00106 } 00107 00114 double SendableGyro::GetUpdatePeriod() 00115 { 00116 return m_period; 00117 } 00118 00125 void SendableGyro::ResetToAngle(double angle) 00126 { 00127 m_offset = angle; 00128 Gyro::Reset(); 00129 } 00130 00131 NetworkTable *SendableGyro::GetTable() 00132 { 00133 if (m_table == NULL) 00134 { 00135 m_table = new NetworkTable(); 00136 m_table->PutInt(kAngle, (int)GetAngle()); 00137 m_table->AddChangeListener(kAngle, this); 00138 m_publisher.Start((UINT32)this); 00139 } 00140 return m_table; 00141 } 00142 00143 void SendableGyro::ValueChanged(NetworkTable *table, const char *name, NetworkTables_Types type) 00144 { 00145 // Update value from smart dashboard 00146 ResetToAngle(m_table->GetDouble(name)); 00147 } 00148 00149 void SendableGyro::PublishTaskRun() 00150 { 00151 m_runPublisher = true; 00152 while(m_runPublisher) 00153 { 00154 m_table->PutInt(kAngle, (int)GetAngle()); 00155 taskDelay((INT32)(m_period * 1000)); 00156 } 00157 }
Generated on Thu Jan 12 2012 22:35:23 for WPILibC++ by
1.7.1