Skip to content

Commit

Permalink
CMake build configuration for C++ library (#150)
Browse files Browse the repository at this point in the history
* rename C++ examples files to *.cpp

* remove duplicated function 'fatal_error'

* all examples: use 'GoPiGo3.h' header and compile 'GoPiGo3.cpp' into examples

* throw exceptions instead of terminating program

* remove whitespaces

* CMake file for compiling GoPiGo3 library and examples

* install library and header files and make them available to other projects

* ROS package definition for GoPiGo3 library
  • Loading branch information
christianrauch authored and CleoQc committed Feb 27, 2018
1 parent 1c5fd5b commit 625d817
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 102 deletions.
74 changes: 74 additions & 0 deletions Software/C/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
cmake_minimum_required(VERSION 2.8.3)
project(gopigo3_cpp)

### Build

include_directories(.)

## GoPiGo3 library
add_library(gopigo3 SHARED GoPiGo3.cpp)

## Examples

# grove_led
add_executable(grove_led Examples/grove_led.cpp)
target_link_libraries(grove_led gopigo3)

# i2c
add_executable(i2c Examples/i2c.cpp)
target_link_libraries(i2c gopigo3)

# info
add_executable(info Examples/info.cpp)
target_link_libraries(info gopigo3)

# leds
add_executable(leds Examples/leds.cpp)
target_link_libraries(leds gopigo3)

# motors
add_executable(motors Examples/motors.cpp)
target_link_libraries(motors gopigo3)

# sensors
add_executable(sensors Examples/sensors.cpp)
target_link_libraries(sensors gopigo3)

### Installation

install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/
DESTINATION include
FILES_MATCHING PATTERN "*.h"
PATTERN "Examples" EXCLUDE
)

install(TARGETS gopigo3 EXPORT gopigo3Targets
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin
INCLUDES DESTINATION include
)

### Export CMake configuration

set_property(TARGET gopigo3 APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include/>
)

set(CONF_INCLUDE_DIRS ${CMAKE_INSTALL_PREFIX}/include/)

export(EXPORT gopigo3Targets
FILE "${PROJECT_BINARY_DIR}/gopigo3_cppTargets.cmake")

configure_file(gopigo3_cppConfig.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/gopigo3_cppConfig.cmake" @ONLY)

install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/gopigo3_cppConfig.cmake"
DESTINATION share/gopigo3_cpp/cmake)

