forked from OpenPHDGuiding/phd2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcam_Starfish.cpp
257 lines (229 loc) · 9.91 KB
/
cam_Starfish.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* cam_Starfish.cpp
* PHD Guiding
*
* Created by Craig Stark.
* Copyright (c) 2007-2010 Craig Stark.
* All rights reserved.
*
* This source code is distributed under the following "BSD" license
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of Craig Stark, Stark Labs nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "phd.h"
#if defined (STARFISH)
#include "camera.h"
#include "time.h"
#include "image_math.h"
#include "cam_Starfish.h"
#if defined (__WINDOWS__)
#define kIOReturnSuccess 0
typedef int IOReturn;
using namespace FcCamSpace;
void fcUsb_init(void) { FcCamFuncs::fcUsb_init(); }
void fcUsb_close(void) { FcCamFuncs::fcUsb_close(); }
void fcUsb_CloseCameraDriver(void) { FcCamFuncs::fcUsb_close(); }
int fcUsb_FindCameras(void) { return FcCamFuncs::fcUsb_FindCameras(); }
int fcUsb_cmd_setRegister(int camNum, UInt16 regAddress, UInt16 dataValue) { return FcCamFuncs::fcUsb_cmd_setRegister(camNum, regAddress, dataValue); }
UInt16 fcUsb_cmd_getRegister(int camNum, UInt16 regAddress) { return FcCamFuncs::fcUsb_cmd_getRegister(camNum, regAddress); }
int fcUsb_cmd_setIntegrationTime(int camNum, UInt32 theTime) { return FcCamFuncs::fcUsb_cmd_setIntegrationTime(camNum, theTime); }
int fcUsb_cmd_startExposure(int camNum) { return FcCamFuncs::fcUsb_cmd_startExposure(camNum); }
int fcUsb_cmd_abortExposure(int camNum) { return FcCamFuncs::fcUsb_cmd_abortExposure(camNum); }
UInt16 fcUsb_cmd_getState(int camNum) { return FcCamFuncs::fcUsb_cmd_getState(camNum); }
int fcUsb_cmd_setRoi(int camNum, UInt16 left, UInt16 top, UInt16 right, UInt16 bottom) { return FcCamFuncs::fcUsb_cmd_setRoi(camNum,left,top,right,bottom); }
int fcUsb_cmd_setRelay(int camNum, int whichRelay) { return FcCamFuncs::fcUsb_cmd_setRelay(camNum,whichRelay); }
int fcUsb_cmd_clearRelay(int camNum, int whichRelay) { return FcCamFuncs::fcUsb_cmd_clearRelay(camNum,whichRelay); }
int fcUsb_cmd_pulseRelay(int camNum, int whichRelay, int onMs, int offMs, bool repeats) { return FcCamFuncs::fcUsb_cmd_pulseRelay(camNum,whichRelay, onMs, offMs, repeats); }
int fcUsb_cmd_setTemperature(int camNum, SInt16 theTemp) { return FcCamFuncs::fcUsb_cmd_setTemperature(camNum,theTemp); }
bool fcUsb_cmd_getTECInPowerOK(int camNum) { return FcCamFuncs::fcUsb_cmd_getTECInPowerOK(camNum); }
int fcUsb_cmd_getRawFrame(int camNum, UInt16 numRows, UInt16 numCols, UInt16 *frameBuffer) { return FcCamFuncs::fcUsb_cmd_getRawFrame(camNum,numRows,numCols, frameBuffer); }
int fcUsb_cmd_setReadMode(int camNum, int DataXfrReadMode, int DataFormat) { return FcCamFuncs::fcUsb_cmd_setReadMode(camNum,DataXfrReadMode,DataFormat); }
bool fcUsb_haveCamera(void) { return FcCamFuncs::fcUsb_haveCamera(); }
#endif
Camera_StarfishClass::Camera_StarfishClass()
{
Connected = false;
Name = _T("Fishcamp Starfish");
FullSize = wxSize(1280,1024);
HasSubframes = true;
HasGainControl = true;
m_hasGuideOutput = true;
DriverLoaded = false;
}
wxByte Camera_StarfishClass::BitsPerPixel()
{
return 16;
}
bool Camera_StarfishClass::Connect(const wxString& camId)
{
// returns true on error
IOReturn rval;
wxBeginBusyCursor();
if (!DriverLoaded) {
fcUsb_init(); // Init the driver
DriverLoaded = true;
}
NCams = fcUsb_FindCameras();
int i = NCams;
wxEndBusyCursor();
if (NCams == 0)
return true;
else {
CamNum = 1; // Assume just the one cam for now
// set to polling mode and turn off black adjustment but turn on auto balancing of the offsets in the 2x2 matrix
rval = fcUsb_cmd_setReadMode(CamNum, fc_classicDataXfr, fc_16b_data);
if (rval != kIOReturnSuccess) return true;
if (fcUsb_cmd_getTECInPowerOK(CamNum))
fcUsb_cmd_setTemperature(CamNum,10);
}
lastSubFrame = wxRect(wxSize(1, 1)); // To differentiate between full-frame "null" subframe
Connected = true;
return false;
}
bool Camera_StarfishClass::Disconnect()
{
if (fcUsb_haveCamera())
fcUsb_CloseCameraDriver();
Connected = false;
return false;
}
void Camera_StarfishClass::InitCapture()
{
// Set gain
unsigned short Gain = (unsigned short) GuideCameraGain;
if (Gain < 25 ) { // Low noise 1x-4x in .125x mode maps on 0-24
Gain = 8 + Gain; //103
}
else if (Gain < 57) { // 4.25x-8x in .25x steps maps onto 25-56
Gain = 0x51 + (Gain - 25)/2; // 81-96 aka 0x51-0x60
}
else { // 9x-15x in 1x steps maps onto 57-95
Gain = 0x61 + (Gain - 57)/6;
}
if (Gain > 0x67) Gain = 0x67;
fcUsb_cmd_setRegister(CamNum,0x35,Gain);
}
static bool StopExposure(int camNum)
{
Debug.AddLine("Starfish: StopExposure");
int ret = fcUsb_cmd_abortExposure(camNum);
return ret == kIOReturnSuccess;
}
bool Camera_StarfishClass::Capture(int duration, usImage& img, int options, const wxRect& subframe)
{
bool debug = true;
int xsize, ysize, xpos, ypos;
bool usingSubFrames = UseSubframes && subframe.GetWidth() > 0 && subframe.GetHeight() > 0;
IOReturn rval;
// init memory
if (img.Init(FullSize)) {
DisconnectWithAlert(CAPT_FAIL_MEMORY);
return true;
}
if (usingSubFrames)
{
xsize = subframe.GetWidth();
ysize = subframe.GetHeight();
xpos = subframe.GetLeft();
ypos = subframe.GetTop();
subImage.Init(xsize, ysize);
subImage.Clear();
}
else
{
xsize = FullSize.GetWidth();
ysize = FullSize.GetHeight();
xpos = 0;
ypos = 0;
}
img.Clear();
// set ROI if something has changed
if (lastSubFrame != subframe)
{
rval = fcUsb_cmd_setRoi(CamNum, (unsigned short)xpos, (unsigned short)ypos, (unsigned short)(xpos + xsize - 1), (unsigned short)(ypos + ysize - 1));
//Debug.AddLine(wxString::Format("Starfish: using %s", usingSubFrames ? " sub-frames" : " full frames"));
//Debug.AddLine(wxString::Format("Starfish: ROI set from {%d,%d} to {%d,%d)", xpos, ypos, xpos + xsize - 1, ypos + ysize - 1));
if (rval != kIOReturnSuccess) { if (debug) pFrame->Alert(_T("Starfish Err 1")); return true; }
lastSubFrame = subframe;
}
// set duration
fcUsb_cmd_setIntegrationTime(CamNum, (unsigned int) duration);
rval = fcUsb_cmd_startExposure(CamNum);
if (rval != kIOReturnSuccess) { if (debug) pFrame->Alert(_T("Starfish Err 2")); return true; }
CameraWatchdog watchdog(duration, GetTimeoutMs());
if (duration > 100)
{
// wait until near end of exposure
if (WorkerThread::MilliSleep(duration - 100, WorkerThread::INT_ANY) &&
(WorkerThread::TerminateRequested() || StopExposure(CamNum)))
{
return true;
}
}
// wait for image to finish and d/l
while (fcUsb_cmd_getState(CamNum) != 0)
{
wxMilliSleep(50);
if (WorkerThread::InterruptRequested() &&
(WorkerThread::TerminateRequested() || StopExposure(CamNum)))
{
return true;
}
if (watchdog.Expired())
{
DisconnectWithAlert(CAPT_FAIL_TIMEOUT);
return true;
}
}
if (usingSubFrames)
{
rval = fcUsb_cmd_getRawFrame(CamNum, (unsigned short)ysize, (unsigned short)xsize, subImage.ImageData);
// Transfer the subframe to the corresponding location in the full-size frame
for (int y = 0; y < ysize; y++)
{
const unsigned short *pSrc = subImage.ImageData + y * xsize;
unsigned short *pDest = img.ImageData + (ypos + y) * FullSize.GetWidth() + xpos;
memcpy(pDest, pSrc, xsize * sizeof(unsigned short));
}
img.Subframe = subframe;
}
else
{
rval = fcUsb_cmd_getRawFrame(CamNum, (unsigned short)ysize, (unsigned short)xsize, img.ImageData);
}
/* if (rval != kIOReturnSuccess) {
if (debug) pFrame->Alert(wxString::Format("Starfish Err 3 %d",rval));
//return true;
}*/
if (options & CAPTURE_SUBTRACT_DARK) SubtractDark(img);
return false;
}
bool Camera_StarfishClass::ST4PulseGuideScope(int direction, int duration) {
if (direction == WEST) direction = EAST; // my ENUM and theirs are flipped
else if (direction == EAST) direction = WEST;
if (fcUsb_cmd_pulseRelay(CamNum, direction, duration, 0, false) != kIOReturnSuccess)
return true;
return false;
}
#endif