7 #include "Vision/AxisCameraParams.h"
9 #include "Vision/AxisCamera.h"
14 #include "Synchronized.h"
17 #include "WPIErrors.h"
19 #if JAVA_CAMERA_LIB != 1
20 #include "DriverStation.h"
23 static const char *
const kRotationChoices[] = {
"0",
"180"};
24 static const char *
const kResolutionChoices[] = {
"640x480",
"640x360",
"320x240",
"160x120"};
25 static const char *
const kExposureControlChoices[] = {
"automatic",
"hold",
"flickerfree50",
"flickerfree60" };
26 static const char *
const kWhiteBalanceChoices[] = {
"auto",
"holdwb",
"fixed_outdoor1",
27 "fixed_outdoor2",
"fixed_indoor",
"fixed_fluor1",
"fixed_fluor2" };
33 : m_paramTask(
"paramTask", (FUNCPTR) s_ParamTaskFunction)
34 , m_paramChangedSem (NULL)
35 , m_socketPossessionSem (NULL)
36 , m_brightnessParam (NULL)
37 , m_compressionParam (NULL)
38 , m_exposurePriorityParam (NULL)
39 , m_colorLevelParam (NULL)
40 , m_maxFPSParam (NULL)
41 , m_rotationParam (NULL)
42 , m_resolutionParam (NULL)
43 , m_exposureControlParam (NULL)
44 , m_whiteBalanceParam (NULL)
46 if (ipAddress == NULL || strlen(ipAddress) == 0)
48 #if JAVA_CAMERA_LIB == 1
49 wpi_setWPIErrorWithContext(ParameterOutOfRange,
"IP Address must be specified");
56 snprintf(cameraIP, 16,
"10.%d.%d.11", teamNumber / 100, teamNumber % 100);
57 m_ipAddress = inet_addr(cameraIP);
62 m_ipAddress = inet_addr((
char*)ipAddress);
65 if (m_ipAddress == (u_long)ERROR)
72 "root.ImageSource.I0.Sensor.Brightness=(.*)",
false);
73 m_parameters.push_back(m_brightnessParam);
75 "root.ImageSource.I0.Sensor.ColorLevel=(.*)",
false);
76 m_parameters.push_back(m_colorLevelParam);
77 m_exposurePriorityParam =
new IntCameraParameter(
"ImageSource.I0.Sensor.exposurePriority=%i",
78 "root.ImageSource.I0.Sensor.ExposurePriority=(.*)",
false);
79 m_parameters.push_back(m_exposurePriorityParam);
81 "root.Image.I0.Appearance.Compression=(.*)",
true);
82 m_parameters.push_back(m_compressionParam);
84 "root.Image.I0.Stream.FPS=(.*)",
false);
85 m_parameters.push_back(m_maxFPSParam);
87 "root.Image.I0.Appearance.Rotation=(.*)",
true, kRotationChoices,
sizeof(kRotationChoices)/
sizeof(kRotationChoices[0]));
88 m_parameters.push_back(m_rotationParam);
90 "root.Image.I0.Appearance.Resolution=(.*)",
true, kResolutionChoices,
sizeof(kResolutionChoices)/
sizeof(kResolutionChoices[0]));
91 m_parameters.push_back(m_resolutionParam);
93 "root.ImageSource.I0.Sensor.Exposure=(.*)",
false, kExposureControlChoices,
sizeof(kExposureControlChoices)/
sizeof(kExposureControlChoices[0]));
94 m_parameters.push_back(m_exposureControlParam);
96 "root.ImageSource.I0.Sensor.WhiteBalance=(.*)",
false, kWhiteBalanceChoices,
sizeof(kWhiteBalanceChoices)/
sizeof(kWhiteBalanceChoices[0]));
97 m_parameters.push_back(m_whiteBalanceParam);
99 m_paramChangedSem = semBCreate (SEM_Q_PRIORITY, SEM_EMPTY);
100 m_socketPossessionSem = semBCreate (SEM_Q_PRIORITY, SEM_FULL);
102 m_paramTask.
Start((
int)
this);
112 semDelete(m_socketPossessionSem);
113 semDelete(m_paramChangedSem);
115 delete m_whiteBalanceParam;
116 delete m_exposureControlParam;
117 delete m_resolutionParam;
118 delete m_rotationParam;
119 delete m_maxFPSParam;
120 delete m_compressionParam;
121 delete m_exposurePriorityParam;
122 delete m_colorLevelParam;
123 delete m_brightnessParam;
142 static bool firstTime =
true;
146 semTake(m_socketPossessionSem, WAIT_FOREVER);
152 bool restartRequired =
false;
154 ParameterVector_t::iterator it = m_parameters.begin();
155 ParameterVector_t::iterator end = m_parameters.end();
156 for(; it != end; it++)
158 bool changed =
false;
160 restartRequired |= (*it)->CheckChanged(changed, param);
170 semGive(m_socketPossessionSem);
181 m_brightnessParam->
SetValue(brightness);
182 semGive(m_paramChangedSem);
191 return m_brightnessParam->
GetValue();
200 m_whiteBalanceParam->
SetValue(whiteBalance);
201 semGive(m_paramChangedSem);
210 return (WhiteBalance_t) m_whiteBalanceParam->
GetValue();
219 m_colorLevelParam->
SetValue(colorLevel);
220 semGive(m_paramChangedSem);
229 return m_colorLevelParam->
GetValue();
238 m_exposureControlParam->
SetValue(exposureControl);
239 semGive(m_paramChangedSem);
248 return (Exposure_t) m_exposureControlParam->
GetValue();
257 m_resolutionParam->
SetValue(resolution);
258 semGive(m_paramChangedSem);
267 return (Resolution_t) m_resolutionParam->
GetValue();
279 m_exposurePriorityParam->
SetValue(exposurePriority);
280 semGive(m_paramChangedSem);
283 int AxisCameraParams::GetExposurePriority()
285 return m_exposurePriorityParam->
GetValue();
295 m_rotationParam->
SetValue(rotation);
296 semGive(m_paramChangedSem);
305 return (Rotation_t) m_rotationParam->
GetValue();
315 m_compressionParam->
SetValue(compression);
316 semGive(m_paramChangedSem);
325 return m_compressionParam->
GetValue();
336 semGive(m_paramChangedSem);
356 const char *requestString =
357 "GET /axis-cgi/admin/param.cgi?action=update&%s HTTP/1.1\n\
358 User-Agent: HTTPStreamClient\n\
359 Connection: Keep-Alive\n\
360 Cache-Control: no-cache\n\
361 Authorization: Basic RlJDOkZSQw==\n\n";
362 char completedRequest[1024];
363 sprintf(completedRequest, requestString, param);
365 int camSocket = CreateCameraSocket(completedRequest);
366 if (camSocket == ERROR)
368 printf(
"UpdateCamParam failed: %s\n", param);
381 const char * requestString =
382 "GET /axis-cgi/admin/param.cgi?action=list HTTP/1.1\n\
383 User-Agent: HTTPStreamClient\n\
384 Connection: Keep-Alive\n\
385 Cache-Control: no-cache\n\
386 Authorization: Basic RlJDOkZSQw==\n\n";
388 int camSocket = CreateCameraSocket(requestString);
389 if (camSocket == ERROR)
394 char *readBuffer =
new char[27000];
398 wpi_assert(totalRead < 26000);
399 int bytesRead = recv(camSocket, &readBuffer[totalRead], 1000, 0);
400 if (bytesRead == ERROR)
402 wpi_setErrnoErrorWithContext(
"Failed to read image header");
406 else if (bytesRead <= 0)
410 totalRead += bytesRead;
412 readBuffer[totalRead] =
'\0';
414 ParameterVector_t::iterator it = m_parameters.begin();
415 ParameterVector_t::iterator end = m_parameters.end();
416 for(; it != end; it++)
418 (*it)->GetParamFromString(readBuffer, totalRead);
421 delete [] readBuffer;
431 int AxisCameraParams::CreateCameraSocket(
const char *requestString)
434 struct sockaddr_in serverAddr;
437 if ((camSocket = socket(AF_INET, SOCK_STREAM, 0)) == ERROR)
439 wpi_setErrnoErrorWithContext(
"Failed to create the camera socket");
443 sockAddrSize =
sizeof(
struct sockaddr_in);
444 bzero((
char *) &serverAddr, sockAddrSize);
445 serverAddr.sin_family = AF_INET;
446 serverAddr.sin_len = (u_char) sockAddrSize;
447 serverAddr.sin_port = htons(80);
449 serverAddr.sin_addr.s_addr = m_ipAddress;
452 struct timeval connectTimeout;
453 connectTimeout.tv_sec = 5;
454 connectTimeout.tv_usec = 0;
455 if (connectWithTimeout(camSocket, (
struct sockaddr *) &serverAddr, sockAddrSize, &connectTimeout) == ERROR)
457 wpi_setErrnoErrorWithContext(
"Failed to connect to the camera");
461 int sent = send(camSocket, requestString, strlen(requestString), 0);
464 wpi_setErrnoErrorWithContext(
"Failed to send a request to the camera");
void WriteExposurePriority(int)
bool Start(uint32_t arg0=0, uint32_t arg1=0, uint32_t arg2=0, uint32_t arg3=0, uint32_t arg4=0, uint32_t arg5=0, uint32_t arg6=0, uint32_t arg7=0, uint32_t arg8=0, uint32_t arg9=0)
void WriteCompression(int)
Resolution_t GetResolution()
AxisCameraParams(const char *ipAddress)
void WriteBrightness(int)
void WriteExposureControl(Exposure_t)
static int s_ParamTaskFunction(AxisCameraParams *thisPtr)
Exposure_t GetExposureControl()
WhiteBalance_t GetWhiteBalance()
void WriteResolution(Resolution_t)
static DriverStation * GetInstance()
void WriteWhiteBalance(WhiteBalance_t whiteBalance)
void WriteColorLevel(int)
int UpdateCamParam(const char *param)
void WriteRotation(Rotation_t)
virtual ~AxisCameraParams()