Hindernisvermeidung funktioniert

This commit is contained in:
Phillip Kühne 2018-06-21 16:01:21 +02:00
commit 7e67dcb0a0
8 changed files with 2161 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.pioenvs
.piolibdeps

67
.travis.yml Normal file
View File

@ -0,0 +1,67 @@
# Continuous Integration (CI) is the practice, in software
# engineering, of merging all developer working copies with a shared mainline
# several times a day < http://docs.platformio.org/page/ci/index.html >
#
# Documentation:
#
# * Travis CI Embedded Builds with PlatformIO
# < https://docs.travis-ci.com/user/integration/platformio/ >
#
# * PlatformIO integration with Travis CI
# < http://docs.platformio.org/page/ci/travis.html >
#
# * User Guide for `platformio ci` command
# < http://docs.platformio.org/page/userguide/cmd_ci.html >
#
#
# Please choice one of the following templates (proposed below) and uncomment
# it (remove "# " before each line) or use own configuration according to the
# Travis CI documentation (see above).
#
#
# Template #1: General project. Test it using existing `platformio.ini`.
#
# language: python
# python:
# - "2.7"
#
# sudo: false
# cache:
# directories:
# - "~/.platformio"
#
# install:
# - pip install -U platformio
# - platformio update
#
# script:
# - platformio run
#
# Template #2: The project is intended to by used as a library with examples
#
# language: python
# python:
# - "2.7"
#
# sudo: false
# cache:
# directories:
# - "~/.platformio"
#
# env:
# - PLATFORMIO_CI_SRC=path/to/test/file.c
# - PLATFORMIO_CI_SRC=examples/file.ino
# - PLATFORMIO_CI_SRC=path/to/test/directory
#
# install:
# - pip install -U platformio
# - platformio update
#
# script:
# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N

36
lib/readme.txt Normal file
View File

@ -0,0 +1,36 @@
This directory is intended for the project specific (private) libraries.
PlatformIO will compile them to static libraries and link to executable file.
The source code of each library should be placed in separate directory, like
"lib/private_lib/[here are source files]".
For example, see how can be organized `Foo` and `Bar` libraries:
|--lib
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |- readme.txt --> THIS FILE
|- platformio.ini
|--src
|- main.c
Then in `src/main.c` you should use:
#include <Foo.h>
#include <Bar.h>
// rest H/C/CPP code
PlatformIO will find your libraries automatically, configure preprocessor's
include paths and build them.
More information about PlatformIO Library Dependency Finder
- http://docs.platformio.org/page/librarymanager/ldf.html

16
platformio.ini Normal file
View File

@ -0,0 +1,16 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; http://docs.platformio.org/page/projectconf.html
[env:uno]
platform = atmelavr
board = uno
lib_deps =
1507
framework = arduino

104
platformio.sublime-project Normal file
View File

@ -0,0 +1,104 @@
{
"build_systems":
[
{
"cmd":
[
"platformio",
"-f", "-c", "sublimetext",
"run"
],
"name": "PlatformIO",
"variants":
[
{
"cmd":
[
"platformio",
"-f", "-c", "sublimetext",
"run"
],
"name": "Build"
},
{
"cmd":
[
"platformio",
"-f", "-c", "sublimetext",
"run",
"--target",
"clean"
],
"name": "Clean"
},
{
"cmd":
[
"platformio",
"-f", "-c", "sublimetext",
"test"
],
"name": "Test"
},
{
"cmd":
[
"platformio",
"-f", "-c", "sublimetext",
"run",
"--target",
"upload"
],
"name": "Upload"
},
{
"cmd":
[
"platformio",
"-f", "-c", "sublimetext",
"run",
"--target",
"program"
],
"name": "Upload using Programmer"
},
{
"cmd":
[
"platformio",
"-f", "-c", "sublimetext",
"run",
"--target",
"uploadfs"
],
"name": "Upload SPIFFS image"
},
{
"cmd":
[
"platformio",
"-f", "-c", "sublimetext",
"update"
],
"name": "Update platforms and libraries"
}
],
"working_dir": "${project_path:${folder}}",
"selector": "source.c, source.c++",
"path": "/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl"
}
],
"folders":
[
{
"path": "."
}
],
"settings":
{
"sublimegdb_workingdir": "/home/phillipk/Code/Robotik",
"sublimegdb_exec_cmd": "-exec-continue",
"sublimegdb_commandline": "/usr/bin/platformio -f -c sublimetext debug --interface=gdb --interpreter=mi -x .pioinit"
}
}

1727
platformio.sublime-workspace Normal file

File diff suppressed because it is too large Load Diff

10
portmappings.md Normal file
View File

