From 6449f7c1164b1035fbd6de27fba610fa22906313 Mon Sep 17 00:00:00 2001 From: Benjamin Boortz Date: Sun, 4 Jun 2017 10:57:22 +0200 Subject: [PATCH 01/12] .travis.yml added /BB --- .travis.yml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..4434d66 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,4 @@ +language: c +compiler: + - gcc +script: script/bootstrap.sh From 82e7488e90bb956eb1fe81bde0143cc705208885 Mon Sep 17 00:00:00 2001 From: Benjamin Boortz Date: Sun, 4 Jun 2017 11:05:00 +0200 Subject: [PATCH 02/12] travis ci build status added /BB --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index e5f353c..b97569c 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # HaxBot + + ## Setting up new environment * git clone https://github.com/fheutz/HaxBot.git From 75e84dc92dfc5e6678ceea1da54474e05dc0a8b5 Mon Sep 17 00:00:00 2001 From: Benjamin Boortz Date: Sun, 4 Jun 2017 13:04:31 +0200 Subject: [PATCH 03/12] #ifndef fixed /BB --- lib/DistanceSensorLib/DistanceSensorLib.h | 1 + lib/StepperLib/StepperLib.h | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/DistanceSensorLib/DistanceSensorLib.h b/lib/DistanceSensorLib/DistanceSensorLib.h index 368cf23..7637b5e 100644 --- a/lib/DistanceSensorLib/DistanceSensorLib.h +++ b/lib/DistanceSensorLib/DistanceSensorLib.h @@ -27,4 +27,5 @@ class DistanceSensor { // CONSTANTS }; + #endif diff --git a/lib/StepperLib/StepperLib.h b/lib/StepperLib/StepperLib.h index 6ccbac5..7c05d8e 100644 --- a/lib/StepperLib/StepperLib.h +++ b/lib/StepperLib/StepperLib.h @@ -1,5 +1,5 @@ -#ifndef _DISTANCE_SENSOR_LIB_ -#define _DISTANCE_SENSOR_LIB_ +#ifndef _STEPPER_LIB_ +#define _STEPPER_LIB_ #include @@ -20,7 +20,6 @@ #define defaultMotorPin7 10 // IN3 on the ULN2003 driver 2 #define defaultMotorPin8 11 // IN4 on the ULN2003 driver 2 - class Stepper { public: @@ -54,4 +53,5 @@ class Stepper { int16_t _stepperMaxSpeed = 2000; }; + #endif From 79f0ee0196e9d8b3d6959e82e47653ad2feef716 Mon Sep 17 00:00:00 2001 From: Benjamin Boortz Date: Sun, 4 Jun 2017 13:05:19 +0200 Subject: [PATCH 04/12] updated for arduino uno and moveForward /BB --- test/StepperLibDefaultTest/Makefile | 14 ++++++++------ .../StepperLibDefaultTest.cpp | 13 ++++++------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/test/StepperLibDefaultTest/Makefile b/test/StepperLibDefaultTest/Makefile index bcc25ce..bb16c0e 100644 --- a/test/StepperLibDefaultTest/Makefile +++ b/test/StepperLibDefaultTest/Makefile @@ -25,10 +25,10 @@ USER_LIB_PATH := $(realpath $(PROJECT_DIR)/lib) ### BOARD_TAG & BOARD_SUB ### for arduino nano -BOARD_TAG = nano -BOARD_SUB = atmega328 +#BOARD_TAG = nano +#BOARD_SUB = atmega328 ### for arduino uno -#BOARD_TAG = uno +BOARD_TAG = uno ### for arduino mega #BOARD_TAG = mega #BOARD_SUB = atmega2560 @@ -64,10 +64,12 @@ endif ### MONITOR_PORT ### The port your board is connected to. Using an '*' tries all the ports and finds the right one. -MONITOR_PORT = /dev/ttyUSB0 +MONITOR_PORT = /dev/ttyACM0 +#MONITOR_PORT = /dev/ttyUSB0 ### ADUINO PORT -ARDUINO_PORT = /dev/ttyUSB0 +ARDUINO_PORT = /dev/ttyACM0 +#ARDUINO_PORT = /dev/ttyUSB0 ### don't touch this CURRENT_DIR = $(shell basename $(CURDIR)) @@ -78,7 +80,7 @@ CURRENT_DIR = $(shell basename $(CURDIR)) OBJDIR = $(PROJECT_DIR)/bin/$(CURRENT_DIR)/$(BOARD_TAG) # ARDUINO_LIBS -ARDUINO_LIBS = StepperLib AccelStepper +#ARDUINO_LIBS = StepperLib AccelStepper ### path to Arduino.mk, inside the ARDMK_DIR, don't touch. include $(ARDMK_DIR)/Arduino.mk diff --git a/test/StepperLibDefaultTest/StepperLibDefaultTest.cpp b/test/StepperLibDefaultTest/StepperLibDefaultTest.cpp index ecbf5e4..a3f7c5d 100644 --- a/test/StepperLibDefaultTest/StepperLibDefaultTest.cpp +++ b/test/StepperLibDefaultTest/StepperLibDefaultTest.cpp @@ -1,21 +1,20 @@ #include -#include "StepperLib.h" +#include +#include -Stepper stepper; +Stepper *stepper; void setup() { + Serial.begin(115200); Serial.println("### START TESTCASE ###"); - Serial.begin(115200); + stepper = new Stepper; delay(1000); - } void loop() { - stepper.moveForward(1000); - - delay(1000); + stepper->moveForward(10000); } From 3a9b0611ceaeb93ad14bc5e391b2e5833b43fbc8 Mon Sep 17 00:00:00 2001 From: Benjamin Boortz Date: Sun, 4 Jun 2017 14:09:29 +0200 Subject: [PATCH 05/12] delays removed /BB --- lib/StepperLib/StepperLib.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/StepperLib/StepperLib.cpp b/lib/StepperLib/StepperLib.cpp index 84b75e9..5c13213 100644 --- a/lib/StepperLib/StepperLib.cpp +++ b/lib/StepperLib/StepperLib.cpp @@ -13,8 +13,6 @@ Stepper::Stepper() { STEPPER1 = AccelStepper(HALFSTEP, defaultMotorPin1, defaultMotorPin3, defaultMotorPin2, defaultMotorPin4); STEPPER2 = AccelStepper(HALFSTEP, defaultMotorPin5, defaultMotorPin7, defaultMotorPin6, defaultMotorPin8); - delay(1000); - initializeStepper(&STEPPER1); initializeStepper(&STEPPER2); } @@ -23,8 +21,6 @@ Stepper::Stepper(short motor1Pins[], short motor2Pins[]) { STEPPER1 = AccelStepper(HALFSTEP, motor1Pins[0], motor1Pins[2], motor1Pins[1], motor1Pins[3]); STEPPER2 = AccelStepper(HALFSTEP, motor2Pins[0], motor2Pins[2], motor2Pins[1], motor2Pins[3]); - delay(1000); - initializeStepper(&STEPPER1); initializeStepper(&STEPPER2); } From 8414474749ae84fe2e170ca0a21de0f0492e8fe7 Mon Sep 17 00:00:00 2001 From: Benjamin Boortz Date: Sun, 4 Jun 2017 14:14:05 +0200 Subject: [PATCH 06/12] HaxBot_with_DistanceSensor/ added /BB --- .../HaxBot_with_DistanceSensor.cpp | 86 ++++++++++++++++++ src/HaxBot_with_DistanceSensor/Makefile | 88 +++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 src/HaxBot_with_DistanceSensor/HaxBot_with_DistanceSensor.cpp create mode 100644 src/HaxBot_with_DistanceSensor/Makefile diff --git a/src/HaxBot_with_DistanceSensor/HaxBot_with_DistanceSensor.cpp b/src/HaxBot_with_DistanceSensor/HaxBot_with_DistanceSensor.cpp new file mode 100644 index 0000000..4de4d9a --- /dev/null +++ b/src/HaxBot_with_DistanceSensor/HaxBot_with_DistanceSensor.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +#include + + +#define DEBUG 1 +#define FORWARD 0 +#define LEFT 1 +#define RIGHT 2 +#define BACKWARD 4 + + +DistanceSensor *ds; +Stepper *stepper; +uint16_t maxRange = 200; +uint16_t loopCount = 0; +uint16_t loopMaxCount = 10000; +uint8_t turn = 0; + + + + +void setup() { + Serial.begin(115200); + + Serial.println("### START PROGRAM ###"); + + stepper = new Stepper; + ds = new DistanceSensor; + + delay(1000); + +} + + +void loop() { + if (loopCount % 100 == 0) { + if (turn == LEFT) { + stepper->turnLeft(1000); + } else if (turn == RIGHT) { + stepper->turnRight(1000); + } else { + stepper->moveForward(1000); + } + } + + + if (loopCount >= loopMaxCount) { + long distance = ds->getDistance(); + Serial.print(distance); + Serial.println(" cm"); + if (distance == 0) { + } else if (distance < 20) { + turn = LEFT; + Serial.println("# turn left"); + } else { + turn = FORWARD; + Serial.println("# move forward"); + } + loopCount = 0; + } + +/* + if (loopCount >= loopMaxCount) { + Serial.print("# loop"); + Serial.print(" - "); + Serial.print(loopCount); + long distance = ds->getDistance(); + if (distance > maxRange) { + Serial.println("not in range!"); + } else { + Serial.print(distance); + Serial.println(" cm"); + } + loopCount = 0; + } + */ + + + + loopCount++; + + +} + diff --git a/src/HaxBot_with_DistanceSensor/Makefile b/src/HaxBot_with_DistanceSensor/Makefile new file mode 100644 index 0000000..0bbfc88 --- /dev/null +++ b/src/HaxBot_with_DistanceSensor/Makefile @@ -0,0 +1,88 @@ +### DISCLAIMER +### This is an example Makefile and it MUST be configured to suit your needs. +### For detailled explanations about all the avalaible options, +### please refer to https://github.com/sudar/Arduino-Makefile/blob/master/arduino-mk-vars.md + +### PROJECT_DIR +### This is the path to where you have created/cloned your project +PROJECT_DIR = ../.. + +### AVR_GCC_VERSION +### Check if the version is equal or higher than 4.9 +AVR_GCC_VERSION := $(shell expr `avr-gcc -dumpversion | cut -f1` \>= 4.9) + +### ARDMK_DIR +### Path to the Arduino-Makefile directory. +ARDMK_DIR = $(PROJECT_DIR)/Arduino-Makefile + +### ARDUINO_DIR +### Path to the Arduino application and ressources directory. +ARDUINO_DIR = $(PROJECT_DIR)/arduino + +### USER_LIB_PATH +### Path to where the your project's libraries are stored. +USER_LIB_PATH := $(realpath $(PROJECT_DIR)/lib) + +### BOARD_TAG & BOARD_SUB +### for arduino nano +#BOARD_TAG = nano +#BOARD_SUB = atmega328 +### for arduino uno +BOARD_TAG = uno +### for arduino mega +#BOARD_TAG = mega +#BOARD_SUB = atmega2560 + +### MONITOR_BAUDRATE +### It must be set to Serial baudrate value you are using. +MONITOR_BAUDRATE = 115200 + +### AVR_TOOLS_DIR +### Path to the AVR tools directory such as avr-gcc, avr-g++, etc. +#AVR_TOOLS_DIR = /usr + +### AVRDDUDE +### Path to avrdude directory. +AVRDDUDE = /usr/bin/avrdude +AVRDUDE_CONF = /etc/avrdude.conf +#AVRDDUDE = /home/benni/Arduino/avrdude-6.3 + +### CFLAGS_STD +CFLAGS_STD = -std=gnu11 + +### CXXFLAGS_STD +CXXFLAGS_STD = -std=gnu++11 + +### CPPFLAGS +### Flags you might want to set for debugging purpose. Comment to stop. +CXXFLAGS = -pedantic -Wall -Wextra + +### If avr-gcc -v is higher than 4.9, activate coloring of the output +ifeq "$(AVR_GCC_VERSION)" "1" + CXXFLAGS += -fdiagnostics-color +endif + +### MONITOR_PORT +### The port your board is connected to. Using an '*' tries all the ports and finds the right one. +#MONITOR_PORT = /dev/ttyUSB0 +MONITOR_PORT = /dev/ttyACM0 + +### ADUINO PORT +#ARDUINO_PORT = /dev/ttyUSB0 +ARDUINO_PORT = /dev/ttyACM0 + +### don't touch this +CURRENT_DIR = $(shell basename $(CURDIR)) + +### OBJDIR +### This is were you put the binaries you just compile using 'make' +CURRENT_DIR = $(shell basename $(CURDIR)) +OBJDIR = $(PROJECT_DIR)/bin/$(CURRENT_DIR)/$(BOARD_TAG) + +# ARDUINO_LIBS +#ARDUINO_LIBS = DistanceSensorLib StepperLib AccelStepper +#ARDUINO_LIBS = StepperLib AccelStepper + +### path to Arduino.mk, inside the ARDMK_DIR, don't touch. +include $(ARDMK_DIR)/Arduino.mk + From a91decd4a1f75fd94826e4aeb3aa06baf2915721 Mon Sep 17 00:00:00 2001 From: Benjamin Boortz Date: Sun, 4 Jun 2017 18:46:33 +0200 Subject: [PATCH 07/12] debug output implemented /BB --- .../HaxBot_with_DistanceSensor.cpp | 28 +++++++------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/src/HaxBot_with_DistanceSensor/HaxBot_with_DistanceSensor.cpp b/src/HaxBot_with_DistanceSensor/HaxBot_with_DistanceSensor.cpp index 4de4d9a..b06c04c 100644 --- a/src/HaxBot_with_DistanceSensor/HaxBot_with_DistanceSensor.cpp +++ b/src/HaxBot_with_DistanceSensor/HaxBot_with_DistanceSensor.cpp @@ -4,7 +4,7 @@ #include -#define DEBUG 1 +// #define DEBUG 1 #define FORWARD 0 #define LEFT 1 #define RIGHT 2 @@ -35,6 +35,8 @@ void setup() { void loop() { + + // move robot if (loopCount % 100 == 0) { if (turn == LEFT) { stepper->turnLeft(1000); @@ -46,38 +48,28 @@ void loop() { } + // detect obstacles if (loopCount >= loopMaxCount) { long distance = ds->getDistance(); +#ifdef DEBUG Serial.print(distance); Serial.println(" cm"); +#endif if (distance == 0) { } else if (distance < 20) { turn = LEFT; +#ifdef DEBUG Serial.println("# turn left"); +#endif } else { turn = FORWARD; +#ifdef DEBUG Serial.println("# move forward"); +#endif } loopCount = 0; } -/* - if (loopCount >= loopMaxCount) { - Serial.print("# loop"); - Serial.print(" - "); - Serial.print(loopCount); - long distance = ds->getDistance(); - if (distance > maxRange) { - Serial.println("not in range!"); - } else { - Serial.print(distance); - Serial.println(" cm"); - } - loopCount = 0; - } - */ - - loopCount++; From ca369e13310dded7f3296a5dc00470da1b49738a Mon Sep 17 00:00:00 2001 From: Benjamin Boortz Date: Sun, 4 Jun 2017 21:11:54 +0200 Subject: [PATCH 08/12] method getDistanceToGo added /BB --- lib/StepperLib/StepperLib.cpp | 4 +++- lib/StepperLib/StepperLib.h | 9 +++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/StepperLib/StepperLib.cpp b/lib/StepperLib/StepperLib.cpp index 5c13213..aae992d 100644 --- a/lib/StepperLib/StepperLib.cpp +++ b/lib/StepperLib/StepperLib.cpp @@ -88,4 +88,6 @@ void Stepper::turnRight(int distance) { executeMove(); } - +uint16_t Stepper::getDistanceToGo() { + return _STEPS1; +} diff --git a/lib/StepperLib/StepperLib.h b/lib/StepperLib/StepperLib.h index 7c05d8e..593b98d 100644 --- a/lib/StepperLib/StepperLib.h +++ b/lib/StepperLib/StepperLib.h @@ -35,6 +35,7 @@ class Stepper { void moveBackward(int distance); void turnLeft(int distance); void turnRight(int distance); + uint16_t getDistanceToGo(); private: @@ -45,12 +46,12 @@ class Stepper { AccelStepper STEPPER1; AccelStepper STEPPER2; - int16_t _STEPS1 = 0; - int16_t _STEPS2 = 0; + uint16_t _STEPS1 = 0; + uint16_t _STEPS2 = 0; // CONSTANTS - int16_t _stepperSpeed = 1000; - int16_t _stepperMaxSpeed = 2000; + uint16_t _stepperSpeed = 1000; + uint16_t _stepperMaxSpeed = 2000; }; From 8aac3155d915c0084bccfed1a585fa66b35fd142 Mon Sep 17 00:00:00 2001 From: Benjamin Boortz Date: Sun, 4 Jun 2017 21:16:39 +0200 Subject: [PATCH 09/12] test cases added /BB --- test/AccelStepperTest/AccelStepperTest.cpp | 80 +++++++++++++++++ test/AccelStepperTest/Makefile | 87 +++++++++++++++++++ test/StepperLibOneTurnTest/Makefile | 87 +++++++++++++++++++ .../StepperLibOneTurnTest.cpp | 40 +++++++++ 4 files changed, 294 insertions(+) create mode 100644 test/AccelStepperTest/AccelStepperTest.cpp create mode 100644 test/AccelStepperTest/Makefile create mode 100644 test/StepperLibOneTurnTest/Makefile create mode 100644 test/StepperLibOneTurnTest/StepperLibOneTurnTest.cpp diff --git a/test/AccelStepperTest/AccelStepperTest.cpp b/test/AccelStepperTest/AccelStepperTest.cpp new file mode 100644 index 0000000..6fdce01 --- /dev/null +++ b/test/AccelStepperTest/AccelStepperTest.cpp @@ -0,0 +1,80 @@ +#include +#include +#define HALFSTEP 8 + +// motor pins +#define motorPin1 3 // IN1 on the ULN2003 driver 1 +#define motorPin2 4 // IN2 on the ULN2003 driver 1 +#define motorPin3 5 // IN3 on the ULN2003 driver 1 +#define motorPin4 6 // IN4 on the ULN2003 driver 1 + +#define motorPin5 8 // IN1 on the ULN2003 driver 2 +#define motorPin6 9 // IN2 on the ULN2003 driver 2 +#define motorPin7 10 // IN3 on the ULN2003 driver 2 +#define motorPin8 11 // IN4 on the ULN2003 driver 2 + +// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48 +AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4); +AccelStepper stepper2(HALFSTEP, motorPin5, motorPin7, motorPin6, motorPin8); + +// variables +int turnSteps = 2100; // number of steps for a 90 degree turn +int lineSteps = -6600; //number of steps to drive straight +int stepperSpeed = 1000; //speed of the stepper (steps per second) +int steps1 = 0; // keep track of the step count for motor 1 +int steps2 = 0; // keep track of the step count for motor 2 + +boolean turn1 = false; //keep track if we are turning or going straight next +boolean turn2 = false; //keep track if we are turning or going straight next + +void setup() { + delay(3000); //sime time to put the robot down after swithing it on + + stepper1.setMaxSpeed(2000.0); + stepper1.move(1); // I found this necessary + stepper1.setSpeed(stepperSpeed); + + stepper2.setMaxSpeed(2000.0); + stepper2.move(-1); // I found this necessary + stepper2.setSpeed(stepperSpeed); + +} +void loop() { + + if (steps1 == 0) { + int target = 0; + if (turn1 == true) { + target = turnSteps; + } + + else { + target = lineSteps; + } + + stepper1.move(target); + stepper1.setSpeed(stepperSpeed); + turn1 = !turn1; + } + + if (steps2 == 0) { + int target = 0; + if (turn2 == true) { + target = turnSteps; + } + + else { + target = -lineSteps; + } + + stepper2.move(target); + stepper2.setSpeed(stepperSpeed); + turn2 = !turn2; + } + + steps1 = stepper1.distanceToGo(); + steps2 = stepper2.distanceToGo(); + + stepper1.runSpeedToPosition(); + stepper2.runSpeedToPosition(); +} + diff --git a/test/AccelStepperTest/Makefile b/test/AccelStepperTest/Makefile new file mode 100644 index 0000000..bb16c0e --- /dev/null +++ b/test/AccelStepperTest/Makefile @@ -0,0 +1,87 @@ +### DISCLAIMER +### This is an example Makefile and it MUST be configured to suit your needs. +### For detailled explanations about all the avalaible options, +### please refer to https://github.com/sudar/Arduino-Makefile/blob/master/arduino-mk-vars.md + +### PROJECT_DIR +### This is the path to where you have created/cloned your project +PROJECT_DIR = ../.. + +### AVR_GCC_VERSION +### Check if the version is equal or higher than 4.9 +AVR_GCC_VERSION := $(shell expr `avr-gcc -dumpversion | cut -f1` \>= 4.9) + +### ARDMK_DIR +### Path to the Arduino-Makefile directory. +ARDMK_DIR = $(PROJECT_DIR)/Arduino-Makefile + +### ARDUINO_DIR +### Path to the Arduino application and ressources directory. +ARDUINO_DIR = $(PROJECT_DIR)/arduino + +### USER_LIB_PATH +### Path to where the your project's libraries are stored. +USER_LIB_PATH := $(realpath $(PROJECT_DIR)/lib) + +### BOARD_TAG & BOARD_SUB +### for arduino nano +#BOARD_TAG = nano +#BOARD_SUB = atmega328 +### for arduino uno +BOARD_TAG = uno +### for arduino mega +#BOARD_TAG = mega +#BOARD_SUB = atmega2560 + +### MONITOR_BAUDRATE +### It must be set to Serial baudrate value you are using. +MONITOR_BAUDRATE = 115200 + +### AVR_TOOLS_DIR +### Path to the AVR tools directory such as avr-gcc, avr-g++, etc. +#AVR_TOOLS_DIR = /usr + +### AVRDDUDE +### Path to avrdude directory. +AVRDDUDE = /usr/bin/avrdude +AVRDUDE_CONF = /etc/avrdude.conf +#AVRDDUDE = /home/benni/Arduino/avrdude-6.3 + +### CFLAGS_STD +CFLAGS_STD = -std=gnu11 + +### CXXFLAGS_STD +CXXFLAGS_STD = -std=gnu++11 + +### CPPFLAGS +### Flags you might want to set for debugging purpose. Comment to stop. +CXXFLAGS = -pedantic -Wall -Wextra + +### If avr-gcc -v is higher than 4.9, activate coloring of the output +ifeq "$(AVR_GCC_VERSION)" "1" + CXXFLAGS += -fdiagnostics-color +endif + +### MONITOR_PORT +### The port your board is connected to. Using an '*' tries all the ports and finds the right one. +MONITOR_PORT = /dev/ttyACM0 +#MONITOR_PORT = /dev/ttyUSB0 + +### ADUINO PORT +ARDUINO_PORT = /dev/ttyACM0 +#ARDUINO_PORT = /dev/ttyUSB0 + +### don't touch this +CURRENT_DIR = $(shell basename $(CURDIR)) + +### OBJDIR +### This is were you put the binaries you just compile using 'make' +CURRENT_DIR = $(shell basename $(CURDIR)) +OBJDIR = $(PROJECT_DIR)/bin/$(CURRENT_DIR)/$(BOARD_TAG) + +# ARDUINO_LIBS +#ARDUINO_LIBS = StepperLib AccelStepper + +### path to Arduino.mk, inside the ARDMK_DIR, don't touch. +include $(ARDMK_DIR)/Arduino.mk + diff --git a/test/StepperLibOneTurnTest/Makefile b/test/StepperLibOneTurnTest/Makefile new file mode 100644 index 0000000..f93ea1e --- /dev/null +++ b/test/StepperLibOneTurnTest/Makefile @@ -0,0 +1,87 @@ +### DISCLAIMER +### This is an example Makefile and it MUST be configured to suit your needs. +### For detailled explanations about all the avalaible options, +### please refer to https://github.com/sudar/Arduino-Makefile/blob/master/arduino-mk-vars.md + +### PROJECT_DIR +### This is the path to where you have created/cloned your project +PROJECT_DIR = ../.. + +### AVR_GCC_VERSION +### Check if the version is equal or higher than 4.9 +AVR_GCC_VERSION := $(shell expr `avr-gcc -dumpversion | cut -f1` \>= 4.9) + +### ARDMK_DIR +### Path to the Arduino-Makefile directory. +ARDMK_DIR = $(PROJECT_DIR)/Arduino-Makefile + +### ARDUINO_DIR +### Path to the Arduino application and ressources directory. +ARDUINO_DIR = $(PROJECT_DIR)/arduino + +### USER_LIB_PATH +### Path to where the your project's libraries are stored. +USER_LIB_PATH := $(realpath $(PROJECT_DIR)/lib) + +### BOARD_TAG & BOARD_SUB +### for arduino nano +#BOARD_TAG = nano +#BOARD_SUB = atmega328 +### for arduino uno +BOARD_TAG = uno +### for arduino mega +#BOARD_TAG = mega +#BOARD_SUB = atmega2560 + +### MONITOR_BAUDRATE +### It must be set to Serial baudrate value you are using. +MONITOR_BAUDRATE = 115200 + +### AVR_TOOLS_DIR +### Path to the AVR tools directory such as avr-gcc, avr-g++, etc. +#AVR_TOOLS_DIR = /usr + +### AVRDDUDE +### Path to avrdude directory. +AVRDDUDE = /usr/bin/avrdude +AVRDUDE_CONF = /etc/avrdude.conf +#AVRDDUDE = /home/benni/Arduino/avrdude-6.3 + +### CFLAGS_STD +CFLAGS_STD = -std=gnu11 + +### CXXFLAGS_STD +CXXFLAGS_STD = -std=gnu++11 + +### CPPFLAGS +### Flags you might want to set for debugging purpose. Comment to stop. +CXXFLAGS = -pedantic -Wall -Wextra + +### If avr-gcc -v is higher than 4.9, activate coloring of the output +ifeq "$(AVR_GCC_VERSION)" "1" + CXXFLAGS += -fdiagnostics-color +endif + +### MONITOR_PORT +### The port your board is connected to. Using an '*' tries all the ports and finds the right one. +MONITOR_PORT = /dev/ttyACM0 +#MONITOR_PORT = /dev/ttyUSB0 + +### ADUINO PORT +ARDUINO_PORT = /dev/ttyACM0 +#ARDUINO_PORT = /dev/ttyUSB0 + +### don't touch this +CURRENT_DIR = $(shell basename $(CURDIR)) + +### OBJDIR +### This is were you put the binaries you just compile using 'make' +CURRENT_DIR = $(shell basename $(CURDIR)) +OBJDIR = $(PROJECT_DIR)/bin/$(CURRENT_DIR)/$(BOARD_TAG) + +# ARDUINO_LIBS +ARDUINO_LIBS = StepperLib AccelStepper + +### path to Arduino.mk, inside the ARDMK_DIR, don't touch. +include $(ARDMK_DIR)/Arduino.mk + diff --git a/test/StepperLibOneTurnTest/StepperLibOneTurnTest.cpp b/test/StepperLibOneTurnTest/StepperLibOneTurnTest.cpp new file mode 100644 index 0000000..d4225b8 --- /dev/null +++ b/test/StepperLibOneTurnTest/StepperLibOneTurnTest.cpp @@ -0,0 +1,40 @@ +#include +#include "StepperLib.h" + +short motor1Pins[4] = { 4, 5, 6, 7 }; +short motor2Pins[4] = { 8, 9, 10, 11 }; + +#define STEPS 4076 + + +uint16_t i = 0; +uint16_t iMax = 4000; + + +Stepper *stepper; + +void setup() { + Serial.begin(115200); + Serial.println("### START TESTCASE ###"); + + stepper = new Stepper(motor1Pins, motor2Pins); + + + delay(1000); + +} + +void loop() { + if (i == 0) { + delay(100); + stepper->moveForward(STEPS); + i++; + } + uint16_t distance = stepper->getDistanceToGo(); + if (distance == 0) { + i = 0; + } else { + stepper->moveForward(distance); + } +} + From 340b2738587e4a4a698361eda1542a423e5da789 Mon Sep 17 00:00:00 2001 From: Benjamin Boortz Date: Sun, 4 Jun 2017 21:57:53 +0200 Subject: [PATCH 10/12] updated for arduino uno /BB --- .../DistanceSensorLibDefaultTest.cpp | 2 +- test/DistanceSensorLibDefaultTest/Makefile | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/test/DistanceSensorLibDefaultTest/DistanceSensorLibDefaultTest.cpp b/test/DistanceSensorLibDefaultTest/DistanceSensorLibDefaultTest.cpp index a727891..d0dcc6e 100644 --- a/test/DistanceSensorLibDefaultTest/DistanceSensorLibDefaultTest.cpp +++ b/test/DistanceSensorLibDefaultTest/DistanceSensorLibDefaultTest.cpp @@ -5,9 +5,9 @@ DistanceSensor ds; uint16_t maxRange = 200; void setup() { + Serial.begin(115200); Serial.println("### START TESTCASE ###"); - Serial.begin(115200); delay(1000); } diff --git a/test/DistanceSensorLibDefaultTest/Makefile b/test/DistanceSensorLibDefaultTest/Makefile index bd854ac..39c1484 100644 --- a/test/DistanceSensorLibDefaultTest/Makefile +++ b/test/DistanceSensorLibDefaultTest/Makefile @@ -25,10 +25,10 @@ USER_LIB_PATH := $(realpath $(PROJECT_DIR)/lib) ### BOARD_TAG & BOARD_SUB ### for arduino nano -BOARD_TAG = nano -BOARD_SUB = atmega328 +#BOARD_TAG = nano +#BOARD_SUB = atmega328 ### for arduino uno -#BOARD_TAG = uno +BOARD_TAG = uno ### for arduino mega #BOARD_TAG = mega #BOARD_SUB = atmega2560 @@ -64,10 +64,12 @@ endif ### MONITOR_PORT ### The port your board is connected to. Using an '*' tries all the ports and finds the right one. -MONITOR_PORT = /dev/ttyUSB0 +MONITOR_PORT = /dev/ttyACM0 +#MONITOR_PORT = /dev/ttyUSB0 ### ADUINO PORT -ARDUINO_PORT = /dev/ttyUSB0 +ARDUINO_PORT = /dev/ttyACM0 +#ARDUINO_PORT = /dev/ttyUSB0 ### don't touch this CURRENT_DIR = $(shell basename $(CURDIR)) From 5a82ed1695190e76a221d5f2a968352c208f2b3f Mon Sep 17 00:00:00 2001 From: Benjamin Boortz Date: Sun, 4 Jun 2017 21:58:33 +0200 Subject: [PATCH 11/12] use executeMove instance of moveForward /BB --- test/StepperLibOneTurnTest/StepperLibOneTurnTest.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/StepperLibOneTurnTest/StepperLibOneTurnTest.cpp b/test/StepperLibOneTurnTest/StepperLibOneTurnTest.cpp index d4225b8..a90d41e 100644 --- a/test/StepperLibOneTurnTest/StepperLibOneTurnTest.cpp +++ b/test/StepperLibOneTurnTest/StepperLibOneTurnTest.cpp @@ -34,7 +34,8 @@ void loop() { if (distance == 0) { i = 0; } else { - stepper->moveForward(distance); +// stepper->moveForward(distance); + stepper->executeMove(); } } From 03337c6cc60636fb27528563ff14f4ccc8771d2f Mon Sep 17 00:00:00 2001 From: Benjamin Boortz Date: Tue, 6 Jun 2017 19:57:23 +0200 Subject: [PATCH 12/12] final release for this mvp /BB --- .../HaxBot_with_DistanceSensor.cpp | 120 ++++++++++++++---- 1 file changed, 97 insertions(+), 23 deletions(-) diff --git a/src/HaxBot_with_DistanceSensor/HaxBot_with_DistanceSensor.cpp b/src/HaxBot_with_DistanceSensor/HaxBot_with_DistanceSensor.cpp index b06c04c..fd37c1b 100644 --- a/src/HaxBot_with_DistanceSensor/HaxBot_with_DistanceSensor.cpp +++ b/src/HaxBot_with_DistanceSensor/HaxBot_with_DistanceSensor.cpp @@ -5,6 +5,8 @@ // #define DEBUG 1 +#define STEPS 4076 / 4 + #define FORWARD 0 #define LEFT 1 #define RIGHT 2 @@ -13,64 +15,136 @@ DistanceSensor *ds; Stepper *stepper; -uint16_t maxRange = 200; uint16_t loopCount = 0; uint16_t loopMaxCount = 10000; -uint8_t turn = 0; +uint16_t stepOfMove = 0; +uint8_t turn = FORWARD; void setup() { Serial.begin(115200); - Serial.println("### START PROGRAM ###"); stepper = new Stepper; ds = new DistanceSensor; + // Timer0 is already used for millis() - we'll just interrupt somewhere + // in the middle and call the "Compare A" function below +// OCR0A = 0xAF; +// TIMSK0 |= _BV(OCIE0A); + delay(1000); } +// Interrupt is called once a millisecond, looks for any new GPS data, and stores it +//SIGNAL(TIMER0_COMPA_vect) { +// Serial.println("timer"); +//} -void loop() { - // move robot - if (loopCount % 100 == 0) { - if (turn == LEFT) { - stepper->turnLeft(1000); - } else if (turn == RIGHT) { - stepper->turnRight(1000); + +void turnLeft(int distance) { + if (stepOfMove == 0) { + stepper->turnLeft(distance); + stepOfMove = 1; + } else { + uint16_t distance = stepper->getDistanceToGo(); + if (distance == 0) { + stepOfMove = 0; } else { - stepper->moveForward(1000); + stepper->executeMove(); } } +} +void turnRight(int distance) { + if (stepOfMove == 0) { + stepper->turnRight(distance); + stepOfMove = 1; + } else { + uint16_t distance = stepper->getDistanceToGo(); + if (distance == 0) { + stepOfMove = 0; + } else { + stepper->executeMove(); + } + } + +} + +void moveForward(int distance) { + if (stepOfMove == 0) { + stepper->moveForward(distance); + stepOfMove = 1; + } else { + uint16_t distance = stepper->getDistanceToGo(); + if (distance == 0) { + stepOfMove = 0; + } else { + stepper->executeMove(); + } + } +} + +void moveBackward(int distance) { + if (stepOfMove == 0) { + stepper->moveBackward(distance); + stepOfMove = 1; + } else { + uint16_t distance = stepper->getDistanceToGo(); + if (distance == 0) { + stepOfMove = 0; + } else { + stepper->executeMove(); + } + } +} + + +void loop() { + + + // detect obstacles + if (loopCount % 10001 == 0) { + long distance = ds->getDistance(); + } // detect obstacles - if (loopCount >= loopMaxCount) { + if (loopCount % 10001 == 0) { long distance = ds->getDistance(); -#ifdef DEBUG - Serial.print(distance); - Serial.println(" cm"); -#endif + Serial.println(distance); if (distance == 0) { } else if (distance < 20) { - turn = LEFT; -#ifdef DEBUG - Serial.println("# turn left"); -#endif + if (turn == FORWARD) { + turn = LEFT; + stepOfMove = 0; + } else { + turn = FORWARD; + } } else { turn = FORWARD; -#ifdef DEBUG - Serial.println("# move forward"); -#endif } loopCount = 0; } + // move robot + if (loopCount % 100 == 0) { + if (turn == LEFT) { + turnLeft(STEPS); + } else if (turn == RIGHT) { + turnRight(STEPS); + } else if (turn == FORWARD) { + moveForward(STEPS); + } else if (turn == BACKWARD) { + moveBackward(STEPS); + } + } + + loopCount++;