diff --git a/Software/C/GoPiGo3.cpp b/Software/C/GoPiGo3.cpp index 65107f9..d653901 100644 --- a/Software/C/GoPiGo3.cpp +++ b/Software/C/GoPiGo3.cpp @@ -571,7 +571,7 @@ int GoPiGo3::get_motor_encoder(uint8_t port, int32_t &value){ } uint32_t Value; int res = spi_read_32(msg_type, Value); - value = (int)(Value / MOTOR_TICKS_PER_DEGREE); + value = (int32_t)(Value) / MOTOR_TICKS_PER_DEGREE; return res; } diff --git a/Software/C/GoPiGo3.h b/Software/C/GoPiGo3.h index 9b4e961..0980890 100644 --- a/Software/C/GoPiGo3.h +++ b/Software/C/GoPiGo3.h @@ -49,13 +49,13 @@ #define ERROR_GROVE_TYPE_MISMATCH -6 #define ERROR_GROVE_DATA_ERROR -7 -int spi_file_handle = -1; // SPI file handle -struct spi_ioc_transfer spi_xfer_struct; // SPI transfer struct -uint8_t spi_array_out[LONGEST_SPI_TRANSFER]; // SPI out array -uint8_t spi_array_in[LONGEST_SPI_TRANSFER]; // SPI in array +static int spi_file_handle = -1; // SPI file handle +static struct spi_ioc_transfer spi_xfer_struct; // SPI transfer struct +static uint8_t spi_array_out[LONGEST_SPI_TRANSFER]; // SPI out array +static uint8_t spi_array_in[LONGEST_SPI_TRANSFER]; // SPI in array // Set up SPI. Open the file, and define the configuration. -int spi_setup(){ +static int spi_setup(){ spi_file_handle = open(SPIDEV_FILE_NAME, O_RDWR); if (spi_file_handle < 0){ @@ -71,7 +71,7 @@ int spi_setup(){ } // Transfer length number of bytes. Write from outArray, read to inArray. -int spi_transfer_array(uint8_t length, uint8_t *outArray, uint8_t *inArray){ +static int spi_transfer_array(uint8_t length, uint8_t *outArray, uint8_t *inArray){ spi_xfer_struct.len = length; spi_xfer_struct.tx_buf = (unsigned long)outArray; spi_xfer_struct.rx_buf = (unsigned long)inArray; @@ -84,13 +84,13 @@ int spi_transfer_array(uint8_t length, uint8_t *outArray, uint8_t *inArray){ } // Function to call if an error occured that can not be resolved, such as failure to set up SPI -void fatal_error(const char *error){ +static void fatal_error(const char *error){ throw std::runtime_error(error); } //struct timespec _time; -struct timeval _time; -double get_time(){ +static struct timeval _time; +static double get_time(){ //clock_gettime(CLOCK_MONOTONIC_RAW, &_time); gettimeofday(&_time, NULL); return _time.tv_sec + (_time.tv_usec / 1000000); @@ -252,8 +252,8 @@ class GoPiGo3{ float WHEEL_BASE_CIRCUMFERENCE = WHEEL_BASE_WIDTH * M_PI; // pi from cmath float WHEEL_CIRCUMFERENCE = WHEEL_DIAMETER * M_PI; // pi from cmath int MOTOR_GEAR_RATIO = 120; - int ENCODER_TICKS_PER_ROTATION = 6; // default GoPiGo3 has 6 ticks, 16 ticks if in .list_of_serial_numbers.pkl file - int MOTOR_TICKS_PER_DEGREE = ((MOTOR_GEAR_RATIO * ENCODER_TICKS_PER_ROTATION) / 360.0); // ticks per degree of wheel shaft rotation + int ENCODER_TICKS_PER_ROTATION = 6; // default GoPiGo3 has 6 ticks, 16 ticks if in .gpg3_list_of_serial_numbers.pkl file + float MOTOR_TICKS_PER_DEGREE = ((MOTOR_GEAR_RATIO * ENCODER_TICKS_PER_ROTATION) / 360.0); // ticks per degree of wheel shaft rotation // Confirm that the BrickPi3 is connected and up-to-date int detect(bool critical = true); diff --git a/Software/C/README.md b/Software/C/README.md new file mode 100644 index 0000000..a062a76 --- /dev/null +++ b/Software/C/README.md @@ -0,0 +1,77 @@ +# C++ programs for GoPiGo3 + +The C++ GoPiGo3 API is defined by the files: + +``` +GoPiGo3.h +GoPiGo3.cpp +``` + +and libgopigo3.so is the built link library for the API. + +Examples/ contains C++ programs that use the C++ GoPiGo3 API: + +``` +info Read GoPiGo3 Board Info +vbatt Read the GoPiGo3 battery voltage +leds Flash GoPiGo3 red LEDs +drive Use keyboard to drive your GoPiGo3 +servos Rotate servos through range +grove_led Blink Grove LED +sensors Use Grove Ultrasonic and IR Receiver +i2c Drive PCA9570 I2C Expander +motors Gently spin left wheel forward, + watch encoder values, right wheel turn +``` + + + +### To build + +1) install cmake: + +``` + sudo apt-get install cmake +``` +2) run cmake to build make files + +``` + cmake CMakeLists.txt +``` + +5) run make to build the examples and the libgopigo3.so link library + +``` + make +``` + +6) try out the info executable: + +``` + ./info +``` + +7) Try out the other executables + + +NOTE: To clean the folder of all build products and binaries + +``` + ./clean.sh +``` + + +============== + +NOTE: To try your hand with the C++ GoPiGo3 API: + +``` +mkdir test +cd test +(create myrobot.cpp, and reference the the API with #include GoPiGo3.h) +g++ -o myrobot myrobot.cpp ../GoPiGo3.cpp -I.. +./myrobot +``` + +The executable will be in the same folder as your C++ program (test/). + diff --git a/Software/C/clean.sh b/Software/C/clean.sh new file mode 100755 index 0000000..c6ddaa2 --- /dev/null +++ b/Software/C/clean.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# FILE: clean.sh + +# Use to fully clean out all cmake and make products (restore to original contents) + +echo -e "Removing all built executables" +make clean +echo -e "Removing all cmake generated files" +rm CMakeCache.txt +rm -rf CMakeFiles +rm *.cmake +rm Makefile +echo -e "See README.txt for build instructions" + +