Ravens Robotics FRC 2015
Camera.cpp
Go to the documentation of this file.
1 
10 #include <Camera.h>
11 #include <algorithm> // std::min, std::max
12 
14 {
15  m_Camera = NULL;
16  m_Stick = NULL;
17  m_CameraServoPosition = kCameraFirstPos;
18  m_CameraMove = 0;
19 }
20 
22  // Delete allocated member objects
23  delete m_Camera; m_Camera = NULL;
24  delete m_Stick; m_Stick = NULL;
25 }
26 
27 /*
28  * Called once, to perform initialization that isn't appropriate for constructor.
29  */
31  // Initialize Camera & joystick
32  if (!m_Camera) m_Camera = new Servo(kCameraChannel);
33  if (!m_Stick) m_Stick = new Joystick(kStickChannel);
34 
35  // Move camera servo to initial position
37 }
38 
39 /*
40  * Component-specific sensory input collector.
41  * Read state of camera control buttons and set
42  * move command accordingly.
43  */
44 void Camera::Sense() {
45  // Store move command; +1 for up, -1 for down, 0 for just hanging around
46  m_CameraMove =
47  m_Stick->GetRawButton(kCameraUp) ? 1 :
48  m_Stick->GetRawButton(kCameraDown) ? -1 : 0;
49 }
50 
51 /*
52  * Component-specific worker function.
53  * Process the move command received by Sense(), ensuring
54  * camera servo remains within motion bounds.
55  * Report operation to SmartDashboard.
56  */
58  static const uint32_t kNoMove = 0;
59 
60  // Apply most recent move command.
61  float newPosition = m_CameraServoPosition + m_CameraMove * kCameraIncrement;
62 
63  /*
64  * Validate move, it must be within bounds.
65  * Test the upper bounds by picking the min of the new position or range max,
66  * then the lower bounds by picking the max of the new position or range min.
67  */
68  float temp = 0.0; // trick to get around constexpr rules
69  newPosition = std::min(newPosition,temp=kMaxCameraServoPosition);
70  newPosition = std::max(newPosition,temp=kMinCameraServoPosition);
71 
72  /*
73  * Operate camera servo if move is within bounds.
74  * If the new position is the same as the old one, then cancel move command.
75  */
76  m_CameraMove = (newPosition == m_CameraServoPosition) ? kNoMove : m_CameraMove;
77  if (m_CameraMove) {
78  m_Camera->Set(m_CameraServoPosition = newPosition);
79  }
80 
81  // Report operation
82  // Todo: this should be made optional, with some way to set on / off.
83  std::string pan; // to report camera motion
84  switch (m_CameraMove) {
85  case -1:
86  pan.assign("Pan down");
87  break;
88  case +1:
89  pan.assign("Pan up");
90  break;
91  default:
92  pan.assign("Stationary");
93  break;
94  }
95 
96  SmartDashboard::PutString("Camera", pan);
97 
98  // Clear move command for this iteration
99  m_CameraMove = kNoMove;
100 
101  return;
102 }
Joystick * m_Stick
joystick controller
Definition: Camera.h:57
const int kStickChannel
Auxiliary joystick Channel TODO: rename after arms merged.
Camera()
Definition: Camera.cpp:13
virtual void ComponentInit()
Definition: Camera.cpp:30
float m_CameraServoPosition
current camera servo position
Definition: Camera.h:60
virtual void DoWork()
Definition: Camera.cpp:57
uint32_t m_CameraMove
command value received
Definition: Camera.h:61
virtual ~Camera()
Definition: Camera.cpp:21
virtual void Sense()
Definition: Camera.cpp:44
Header file for Camera component.
Servo * m_Camera
camera servo instance
Definition: Camera.h:58