Skip to content

Commit 4a974b7

Browse files
authored
Fix Simulator Interface + Optimize CMake (#371)
* Fix Simulator Interface + Optimize CMake * Minor formatting, added comments, CAN CMake update * Formatting * Implement openCamera
1 parent bd85c6d commit 4a974b7

File tree

7 files changed

+130
-106
lines changed

7 files changed

+130
-106
lines changed

src/CAN/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
cmake_minimum_required(VERSION 3.8)
22

3+
if(WORLD_INTERFACE STREQUAL "REAL")
34
add_executable(FakeCANBoard FakeCANBoard.cpp)
4-
target_link_libraries(FakeCANBoard can_interface real_world_interface)
5+
target_link_libraries(FakeCANBoard can_interface real_world_interface)
6+
endif()

src/CMakeLists.txt

Lines changed: 56 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,19 @@ link_libraries(frozen::frozen)
8585
# Find argparse, library used for parsing command line arguments
8686
find_package(argparse REQUIRED)
8787

88-
8988
# Find the JSON library
9089
find_package(nlohmann_json 3.2.0 REQUIRED)
9190

92-
# Find the CAN library; contains packet and protocol definitions and does not
93-
# actually require physical CAN to be present.
94-
FetchContent_Declare(
91+
if(WORLD_INTERFACE STREQUAL "REAL")
92+
# Find the CAN library; contains packet and protocol definitions and does not
93+
# actually require physical CAN to be present.
94+
FetchContent_Declare(
9595
HindsightCAN
9696
GIT_REPOSITORY https://github.com/huskyroboticsteam/HindsightCAN.git
9797
GIT_TAG ce04c1a2dced3eb0e5beaaf39b2fcc436655e790
98-
)
99-
FetchContent_MakeAvailable(HindsightCAN)
98+
)
99+
FetchContent_MakeAvailable(HindsightCAN)
100+
endif()
100101

101102
FetchContent_Declare(LoguruGitRepo
102103
GIT_REPOSITORY "https://github.com/emilk/loguru"
@@ -154,30 +155,32 @@ add_library(utils SHARED
154155
world_interface/data.cpp)
155156
target_link_libraries(utils ${OpenCV_LIBS})
156157

157-
## ====== CAN Interfaces =======
158-
# Stub CAN interface is used for the tests (and for the Rover if
159-
# CAN is disabled) and the real CAN interface is used for the Rover if CAN is
160-
# enabled.
161-
162-
# Common CAN source files
163-
164-
# **DON'T MAKE THIS SHARED**
165-
# Making this library shared causes some memory fuckery
166-
# No clue why but CAN I/O goes to shit. Don't do it.
167-
add_library(can_interface STATIC)
168-
target_sources(can_interface PRIVATE
169-
CAN/CANMotor.cpp
170-
CAN/CANUtils.cpp
171-
)
158+
if(WORLD_INTERFACE STREQUAL "REAL")
159+
## ====== CAN Interfaces =======
160+
# Stub CAN interface is used for the tests (and for the Rover if
161+
# CAN is disabled) and the real CAN interface is used for the Rover if CAN is
162+
# enabled.
163+
164+
# Common CAN source files
165+
166+
# **DON'T MAKE THIS SHARED**
167+
# Making this library shared causes some memory fuckery
168+
# No clue why but CAN I/O goes to shit. Don't do it.
169+
add_library(can_interface STATIC)
170+
target_sources(can_interface PRIVATE
171+
CAN/CANMotor.cpp
172+
CAN/CANUtils.cpp
173+
)
172174

173-
# Hardware specific source files
174-
target_sources(can_interface PRIVATE
175-
CAN/CAN.cpp)
176-
target_link_libraries(can_interface PUBLIC
177-
HindsightCAN
178-
Threads::Threads
179-
utils
180-
)
175+
# Hardware specific source files
176+
target_sources(can_interface PRIVATE
177+
CAN/CAN.cpp)
178+
target_link_libraries(can_interface PUBLIC
179+
HindsightCAN
180+
Threads::Threads
181+
utils
182+
)
183+
endif()
181184

182185
## GPS Implementations
183186

@@ -208,31 +211,27 @@ add_library(world_interface_core STATIC
208211
target_include_directories(world_interface_core SYSTEM PUBLIC ${OpenCV_INCLUDE_DIRS})
209212
target_link_libraries(world_interface_core PUBLIC ${vision_libs} opencv_aruco ${OpenCV_LIBS} utils camera)
210213

211-
# CAN library (above) requires some utilities from this
212-
target_link_libraries(can_interface PUBLIC utils)
213-
214-
add_library(stub_world_interface STATIC
215-
world_interface/noop_world.cpp)
216-
217-
add_library(real_world_interface STATIC
218-
world_interface/real_world_interface.cpp
219-
world_interface/motor/can_motor.cpp
220-
)
221-
222-
add_library(simulator_interface STATIC
223-
world_interface/simulator_interface.cpp
224-
world_interface/motor/sim_motor.cpp
225-
)
226-
227-
target_link_libraries(real_world_interface gps_interface can_interface world_interface_core)
228-
target_link_libraries(simulator_interface world_interface_core)
229-
target_link_libraries(stub_world_interface world_interface_core)
230-
231214
if(WORLD_INTERFACE STREQUAL "REAL")
215+
# CAN library (above) requires some utilities from this
216+
target_link_libraries(can_interface PUBLIC utils)
217+
218+
add_library(real_world_interface STATIC
219+
world_interface/real_world_interface.cpp
220+
world_interface/motor/can_motor.cpp
221+
)
222+
target_link_libraries(real_world_interface gps_interface can_interface world_interface_core)
232223
add_library(world_interface ALIAS real_world_interface)
233224
elseif(WORLD_INTERFACE STREQUAL "SIMULATOR")
225+
add_library(simulator_interface STATIC
226+
world_interface/simulator_interface.cpp
227+
world_interface/motor/sim_motor.cpp
228+
)
229+
target_link_libraries(simulator_interface world_interface_core)
234230
add_library(world_interface ALIAS simulator_interface)
235231
else()
232+
add_library(stub_world_interface STATIC
233+
world_interface/noop_world.cpp)
234+
target_link_libraries(stub_world_interface world_interface_core)
236235
add_library(world_interface ALIAS stub_world_interface)
237236
endif()
238237

@@ -273,7 +272,7 @@ add_executable(Rover Rover.cpp)
273272
add_library(rover_common STATIC
274273
Globals.cpp
275274
control_interface.cpp
276-
)
275+
)
277276
target_link_libraries(rover_common
278277
utils
279278
websocket_utils
@@ -296,13 +295,6 @@ target_link_libraries(Rover
296295
${vision_libs}
297296
)
298297

299-
add_executable(TunePID TunePID.cpp)
300-
target_link_libraries(TunePID
301-
${rover_libs}
302-
can_interface
303-
)
304-
target_link_libraries(TunePID ${vision_libs})
305-
306298
if(WITH_TESTS)
307299
add_executable(tests
308300
Tests.cpp
@@ -359,6 +351,13 @@ if (WORLD_INTERFACE STREQUAL "REAL")
359351
add_executable(LimitSwitchCalibration
360352
LimitCalib.cpp)
361353
target_link_libraries(LimitSwitchCalibration real_world_interface)
354+
355+
add_executable(TunePID TunePID.cpp)
356+
target_link_libraries(TunePID
357+
${rover_libs}
358+
can_interface
359+
)
360+
target_link_libraries(TunePID ${vision_libs})
362361
endif()
363362

364363
add_compile_options(

src/Constants.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ const robot::types::CameraID DRILL_CAMERA_ID = 540;
5858
const robot::types::CameraID MICROSCOPE_ID = 500;
5959

6060
const std::unordered_map<robot::types::CameraID, std::string> CAMERA_CONFIG_PATHS = {
61-
{HAND_CAMERA_ID, "../camera-config/HandCameraCalibration.yml"},
62-
{WRIST_CAMERA_ID, "../camera-config/WristCameraCalibration.yml"},
63-
{MAST_CAMERA_ID, "../camera-config/MastCameraCalibration.yml"},
64-
{RAND_CAMERA_ID, "../camera-config/RandCameraCalibration.yml"},
65-
{CJ1_CAMERA_ID, "../camera-config/CJ1.yml"},
66-
{BOX_CAMERA_ID, "../camera-config/BoxCamera.yml"},
67-
{CJ3_CAMERA_ID, "../camera-config/CJ3.yml"},
68-
{DRILL_CAMERA_ID, "../camera-config/DrillCamera.yml"},
69-
{MICROSCOPE_ID, "../camera-config/Microscope.yml"},
61+
{HAND_CAMERA_ID, "../camera-config/HandCameraCalibration.yml"},
62+
{WRIST_CAMERA_ID, "../camera-config/WristCameraCalibration.yml"},
63+
{MAST_CAMERA_ID, "../camera-config/MastCameraCalibration.yml"},
64+
{RAND_CAMERA_ID, "../camera-config/RandCameraCalibration.yml"},
65+
{CJ1_CAMERA_ID, "../camera-config/CJ1.yml"},
66+
{BOX_CAMERA_ID, "../camera-config/BoxCamera.yml"},
67+
{CJ3_CAMERA_ID, "../camera-config/CJ3.yml"},
68+
{DRILL_CAMERA_ID, "../camera-config/DrillCamera.yml"},
69+
{MICROSCOPE_ID, "../camera-config/Microscope.yml"},
7070
};
7171

7272
/**

src/camera/CMakeLists.txt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ find_package(Threads REQUIRED)
1212
add_executable(calibration
1313
calibration.cpp)
1414

15-
add_executable(panoramic
16-
./CameraParams.cpp
17-
./Camera.cpp
18-
./CameraConfig.cpp
19-
PanoramicCamera.cpp)
20-
2115
target_link_libraries(calibration ${OpenCV_LIBS})
22-
target_link_libraries(panoramic ${OpenCV_LIBS} can_interface real_world_interface)
2316

17+
if(WORLD_INTERFACE STREQUAL "REAL")
18+
add_executable(panoramic
19+
./CameraParams.cpp
20+
./Camera.cpp
21+
./CameraConfig.cpp
22+
PanoramicCamera.cpp)
23+
target_link_libraries(panoramic ${OpenCV_LIBS} can_interface real_world_interface)
24+
endif()

src/control_interface.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -186,22 +186,21 @@ void setJointMotorPower(robot::types::jointid_t joint, double power) {
186186
setMotorPower(motorid_t::wristDiffLeft, gearPwr.left);
187187
setMotorPower(motorid_t::wristDiffRight, gearPwr.right);
188188
} else if (joint == jointid_t::fourBarLinkage) {
189-
auto dp = getJointPos(jointid_t::fourBarLinkage);
190-
if (dp.isValid()) {
191-
int currAngle = dp.getData();
192-
currAngle = currAngle / 1000;
193-
if(currAngle > 320 ||
194-
currAngle < 50) {
195-
setMotorPower(motorid_t::fourbar1, power * 0.7);
196-
setMotorPower(motorid_t::fourbar2, power * 0.7);
197-
} else if (currAngle < 265 && power < 0){
198-
setMotorPower(motorid_t::fourbar1, -power);
199-
setMotorPower(motorid_t::fourbar2, -power);
200-
} else {
201-
setMotorPower(motorid_t::fourbar1, power);
202-
setMotorPower(motorid_t::fourbar2, power);
203-
}
204-
} else {
189+
auto dp = getJointPos(jointid_t::fourBarLinkage);
190+
if (dp.isValid()) {
191+
int currAngle = dp.getData();
192+
currAngle = currAngle / 1000;
193+
if (currAngle > 320 || currAngle < 50) {
194+
setMotorPower(motorid_t::fourbar1, power * 0.7);
195+
setMotorPower(motorid_t::fourbar2, power * 0.7);
196+
} else if (currAngle < 265 && power < 0) {
197+
setMotorPower(motorid_t::fourbar1, -power);
198+
setMotorPower(motorid_t::fourbar2, -power);
199+
} else {
200+
setMotorPower(motorid_t::fourbar1, power);
201+
setMotorPower(motorid_t::fourbar2, power);
202+
}
203+
} else {
205204
setMotorPower(motorid_t::fourbar1, power);
206205
setMotorPower(motorid_t::fourbar2, power);
207206
}

src/navtypes.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ using pose_t = Eigen::Vector3d; // Robot pose: x, y, theta
3131
using transform_t = Eigen::Matrix3d; // a pose in matrix form; see toPose below
3232
using trajectory_t = std::vector<transform_t>;
3333

34-
template <int rows, int cols> using Matrixd = Eigen::Matrix<double, rows, cols>;
34+
template <int rows, int cols>
35+
using Matrixd = Eigen::Matrix<double, rows, cols>;
3536

36-
template <int dim> using Vectord = Eigen::Matrix<double, dim, 1>;
37+
template <int dim>
38+
using Vectord = Eigen::Matrix<double, dim, 1>;
3739

38-
template <int dim, int dim2 = 1> using Arrayd = Eigen::Array<double, dim, dim2>;
40+
template <int dim, int dim2 = 1>
41+
using Arrayd = Eigen::Array<double, dim, dim2>;
3942

4043
using point_t =
4144
Eigen::Vector3d; // x, y, 1 (or 0, 0, 0 to represent "no data")

src/world_interface/simulator_interface.cpp

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ void sendJSON(const json& obj) {
8787
wsServer->get().sendJSON(PROTOCOL_PATH, obj);
8888
}
8989

90-
static std::shared_ptr<robot::types::CameraHandle> openCamera(CameraID cam) {
91-
return nullptr;
92-
}
93-
9490
static std::shared_ptr<robot::types::CameraHandle> openCamera(CameraID cam, std::optional<std::vector<double>> list1d = std::nullopt,
9591
uint8_t fps = 20, uint16_t width = 640, uint16_t height = 480) {
9692
if (list1d) {
@@ -114,14 +110,6 @@ static std::shared_ptr<robot::types::CameraHandle> openCamera(CameraID cam, std:
114110
return nullptr;
115111
}
116112

117-
void initCameras() {
118-
auto cfg = cam::readConfigFromFile(Constants::CAMERA_CONFIG_PATHS.at(Constants::MAST_CAMERA_ID));
119-
cameraConfigMap[Constants::MAST_CAMERA_ID] = cfg;
120-
openCamera(Constants::HAND_CAMERA_ID, cfg.intrinsicParams->getIntrinsicList());
121-
openCamera(Constants::WRIST_CAMERA_ID, cfg.intrinsicParams->getIntrinsicList());
122-
openCamera(Constants::MAST_CAMERA_ID, cfg.intrinsicParams->getIntrinsicList());
123-
}
124-
125113
void initMotors() {
126114
// initializes map of motor ids and shared ptrs of their objects
127115
for (const auto& x : motorNameMap) {
@@ -265,7 +253,6 @@ void world_interface_init(
265253
std::optional<std::reference_wrapper<net::websocket::SingleClientWSServer>> wsServer,
266254
bool initOnlyMotors) {
267255
initSimServer(wsServer.value());
268-
initCameras();
269256
initMotors();
270257
}
271258

@@ -298,6 +285,27 @@ std::unordered_set<CameraID> getCameras() {
298285
return util::keySet(cameraLastFrameIdxMap);
299286
}
300287

288+
std::shared_ptr<robot::types::CameraHandle> openCamera(CameraID cam) {
289+
cv::FileStorage fs(Constants::CAMERA_CONFIG_PATHS.at(cam), cv::FileStorage::READ);
290+
if (!fs.isOpened()) {
291+
throw std::invalid_argument("Configuration file for Camera ID" + std::to_string(cam) + " does not exist");
292+
}
293+
294+
if (fs[KEY_IMAGE_WIDTH].empty() || fs[KEY_IMAGE_HEIGHT].empty() || fs[KEY_FRAMERATE].empty()) {
295+
throw std::invalid_argument("Configuration file missing key(s)");
296+
}
297+
298+
json msg = {{"type", "simCameraStreamOpenRequest"},
299+
{"camera", cam},
300+
{"fps", static_cast<int>(fs[KEY_FRAMERATE])},
301+
{"width", static_cast<int>(fs[KEY_IMAGE_WIDTH])},
302+
{"height", static_cast<int>(fs[KEY_IMAGE_HEIGHT])},
303+
{"intrinsics", nullptr}};
304+
sendJSON(msg);
305+
306+
return nullptr;
307+
}
308+
301309
bool hasNewCameraFrame(CameraID cameraID, uint32_t oldFrameNum) {
302310
// acquire shared lock
303311
std::shared_lock<std::shared_mutex> lock(cameraFrameMapMutex);
@@ -392,6 +400,18 @@ void setMotorVel(robot::types::motorid_t motor, int32_t targetVel) {
392400
motor_ptr->setMotorVel(targetVel);
393401
}
394402

403+
void setServoPos(robot::types::servoid_t servo, int32_t position) {
404+
// Implement when Servos are added to Simulator
405+
}
406+
407+
void setRequestedStepperTurnAngle(robot::types::stepperid_t stepper, int16_t angle) {
408+
// Implement when Steppers are added to Simulator
409+
}
410+
411+
void setActuator(uint8_t value) {
412+
// Implement when Actuators are added to Simulator
413+
}
414+
395415
callbackid_t addLimitSwitchCallback(
396416
robot::types::motorid_t motor,
397417
const std::function<void(robot::types::motorid_t motor,

0 commit comments

Comments
 (0)