Skip to content
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: c
compiler:
- gcc
script: script/bootstrap.sh
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# HaxBot

<a href='https://travis-ci.org/sebdah/git-pylint-commit-hook'><img src='https://secure.travis-ci.org/bboortz/HaxBot.png?branch=master'></a>

## Setting up new environment

* git clone https://github.com/fheutz/HaxBot.git
Expand Down
1 change: 1 addition & 0 deletions lib/DistanceSensorLib/DistanceSensorLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ class DistanceSensor {
// CONSTANTS
};


#endif
8 changes: 3 additions & 5 deletions lib/StepperLib/StepperLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -92,4 +88,6 @@ void Stepper::turnRight(int distance) {
executeMove();
}


uint16_t Stepper::getDistanceToGo() {
return _STEPS1;
}
15 changes: 8 additions & 7 deletions lib/StepperLib/StepperLib.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _DISTANCE_SENSOR_LIB_
#define _DISTANCE_SENSOR_LIB_
#ifndef _STEPPER_LIB_
#define _STEPPER_LIB_


#include <Arduino.h>
Expand All @@ -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:
Expand All @@ -36,6 +35,7 @@ class Stepper {
void moveBackward(int distance);
void turnLeft(int distance);
void turnRight(int distance);
uint16_t getDistanceToGo();

private:

Expand All @@ -46,12 +46,13 @@ 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;
};


#endif
152 changes: 152 additions & 0 deletions src/HaxBot_with_DistanceSensor/HaxBot_with_DistanceSensor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#include <Arduino.h>
#include <AccelStepper.h>
#include <StepperLib.h>
#include <DistanceSensorLib.h>


// #define DEBUG 1
#define STEPS 4076 / 4

#define FORWARD 0
#define LEFT 1
#define RIGHT 2
#define BACKWARD 4


DistanceSensor *ds;
Stepper *stepper;
uint16_t loopCount = 0;
uint16_t loopMaxCount = 10000;
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 turnLeft(int distance) {
if (stepOfMove == 0) {
stepper->turnLeft(distance);
stepOfMove = 1;
} else {
uint16_t distance = stepper->getDistanceToGo();
if (distance == 0) {
stepOfMove = 0;
} else {
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 % 10001 == 0) {
long distance = ds->getDistance();
Serial.println(distance);
if (distance == 0) {
} else if (distance < 20) {
if (turn == FORWARD) {
turn = LEFT;
stepOfMove = 0;
} else {
turn = FORWARD;
}
} else {
turn = FORWARD;
}
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++;


}

88 changes: 88 additions & 0 deletions src/HaxBot_with_DistanceSensor/Makefile
Original file line number Diff line number Diff line change
@@ -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

Loading