@ -0,0 +1,10 @@
## Mappings
**PWM ist rückwärts invertiert**
| Pin | Bedeutung |
|-----|-----|
|PIN_IN1| PWM Motor rechts |
|PIN_IN2| Richtung Motor Rechts (LOW: Vorwärts, HIGH: Rückwärts)|
|PIN_IN3| PWM Motor Links |
|PIN_IN2| Richtung Motor Rechts (LOW: Vorwärts, HIGH: Rückwärts)|

199
src/main.cpp Normal file
View File

@ -0,0 +1,199 @@
#include <Arduino.h>
#include <Ultrasonic.h>
//// Adafruit Display
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
const int PIN_ENA = 0;
const int PIN_ENB = 1;
const int PIN_IN1 = 3;
const int PIN_IN2 = 5;
const int PIN_IN3 = 9;
const int PIN_IN4 = 10;
const int SERVO_PIN = 7;
Ultrasonic ultrasonic(A2,A3);
int distance = 21;
// put your setup code here, to run once:
void setup() {
Serial.begin(9600);
pinMode(PIN_ENA,OUTPUT);
pinMode(PIN_ENB, OUTPUT);
pinMode(PIN_IN1, OUTPUT);
pinMode(PIN_IN2, OUTPUT);
pinMode(PIN_IN3, OUTPUT);
pinMode(PIN_IN4, OUTPUT);
pinMode(SERVO_PIN, OUTPUT);
digitalWrite(PIN_ENA, HIGH);
digitalWrite(PIN_ENB, HIGH);
// Display Initialisieren
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("2WD China Bots");
//display.setTextSize(3);
display.println("HTWK-");
display.setTextSize(2);
display.println("Leipzig");
display.display();
}
/**
* @param leftMotor -255 bis 255, Stärke
* @param rightMotor -255 bis 255, Stärke
*/
void setMotors(int leftMotor, int rightMotor) {
if (rightMotor>=0)
{
analogWrite(PIN_IN2, rightMotor);
digitalWrite(PIN_IN1, LOW);
} else {
analogWrite(PIN_IN2, rightMotor);
digitalWrite(PIN_IN1, HIGH);
}
if (leftMotor>=0)
{
analogWrite(PIN_IN4, leftMotor);
digitalWrite(PIN_IN3, LOW);
} else {
analogWrite(PIN_IN4, leftMotor);
digitalWrite(PIN_IN3, HIGH);
}
}
void stopMotors() {
digitalWrite(PIN_IN1,LOW);
digitalWrite(PIN_IN2,LOW);
digitalWrite(PIN_IN3,LOW);
digitalWrite(PIN_IN4,LOW);
digitalWrite(PIN_ENA,LOW);
digitalWrite(PIN_ENB,LOW);
}
void enableHigh() {
digitalWrite(PIN_ENA,HIGH);
digitalWrite(PIN_ENB,HIGH);
}
void setServo(int degrees) {
int lenMicroSecondsOfPulse = (float) degrees/ 90.0 * 1000 + 300;
int lenMicroSecondsOfPeriod = 20 * 1000; // 20 milliseconds (ms)
for (int i = 0; i < 200; ++i) {
digitalWrite(SERVO_PIN, HIGH);
// Delay for the length of the pulse
delayMicroseconds(lenMicroSecondsOfPulse);
// Turn the voltage low for the remainder of the pulse
digitalWrite(SERVO_PIN, LOW);
// Delay this loop for the remainder of the period so we don't
// send the next signal too soon or too late
delayMicroseconds(lenMicroSecondsOfPeriod - lenMicroSecondsOfPulse);
}
}
int lookLeft(){
setServo(180);
delay(1000);
return ultrasonic.distanceRead();
}
int lookRight(){
setServo(0);
delay(1000);
return ultrasonic.distanceRead();
}
void loop() {
enableHigh();
setServo(90);
distance = ultrasonic.distanceRead();
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Abstand: ");
display.setTextSize(3);
display.print(distance);
display.setTextSize(2);
display.println(" cm");
display.display();
Serial.println(distance);
if (distance<30 && distance>0) {
setMotors(0, 0);
if(lookLeft()>lookRight()){
setServo(90);
while(distance<50) {
distance = ultrasonic.distanceRead();
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Abstand (turning left): ");
display.setTextSize(3);
display.print(distance);
display.setTextSize(2);
display.println(" cm");
display.display();
Serial.println("Turning left: ");
Serial.println(distance);
setMotors(-120,0);
delay(10);
}
delay(500);
} else {
setServo(90);
while(distance<50) {
distance = ultrasonic.distanceRead();
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Abstand (turning right): ");
display.setTextSize(3);
display.print(distance);
display.setTextSize(2);
display.println(" cm");
display.display();
Serial.println("Turning right: ");
Serial.println(distance);
setMotors(0,-120);
delay(10);
}
delay(500);
}
} else {
if((distance>215)||(distance<=0)){
setMotors(255, 255);
} else {
setMotors(distance+70, distance+70);
}
}
delay(10);
}