Skip to content

Commit ad35788

Browse files
committed
Add static mcu reset fct
1 parent e608bad commit ad35788

File tree

5 files changed

+139
-11
lines changed

5 files changed

+139
-11
lines changed

examples/zed_oc_sensors_example.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ 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+
3537
// Create a SensorCapture object
3638
sl_oc::sensors::SensorCapture sens(verbose);
3739

@@ -52,7 +54,7 @@ int main(int argc, char *argv[])
5254
return EXIT_FAILURE;
5355
}
5456

55-
std::cout << "Video Capture connected to camera sn: " << sens.getSerialNumber() << std::endl;
57+
std::cout << "Sensor Capture connected to camera sn: " << sens.getSerialNumber() << std::endl;
5658
// <---- Inizialize the sensors
5759

5860
// ----> Get FW version information

include/sensorcapture.hpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,29 @@ class SL_OC_EXPORT SensorCapture
201201
*/
202202
const data::Temperature& getLastCameraTemperatureData(uint64_t timeout_usec=100);
203203

204+
/*!
205+
* \brief Perform a SW reset of the Sensors Module. To be called in case one of the sensors stops to work correctly.
206+
*
207+
* \param serial_number The serial number of the device to be reset (0 to reset the first available)
208+
* \return true if successful
209+
*
210+
* \note After the reset a new \ref SensorCapture connection is required
211+
* \note The Sensors Module reset automatically performs a reset of the Video Module, so a new \ref VideoCapture
212+
* connection is required
213+
*/
214+
static bool resetSensorModule(int serial_number=0);
215+
216+
/*!
217+
* \brief Perform a reset of the video module without resetting the sensor module. To be called in case the Video
218+
* module stops to work correctly.
219+
*
220+
* \param \param serial_number The serial number of the device to be reset (0 to reset the first available)
221+
* \return true if successful
222+
*
223+
* \note After the reset a new \ref VideoCapture connection is required
224+
*/
225+
static bool resetVideoModule(int serial_number=0);
226+
204227
#ifdef VIDEO_MOD_AVAILABLE
205228
void updateTimestampOffset(uint64_t frame_ts); //!< Called by VideoCapture to update timestamp offset
206229
inline void setStartTimestamp(uint64_t start_ts){mStartSysTs=start_ts;} //!< Called by VideoCapture to sync timestamps reference point
@@ -221,10 +244,6 @@ class SL_OC_EXPORT SensorCapture
221244
bool enableDataStream(bool enable); //!< Enable/Disable the data stream
222245
bool isDataStreamEnabled(); //!< Check if the data stream is enabled
223246
bool sendPing(); //!< Send a ping each second (before 6 seconds) to keep data streaming alive
224-
225-
//bool resetMCU(); TODO
226-
//bool resetUVC(); TODO
227-
228247
// ----> USB commands to MCU
229248

230249
private:

