Skip to content

Commit db37864

Browse files
committed
usb: Add proper warmup sequence
1 parent 3085e67 commit db37864

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

include/internal/libfreenect2/protocol/command.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
#define KCMD_READ_FIRMWARE_VERSIONS 0x02
3333
#define KCMD_INIT_STREAMS 0x09
34-
#define KCMD_READ_DATA_0x14 0x14
34+
#define KCMD_READ_HARDWARE_INFO 0x14
3535
#define KCMD_READ_STATUS 0x16
3636
#define KCMD_READ_DATA_PAGE 0x22
3737
#define KCMD_READ_DATA_0x26 0x26
@@ -43,7 +43,8 @@
4343
#define KCMD_0x47 0x47
4444

4545
// observed in sensor stop/shutdown sequence
46-
#define KCMD_0x0A 0x0A
46+
#define KCMD_STOP 0x0A
47+
#define KCMD_SHUTDOWN 0x00
4748

4849
namespace libfreenect2
4950
{
@@ -166,7 +167,7 @@ struct CommandWith4Params : public Command<CommandId, MaxResponseLength, 4>
166167

167168
typedef CommandWith0Params<0x02, 0x200> ReadFirmwareVersionsCommand;
168169

169-
typedef CommandWith0Params<KCMD_READ_DATA_0x14, 0x5C> ReadData0x14Command;
170+
typedef CommandWith0Params<KCMD_READ_HARDWARE_INFO, 0x5C> ReadHardwareInfoCommand;
170171

171172
typedef CommandWith0Params<KCMD_INIT_STREAMS, 0x00> InitStreamsCommand;
172173

@@ -192,7 +193,8 @@ typedef CommandWith1Param<KCMD_SET_STREAMING, 0x00, 0x01> SetStreamEnabledComman
192193
typedef CommandWith4Params<KCMD_0x46, 0x00, 0x00, 0x00003840, 0x00000037, 0x00> Unknown0x46Command;
193194
typedef CommandWith0Params<KCMD_0x47, 0x10> Unknown0x47Command;
194195

195-
typedef CommandWith0Params<KCMD_0x0A, 0x00> Unknown0x0ACommand;
196+
typedef CommandWith0Params<KCMD_STOP, 0x00> StopCommand;
197+
typedef CommandWith0Params<KCMD_SHUTDOWN, 0x00> ShutdownCommand;
196198

197199
typedef CommandWith4Params<KCMD_SET_MODE, 0x00, 0x00> SetModeDisabledCommand;
198200
typedef CommandWith4Params<KCMD_SET_MODE, 0x00, 0x01> SetModeEnabledCommand;

src/libfreenect2.cpp

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -683,9 +683,9 @@ void Freenect2DeviceImpl::start()
683683
command_tx_.execute(ReadFirmwareVersionsCommand(nextCommandSeq()), firmware_result);
684684
firmware_ = FirmwareVersionResponse(firmware_result.data, firmware_result.length).toString();
685685

686-
command_tx_.execute(ReadData0x14Command(nextCommandSeq()), result);
687-
LOG_DEBUG << "ReadData0x14 response";
688-
LOG_DEBUG << GenericResponse(result.data, result.length).toString();
686+
command_tx_.execute(ReadHardwareInfoCommand(nextCommandSeq()), result);
687+
//The hardware version is currently useless. It is only used to select the
688+
//IR normalization table, but we don't have that.
689689

690690
command_tx_.execute(ReadSerialNumberCommand(nextCommandSeq()), serial_result);
691691
std::string new_serial = SerialNumberResponse(serial_result.data, serial_result.length).toString();
@@ -749,17 +749,28 @@ void Freenect2DeviceImpl::start()
749749
rgb_camera_params_.my_x0y0 = rgb_p->my_x0y0; // 1
750750
setColorCameraParams(rgb_camera_params_);
751751

752-
command_tx_.execute(ReadStatus0x090000Command(nextCommandSeq()), result);
753-
LOG_DEBUG << "ReadStatus0x090000 response";
754-
LOG_DEBUG << GenericResponse(result.data, result.length).toString();
752+
command_tx_.execute(SetModeEnabledWith0x00640064Command(nextCommandSeq()), result);
753+
command_tx_.execute(SetModeDisabledCommand(nextCommandSeq()), result);
754+
755+
for (uint32_t status = 0, last = 0; (status & 1) == 0; last = status)
756+
{
757+
command_tx_.execute(ReadStatus0x090000Command(nextCommandSeq()), result);
758+
if (result.length < sizeof(uint32_t))
759+
continue; //TODO should report error
760+
status = *reinterpret_cast<const uint32_t *>(result.data);
761+
if (status != last)
762+
LOG_DEBUG << "status 0x090000: " << status;
763+
if ((status & 1) == 0)
764+
this_thread::sleep_for(chrono::milliseconds(100));
765+
}
755766

756767
command_tx_.execute(InitStreamsCommand(nextCommandSeq()), result);
757768

758769
usb_control_.setIrInterfaceState(UsbControl::Enabled);
759770

760771
command_tx_.execute(ReadStatus0x090000Command(nextCommandSeq()), result);
761-
LOG_DEBUG << "ReadStatus0x090000 response";
762-
LOG_DEBUG << GenericResponse(result.data, result.length).toString();
772+
if (result.length >= sizeof(uint32_t))
773+
LOG_DEBUG << "status 0x090000: " << *reinterpret_cast<const uint32_t *>(result.data);
763774

764775
command_tx_.execute(SetStreamEnabledCommand(nextCommandSeq()), result);
765776

@@ -813,8 +824,14 @@ void Freenect2DeviceImpl::stop()
813824
usb_control_.setIrInterfaceState(UsbControl::Disabled);
814825

815826
CommandTransaction::Result result;
816-
command_tx_.execute(Unknown0x0ACommand(nextCommandSeq()), result);
827+
command_tx_.execute(SetModeEnabledWith0x00640064Command(nextCommandSeq()), result);
828+
command_tx_.execute(SetModeDisabledCommand(nextCommandSeq()), result);
829+
command_tx_.execute(StopCommand(nextCommandSeq()), result);
817830
command_tx_.execute(SetStreamDisabledCommand(nextCommandSeq()), result);
831+
command_tx_.execute(SetModeEnabledCommand(nextCommandSeq()), result);
832+
command_tx_.execute(SetModeDisabledCommand(nextCommandSeq()), result);
833+
command_tx_.execute(SetModeEnabledCommand(nextCommandSeq()), result);
834+
command_tx_.execute(SetModeDisabledCommand(nextCommandSeq()), result);
818835

819836
usb_control_.setVideoTransferFunctionState(UsbControl::Disabled);
820837

@@ -837,6 +854,11 @@ void Freenect2DeviceImpl::close()
837854
stop();
838855
}
839856

857+
CommandTransaction::Result result;
858+
command_tx_.execute(SetModeEnabledWith0x00640064Command(nextCommandSeq()), result);
859+
command_tx_.execute(SetModeDisabledCommand(nextCommandSeq()), result);
860+
command_tx_.execute(ShutdownCommand(nextCommandSeq()), result);
861+
840862
if(pipeline_->getRgbPacketProcessor() != 0)
841863
pipeline_->getRgbPacketProcessor()->setFrameListener(0);
842864

0 commit comments

Comments
 (0)