Skip to content

Commit e9f234b

Browse files
committed
Add function to reset the OV580 module without resetting the MCU
1 parent ad35788 commit e9f234b

File tree

5 files changed

+114
-15
lines changed

5 files changed

+114
-15
lines changed

examples/zed_oc_sensors_example.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ int main(int argc, char *argv[])
3232
// Set the verbose level
3333
sl_oc::VERBOSITY verbose = sl_oc::VERBOSITY::INFO;
3434

35-
sl_oc::sensors::SensorCapture::resetSensorModule();
36-
3735
// Create a SensorCapture object
3836
sl_oc::sensors::SensorCapture sens(verbose);
3937

examples/zed_oc_sync_example.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ uint64_t mcu_sync_ts=0;
5151
// The main function
5252
int main(int argc, char *argv[])
5353
{
54+
sl_oc::sensors::SensorCapture::resetSensorModule();
55+
sl_oc::sensors::SensorCapture::resetVideoModule();
56+
5457
// Set the verbose level
55-
sl_oc::VERBOSITY verbose = sl_oc::VERBOSITY::INFO;
58+
sl_oc::VERBOSITY verbose = sl_oc::VERBOSITY::ERROR;
5659

5760
// ----> Set the video parameters
5861
sl_oc::video::VideoParams params;

include/sensorcapture.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ class SL_OC_EXPORT SensorCapture
231231
#endif
232232

233233
private:
234+
static bool searchForConnectedDev(int* serial_number, unsigned short* found_pid); //!< Search for a device and returns its pid and serial number
235+
234236
void grabThreadFunc(); //!< The sensor data grabbing thread function
235237

236238
bool startCapture(); //!< Start data capture thread

include/sensorcapture_def.hpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,23 @@ typedef enum CUSTOMHID_REPORT_ID {
5050
REP_ID_SENSOR_DATA = 0x01, //!< Sensor data report ID
5151

5252
// Generic commands
53-
REP_ID_REQUEST_SET = 0x21, //!< USB Request report ID
53+
REP_ID_REQUEST_SET = 0x21, //!< USB Request report ID
54+
55+
// OV580
56+
REP_ID_OV580_CMD = 0x22, //!< OV580 control request
5457

5558
// Features Reports
56-
REP_ID_SENSOR_STREAM_STATUS = 0x32, //!< Stream Status report ID
59+
REP_ID_SENSOR_STREAM_STATUS = 0x32, //!< Stream Status report ID
5760

5861
} CUSTOMHID_REPORT_ID;
5962

6063
/*!
6164
* \brief USB HID requests IDs
6265
*/
6366
typedef enum CUSTOMHID_REQUEST_ID {
64-
RQ_CMD_PING = 0xF2, //!< Command to ping the MCU to communicate that host is alive
65-
RQ_CMD_RST = 0xE1, //!< Command to reset the MCU
67+
RQ_CMD_PING = 0xF2, //!< Command to ping the MCU to communicate that host is alive
68+
RQ_CMD_RST = 0xE1, //!< Command to reset the MCU
69+
OV580_CMD_RESET = 0x02 //!< Command to reset the OV580 using the MCU
6670
} CUSTOMHID_REQUEST_ID;
6771

6872
#pragma pack(push) // push current alignment to stack
@@ -109,6 +113,15 @@ typedef struct StreamStatus {
109113
uint8_t stream_status; //!< Status of the USB streaming
110114
} StreamStatus;
111115

116+
/*!
117+
* \brief OV580 control using the MCU
118+
*/
119+
typedef struct _ov580_cmd_struct {
120+
uint8_t struct_id; //!< struct identifier for HID comm
121+
uint8_t cmd; //!< command to be sent to OV580: OV580_RESET
122+
uint16_t info; //!< NOT USED
123+
} OV580CmdStruct;
124+
112125
#pragma pack(pop) // Restore previous saved alignment
113126

114127
}

src/sensorcapture.cpp

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -604,13 +604,20 @@ bool SensorCapture::sendPing() {
604604
return true;
605605
}
606606