include/sensorcapture_def.hpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ namespace sl_oc {
4141
namespace sensors {
4242

4343
namespace usb {
44-
// ----> Command to be used with the REPORT ID "REP_ID_REQUEST_SET"
45-
// Command to ping the MCU to communicate that host is alive
46-
const uint8_t RQ_CMD_PING = 0xF2;
47-
// <---- Command to be used with the REPORT ID "REP_ID_REQUEST_SET"
4844

4945
/*!
5046
* \brief USB HID communication report IDs
@@ -61,6 +57,14 @@ typedef enum CUSTOMHID_REPORT_ID {
6157

6258
} CUSTOMHID_REPORT_ID;
6359

60+
/*!
61+
* \brief USB HID requests IDs
62+
*/
63+
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
66+
} CUSTOMHID_REQUEST_ID;
67+
6468
#pragma pack(push) // push current alignment to stack
6569
#pragma pack(1) // set alignment to 1 byte boundary
6670

@@ -163,6 +167,9 @@ const size_t TS_SHIFT_VAL_COUNT = 50; //!< Number of sensor data to use to updat
163167
*/
164168
inline std::string wstr2str( const wchar_t* wstr)
165169
{
170+
if(wstr==NULL)
171+
return std::string();
172+
166173
try
167174
{
168175
std::wstring ws( wstr );

src/sensorcapture.cpp

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ int SensorCapture::enumerateDevices()
6969
int fw_major = cur_dev->release_number>>8;
7070
int fw_minor = cur_dev->release_number&0x00FF;
7171
uint16_t pid = cur_dev->product_id;
72+
if(!cur_dev->serial_number)
73+
{
74+
cur_dev = cur_dev->next;
75+
continue;
76+
}
7277
std::string sn_str = wstr2str( cur_dev->serial_number );
7378
int sn = std::stoi( sn_str );
7479

@@ -157,7 +162,7 @@ bool SensorCapture::initializeSensors( int sn )
157162
if(!open( pid,sn))
158163
{
159164
std::string msg = "Connection to device with sn ";
160-
msg += sn_str;
165+
msg += std::to_string(sn);
161166
msg += " failed";
162167

163168
ERROR_OUT(mVerbose,msg);
@@ -171,7 +176,7 @@ bool SensorCapture::initializeSensors( int sn )
171176
if(mVerbose)
172177
{
173178
std::string msg = "Connected to device with sn ";
174-
msg += sn_str;
179+
msg += std::to_string(sn);
175180

176181
INFO_OUT(mVerbose,msg);
177182
}
@@ -599,6 +604,100 @@ bool SensorCapture::sendPing() {
599604
return true;
600605
}
601606

607+
bool SensorCapture::resetSensorModule(int serial_number)
608+
{
609+
// ----> Search for connected device
610+
struct hid_device_info *devs, *cur_dev;
611+
612+
if (hid_init()==-1)
613+
return 0;
614+
615+
devs = hid_enumerate(SL_USB_VENDOR, 0x0);
616+
cur_dev = devs;
617+
618+
bool found = false;
619+
uint16_t pid=0;
620+
std::string sn_str;
621+
622+
while (cur_dev) {
623+
int fw_major = cur_dev->release_number>>8;
624+
int fw_minor = cur_dev->release_number&0x00FF;
625+
pid = cur_dev->product_id;
626+
sn_str = wstr2str( cur_dev->serial_number );
627+
int sn = std::stoi( sn_str );
628+
629+
if(serial_number==0 || sn==serial_number)
630+
{
631+
if( pid==SL_USB_PROD_MCU_ZED2_REVA || pid==SL_USB_PROD_MCU_ZED2i_REVA)
632+
{
633+
found = true;
634+
break;
635+
}
636+
else
637+
{
638+
std::string msg = "The reset function works only for ZED2/ZED2i camera models.";
639+
std::cerr << msg << std::endl;
640+
641+
if(serial_number==0)
642+
continue;
643+
else
644+
return false;
645+
}
646+
}
647+
648+
cur_dev = cur_dev->next;
649+
}
650+
651+
hid_free_enumeration(devs);
652+
// <---- Search for connected device
653+
654+
if(!found)
655+
{
656+
std::string msg;
657+
if(serial_number!=0)
658+
{
659+
msg = "Unable to find the Sensor Module with serial number ";
660+
msg += std::to_string(serial_number);
661+
}
662+
else
663+
{
664+
msg = "Unable to find the Sensor Module of a ZED2 camera. Please verify the USB connection.";
665+
}
666+
667+
std::cerr << msg << std::endl;
668+
669+
return false;
670+
}
671+
672+
std::wstring wide_sn_string = std::wstring(sn_str.begin(), sn_str.end());
673+
const wchar_t* wsn = wide_sn_string.c_str();
674+
675+
hid_device* devHandle = hid_open(SL_USB_VENDOR, pid, wsn );
676+
677+
if(!devHandle)
678+
{
679+
std::string msg = "Unable to open the MCU HID device";
680+
std::cerr << msg << std::endl;
681+
682+
return false;
683+
}
684+
685+
unsigned char buf[65];
686+
buf[0] = static_cast<unsigned char>(usb::REP_ID_REQUEST_SET);
687+
buf[1] = static_cast<unsigned char>(usb::RQ_CMD_RST);
688+
689+
hid_send_feature_report(devHandle, buf, 2);
690+
sleep(2); // Wait for MCU to reboot
691+
692+
return true;
693+
}
694+
695+
bool SensorCapture::resetVideoModule(int serial_number)
696+
{
697+
698+
return true;
699+
}
700+
602701
const data::Imu& SensorCapture::getLastIMUData(uint64_t timeout_usec)
603702
{
604703
// ----> Wait for a new frame

src/videocapture.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,6 +2013,7 @@ void VideoCapture::setColorBars(int side, bool c)
20132013

20142014

20152015
unsigned long long ulAddr2 = 0x80181080;
2016+
20162017
if (side==1)
20172018
ulAddr2 = 0x80181880;
20182019

0 commit comments

Comments
 (0)