Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2014. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ |
| 5 | /*----------------------------------------------------------------------------*/ |
| 6 | |
| 7 | #pragma once |
| 8 | |
| 9 | #include <thread> |
| 10 | #include <string> |
| 11 | #include "HAL/cpp/priority_mutex.h" |
| 12 | |
| 13 | #include "ErrorBase.h" |
| 14 | #include "Vision/ColorImage.h" |
| 15 | #include "Vision/HSLImage.h" |
| 16 | #include "nivision.h" |
| 17 | |
| 18 | /** |
| 19 | * Axis M1011 network camera |
| 20 | */ |
| 21 | class AxisCamera : public ErrorBase { |
| 22 | public: |
| 23 | enum WhiteBalance { |
| 24 | kWhiteBalance_Automatic, |
| 25 | kWhiteBalance_Hold, |
| 26 | kWhiteBalance_FixedOutdoor1, |
| 27 | kWhiteBalance_FixedOutdoor2, |
| 28 | kWhiteBalance_FixedIndoor, |
| 29 | kWhiteBalance_FixedFluorescent1, |
| 30 | kWhiteBalance_FixedFluorescent2 |
| 31 | }; |
| 32 | |
| 33 | enum ExposureControl { |
| 34 | kExposureControl_Automatic, |
| 35 | kExposureControl_Hold, |
| 36 | kExposureControl_FlickerFree50Hz, |
| 37 | kExposureControl_FlickerFree60Hz |
| 38 | }; |
| 39 | |
| 40 | enum Resolution { |
| 41 | kResolution_640x480, |
| 42 | kResolution_480x360, |
| 43 | kResolution_320x240, |
| 44 | kResolution_240x180, |
| 45 | kResolution_176x144, |
| 46 | kResolution_160x120, |
| 47 | }; |
| 48 | |
| 49 | enum Rotation { kRotation_0, kRotation_180 }; |
| 50 | |
| 51 | explicit AxisCamera(std::string const &cameraHost); |
| 52 | virtual ~AxisCamera(); |
| 53 | |
| 54 | AxisCamera(const AxisCamera&) = delete; |
| 55 | AxisCamera& operator=(const AxisCamera&) = delete; |
| 56 | |
| 57 | bool IsFreshImage() const; |
| 58 | |
| 59 | int GetImage(Image *image); |
| 60 | int GetImage(ColorImage *image); |
| 61 | HSLImage *GetImage(); |
| 62 | int CopyJPEG(char **destImage, unsigned int &destImageSize, |
| 63 | unsigned int &destImageBufferSize); |
| 64 | |
| 65 | void WriteBrightness(int brightness); |
| 66 | int GetBrightness(); |
| 67 | |
| 68 | void WriteWhiteBalance(WhiteBalance whiteBalance); |
| 69 | WhiteBalance GetWhiteBalance(); |
| 70 | |
| 71 | void WriteColorLevel(int colorLevel); |
| 72 | int GetColorLevel(); |
| 73 | |
| 74 | void WriteExposureControl(ExposureControl exposureControl); |
| 75 | ExposureControl GetExposureControl(); |
| 76 | |
| 77 | void WriteExposurePriority(int exposurePriority); |
| 78 | int GetExposurePriority(); |
| 79 | |
| 80 | void WriteMaxFPS(int maxFPS); |
| 81 | int GetMaxFPS(); |
| 82 | |
| 83 | void WriteResolution(Resolution resolution); |
| 84 | Resolution GetResolution(); |
| 85 | |
| 86 | void WriteCompression(int compression); |
| 87 | int GetCompression(); |
| 88 | |
| 89 | void WriteRotation(Rotation rotation); |
| 90 | Rotation GetRotation(); |
| 91 | |
| 92 | private: |
| 93 | std::thread m_captureThread; |
| 94 | std::string m_cameraHost; |
| 95 | int m_cameraSocket = -1; |
| 96 | priority_mutex m_captureMutex; |
| 97 | |
| 98 | priority_mutex m_imageDataMutex; |
| 99 | std::vector<uint8_t> m_imageData; |
| 100 | bool m_freshImage = false; |
| 101 | |
| 102 | int m_brightness = 50; |
| 103 | WhiteBalance m_whiteBalance = kWhiteBalance_Automatic; |
| 104 | int m_colorLevel = 50; |
| 105 | ExposureControl m_exposureControl = kExposureControl_Automatic; |
| 106 | int m_exposurePriority = 50; |
| 107 | int m_maxFPS = 0; |
| 108 | Resolution m_resolution = kResolution_640x480; |
| 109 | int m_compression = 50; |
| 110 | Rotation m_rotation = kRotation_0; |
| 111 | bool m_parametersDirty = true; |
| 112 | bool m_streamDirty = true; |
| 113 | priority_mutex m_parametersMutex; |
| 114 | |
| 115 | bool m_done = false; |
| 116 | |
| 117 | void Capture(); |
| 118 | void ReadImagesFromCamera(); |
| 119 | bool WriteParameters(); |
| 120 | |
| 121 | int CreateCameraSocket(std::string const &requestString, bool setError); |
| 122 | }; |