607-
bool SensorCapture::resetSensorModule(int serial_number)
607+
bool SensorCapture::searchForConnectedDev(int* serial_number, unsigned short* found_pid)
608608
{
609+
int in_serial_number;
610+
if(serial_number==nullptr)
611+
in_serial_number = 0;
612+
else
613+
in_serial_number = *serial_number;
614+
int found_serial_number = 0;
615+
609616
// ----> Search for connected device
610617
struct hid_device_info *devs, *cur_dev;
611618

612619
if (hid_init()==-1)
613-
return 0;
620+
return false;
614621

615622
devs = hid_enumerate(SL_USB_VENDOR, 0x0);
616623
cur_dev = devs;
@@ -626,19 +633,20 @@ bool SensorCapture::resetSensorModule(int serial_number)
626633
sn_str = wstr2str( cur_dev->serial_number );
627634
int sn = std::stoi( sn_str );
628635

629-
if(serial_number==0 || sn==serial_number)
636+
if(in_serial_number==0 || sn==in_serial_number)
630637
{
631638
if( pid==SL_USB_PROD_MCU_ZED2_REVA || pid==SL_USB_PROD_MCU_ZED2i_REVA)
632639
{
633640
found = true;
641+
found_serial_number = sn;
634642
break;
635643
}
636644
else
637645
{
638646
std::string msg = "The reset function works only for ZED2/ZED2i camera models.";
639647
std::cerr << msg << std::endl;
640648

641-
if(serial_number==0)
649+
if(in_serial_number==0)
642650
continue;
643651
else
644652
return false;
@@ -651,24 +659,41 @@ bool SensorCapture::resetSensorModule(int serial_number)
651659
hid_free_enumeration(devs);
652660
// <---- Search for connected device
653661

654-
if(!found)
662+
if(!found) {
663+
return false;
664+
}
665+
666+
if(serial_number)
667+
*serial_number = found_serial_number;
668+
if(found_pid)
669+
*found_pid = pid;
670+
return true;
671+
}
672+
673+
bool SensorCapture::resetSensorModule(int serial_number)
674+
{
675+
int found_sn = serial_number;
676+
unsigned short pid;
677+
bool res = searchForConnectedDev(&found_sn, &pid);
678+
if(!res)
655679
{
656680
std::string msg;
657681
if(serial_number!=0)
658682
{
659-
msg = "Unable to find the Sensor Module with serial number ";
683+
msg = "[sl_oc::sensors::SensorCapture] WARNING: Sensors Module reset failed. Unable to find the Sensor Module with serial number ";
660684
msg += std::to_string(serial_number);
661685
}
662686
else
663687
{
664-
msg = "Unable to find the Sensor Module of a ZED2 camera. Please verify the USB connection.";
688+
msg = "[sl_oc::sensors::SensorCapture] WARNING: Sensors Module reset failed. Unable to find the Sensor Module of a ZED2 camera. Please verify the USB connection.";
665689
}
666690

667691
std::cerr << msg << std::endl;
668692

669693
return false;
670694
}
671695

696+
std::string sn_str = std::to_string(found_sn);
672697
std::wstring wide_sn_string = std::wstring(sn_str.begin(), sn_str.end());
673698
const wchar_t* wsn = wide_sn_string.c_str();
674699

@@ -687,14 +712,72 @@ bool SensorCapture::resetSensorModule(int serial_number)
687712
buf[1] = static_cast<unsigned char>(usb::RQ_CMD_RST);
688713

689714
hid_send_feature_report(devHandle, buf, 2);
690-
sleep(2); // Wait for MCU to reboot
715+
// Note: cannot verify the return value of the `hid_send_feature_report` command because the MCU is suddenly reset
716+
// and it cannot return a valid value
717+
718+
sleep(2); // Wait for MCU and OV580 to reboot
719+
720+
std::cerr << "[sl_oc::sensors::SensorCapture] INFO: Sensors Module reset successful" << std::endl;
691721

692722
return true;
693723
}
694724

695725
bool SensorCapture::resetVideoModule(int serial_number)
696726
{
727+
int found_sn = serial_number;
728+
unsigned short pid;
729+
bool res = searchForConnectedDev(&found_sn, &pid);
730+
if(!res)
731+
{
732+
std::string msg;
733+
if(serial_number!=0)
734+
{
735+
msg = "[sl_oc::sensors::SensorCapture] WARNING: Video Module reset failed. Unable to find the Sensor Module with serial number ";
736+
msg += std::to_string(serial_number);
737+
}
738+
else
739+
{
740+
msg = "[sl_oc::sensors::SensorCapture] WARNING: Video Module reset failed. Unable to find the Sensor Module of a ZED2 camera. Please verify the USB connection.";
741+
}
742+
743+
std::cerr << msg << std::endl;
744+
745+
return false;
746+
}
747+
748+
std::string sn_str = std::to_string(found_sn);
749+
std::wstring wide_sn_string = std::wstring(sn_str.begin(), sn_str.end());
750+
const wchar_t* wsn = wide_sn_string.c_str();
751+
752+
hid_device* devHandle = hid_open(SL_USB_VENDOR, pid, wsn );
753+
754+
if(!devHandle)
755+
{
756+
std::string msg = "Unable to open the MCU HID device";
757+
std::cerr << msg << std::endl;
758+
759+
return false;
760+
}
761+
762+
usb::OV580CmdStruct cmd;
763+
cmd.struct_id = usb::REP_ID_OV580_CMD;
764+
cmd.cmd = usb::OV580_CMD_RESET;
765+
cmd.info=0;
766+
767+
unsigned char buf[65];
768+
memcpy(buf, &(cmd.struct_id), sizeof(usb::OV580CmdStruct));
769+
770+
int ret = hid_send_feature_report(devHandle, buf, sizeof(usb::OV580CmdStruct));
771+
hid_close(devHandle);
772+
773+
if(ret!=sizeof(usb::OV580CmdStruct)) {
774+
std::cerr << "[sl_oc::sensors::SensorCapture] INFO: Video Module reset failed" << std::endl;
775+
return false;
776+
}
777+
778+
sleep(2); // Wait for OV580 to reboot
697779

780+
std::cerr << "[sl_oc::sensors::SensorCapture] INFO: Video Module reset successful" << std::endl;
698781
return true;
699782
}
700783

0 commit comments

Comments
 (0)