install(EXPORT gopigo3Targets
FILE gopigo3_cppTargets.cmake
DESTINATION share/gopigo3_cpp/cmake
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
* Results: When you run this program, you should see the grove LED brightness pulsing up and down.
*
* Example compile command:
* g++ -o program "grove_led.c"
* g++ -o grove_led grove_led.cpp ../GoPiGo3.cpp -I..
* Example run command:
* sudo ./program
* sudo ./grove_led
*
*/

#include "GoPiGo3.cpp" // for GoPiGo3
#include <GoPiGo3.h> // for GoPiGo3
#include <stdio.h> // for printf
#include <unistd.h> // for usleep
#include <signal.h> // for catching exit signals
Expand All @@ -30,12 +30,12 @@ void exit_signal_handler(int signo);

int main(){
signal(SIGINT, exit_signal_handler); // register the exit function for Ctrl+C

GPG.detect(); // Make sure that the GoPiGo3 is communicating and that the firmware is compatible with the drivers.

GPG.set_grove_type(GROVE_1, GROVE_TYPE_CUSTOM);
GPG.set_grove_mode(GROVE_1_1, OUTPUT_PWM);

int8_t i = 0;
int8_t a = 1;
while(true){
Expand Down
10 changes: 5 additions & 5 deletions Software/C/Examples/i2c.c → Software/C/Examples/i2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
* Results: When you run this program, P0 output should toggle.
*
* Example compile command:
* g++ -o program "i2c.c"
* g++ -o i2c i2c.cpp ../GoPiGo3.cpp -I..
* Example run command:
* sudo ./program
* sudo ./i2c
*
*/

#include "GoPiGo3.cpp" // for GoPiGo3
#include <GoPiGo3.h> // for GoPiGo3
#include <stdio.h> // for printf
#include <unistd.h> // for usleep
#include <signal.h> // for catching exit signals
Expand All @@ -30,9 +30,9 @@ void exit_signal_handler(int signo);

int main(){
signal(SIGINT, exit_signal_handler); // register the exit function for Ctrl+C

GPG.detect(); // Make sure that the GoPiGo3 is communicating and that the firmware is compatible with the drivers.

GPG.set_grove_type(GROVE_1, GROVE_TYPE_I2C);
i2c_struct_t I2C1;
I2C1.address = 0x24;
Expand Down
20 changes: 10 additions & 10 deletions Software/C/Examples/info.c → Software/C/Examples/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,40 @@
* Results: Print information about the attached GoPiGo3.
*
* Example compile command:
* g++ -o program "info.c"
* g++ -o info info.cpp ../GoPiGo3.cpp -I..
* Example run command:
* sudo ./program
* sudo ./info
*
*/

#include "GoPiGo3.cpp" // for GoPiGo3
#include <GoPiGo3.h> // for GoPiGo3
#include <stdio.h> // for printf

GoPiGo3 GPG; // Create a GoPiGo3 instance

int main(){
char str[33]; // Room for the 32-character serial number string plus the NULL terminator.

// Make sure that the GoPiGo3 is communicating and that the firmware is compatible with the drivers.
// pass 'false' to detect() to make the error non-critical (return the error instead of exiting the program).
if(GPG.detect(false) == ERROR_NONE){
printf("\nGoPiGo3 info:\n");

GPG.get_manufacturer(str);
printf(" Manufacturer : %s\n", str);

GPG.get_board(str);
printf(" Board : %s\n", str);

GPG.get_id(str);
printf(" Serial Number : %s\n", str);

GPG.get_version_hardware(str);
printf(" Hardware version: %s\n", str);

GPG.get_version_firmware(str);
printf(" Firmware version: %s\n", str);

printf(" Battery voltage : %.3f\n", GPG.get_voltage_battery());
printf(" 5v voltage : %.3f\n", GPG.get_voltage_5v());
}else{
Expand Down
14 changes: 7 additions & 7 deletions Software/C/Examples/leds.c → Software/C/Examples/leds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
* Results: When you run this program, you should see the encoder value for each motor. Manually rotate the left motor, and the right motor will follow.
*
* Example compile command:
* g++ -o program "leds.c"
* g++ -o leds leds.cpp ../GoPiGo3.cpp -I..
* Example run command:
* sudo ./program
* sudo ./leds
*
*/

#include "GoPiGo3.cpp" // for GoPiGo3
#include <GoPiGo3.h> // for GoPiGo3
#include <stdio.h> // for printf
#include <unistd.h> // for usleep
#include <signal.h> // for catching exit signals
Expand All @@ -30,9 +30,9 @@ void exit_signal_handler(int signo);

int main(){
signal(SIGINT, exit_signal_handler); // register the exit function for Ctrl+C

GPG.detect(); // Make sure that the GoPiGo3 is communicating and that the firmware is compatible with the drivers.

uint8_t i1 = 0;
uint8_t i2 = BRIGHTNESS_LIMIT / 3;
uint8_t i3 = (BRIGHTNESS_LIMIT * 2) / 3;
Expand All @@ -44,7 +44,7 @@ int main(){
GPG.set_led(LED_EYE_RIGHT, i2, i3, i1);
GPG.set_led(LED_BLINKER_LEFT, i1);
GPG.set_led(LED_BLINKER_RIGHT, i2);

i1 += a1;
if(i1 == BRIGHTNESS_LIMIT || i1 == 0){
a1 *= -1;
Expand All @@ -57,7 +57,7 @@ int main(){
if(i3 == BRIGHTNESS_LIMIT || i3 == 0){
a3 *= -1;
}

// slow down
usleep(25000);
}
Expand Down
18 changes: 9 additions & 9 deletions Software/C/Examples/motors.c → Software/C/Examples/motors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
* Results: When you run this program, you should see the encoder value for each motor. Manually rotate the left motor, and the right motor will follow.
*
* Example compile command:
* g++ -o program "motors.c"
* g++ -o motors motors.cpp ../GoPiGo3.cpp -I..
* Example run command:
* sudo ./program
* sudo ./motors
*
*/

#include "GoPiGo3.cpp" // for GoPiGo3
#include <GoPiGo3.h> // for GoPiGo3
#include <stdio.h> // for printf
#include <unistd.h> // for usleep
#include <signal.h> // for catching exit signals
Expand All @@ -28,24 +28,24 @@ void exit_signal_handler(int signo);

int main(){
signal(SIGINT, exit_signal_handler); // register the exit function for Ctrl+C

GPG.detect(); // Make sure that the GoPiGo3 is communicating and that the firmware is compatible with the drivers.

// Reset the encoders
GPG.offset_motor_encoder(MOTOR_LEFT, GPG.get_motor_encoder(MOTOR_LEFT));
GPG.offset_motor_encoder(MOTOR_RIGHT, GPG.get_motor_encoder(MOTOR_RIGHT));

while(true){
// Read the encoders
int32_t EncoderLeft = GPG.get_motor_encoder(MOTOR_LEFT);
int32_t EncoderRight = GPG.get_motor_encoder(MOTOR_RIGHT);

// Use the encoder value from the left motor to control the position of the right motor
GPG.set_motor_position(MOTOR_RIGHT, EncoderLeft);

// Display the encoder values
printf("Encoder Left: %6d Right: %6d\n", EncoderLeft, EncoderRight);

// Delay for 20ms
usleep(20000);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
* Results: When you run this program, you should see the values for each sensor.
*
* Example compile command:
* g++ -o program "sensors.c"
* g++ -o sensors sensors.cpp ../GoPiGo3.cpp -I..
* Example run command:
* sudo ./program
* sudo ./sensors
*
*/

#include "GoPiGo3.cpp" // for GoPiGo3
#include <GoPiGo3.h> // for GoPiGo3
#include <stdio.h> // for printf
#include <unistd.h> // for usleep
#include <signal.h> // for catching exit signals
Expand All @@ -30,14 +30,14 @@ void exit_signal_handler(int signo);

int main(){
signal(SIGINT, exit_signal_handler); // register the exit function for Ctrl+C

GPG.detect(); // Make sure that the GoPiGo3 is communicating and that the firmware is compatible with the drivers.

GPG.set_grove_type(GROVE_1, GROVE_TYPE_US);
GPG.set_grove_type(GROVE_2, GROVE_TYPE_IR_DI_REMOTE);
sensor_ultrasonic_t US;
sensor_infrared_gobox_t IR;

while(true){
int USerror = GPG.get_grove_value(GROVE_1, &US);
int IRerror = GPG.get_grove_value(GROVE_2, &IR);
Expand Down
Loading

0 comments on commit 625d817

Please sign in to comment.