initial commit

This commit is contained in:
jpunkt 2021-12-14 20:07:55 +01:00
commit ae9c6451a9
9 changed files with 270 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

7
.vscode/extensions.json vendored Normal file
View File

@ -0,0 +1,7 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
]
}

15
include/Motor.h Normal file
View File

@ -0,0 +1,15 @@
#include <Arduino.h>
class Motor
{
private:
uint8_t pwm_pin;
uint8_t in1_pin;
uint8_t in2_pin;
public:
Motor(uint8_t pwm_pin, uint8_t in1_pin, uint8_t in2_pin);
void setup();
void run(uint8_t speed, bool forward);
void stop(bool freewheel);
};

39
include/README Normal file
View File

@ -0,0 +1,39 @@
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

46
lib/README Normal file
View File

@ -0,0 +1,46 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.
The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").
For example, see a structure of the following two libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://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
; https://docs.platformio.org/page/projectconf.html
[env:teensy41]
platform = teensy
board = teensy41
framework = arduino
upload_protocol = teensy-cli

34
src/Motor.cpp Normal file
View File

@ -0,0 +1,34 @@
#include "Motor.h"
Motor::Motor(uint8_t pwm, uint8_t in1, uint8_t in2)
{
pwm_pin = pwm;
in1_pin = in1;
in2_pin = in2;
}
void Motor::setup()
{
pinMode(pwm_pin, OUTPUT);
pinMode(in1_pin, OUTPUT);
pinMode(in2_pin, OUTPUT);
}
void Motor::run(uint8_t speed, bool forward)
{
if (speed == 0) {
return stop(false);
}
digitalWrite(in1_pin, forward ? HIGH : LOW);
digitalWrite(in2_pin, forward ? LOW : HIGH);
analogWrite(pwm_pin, speed);
}
void Motor::stop(bool freewheel)
{
analogWrite(pwm_pin, 0);
digitalWrite(in1_pin, freewheel ? LOW : HIGH);
digitalWrite(in2_pin, freewheel ? LOW : HIGH);
}

97
src/main.cpp Normal file
View File

@ -0,0 +1,97 @@
#include <Arduino.h>
#include "Motor.h"
// Vertical motor top
#define VERT_UP_PWM 3
#define VERT_UP_AIN2 4
#define VERT_UP_AIN1 5
// Vertical motor bottom
#define VERT_DOWN_PWM 9
#define VERT_DOWN_AIN2 7
#define VERT_DOWN_AIN1 8
// Horizontal motor left
#define HORZ_LEFT_PWM 24
#define HORZ_LEFT_AIN2 25
#define HORZ_LEFT_AIN1 26
// Horizontal motor right
#define HORZ_RIGHT_PWM 29
#define HORZ_RIGHT_AIN2 27
#define HORZ_RIGHT_AIN1 28
// Vertical sensors
#define VERT_END_OUTER 40
#define VERT_END_INNER 39
#define VERT_CNT_OUTER 38
#define VERT_CNT_INNER 37
// Horizontal sensors
#define HORZ_END_OUTER 33
#define HORZ_END_INNER 34
#define HORZ_CNT_INNER 35
#define HORZ_CNT_OUTER 36
volatile int32_t hor_pos;
volatile int32_t vert_pos;
Motor vert_up(VERT_UP_PWM, VERT_UP_AIN1, VERT_UP_AIN2);
Motor vert_down(VERT_DOWN_PWM, VERT_DOWN_AIN1, VERT_DOWN_AIN2);
Motor horz_left(HORZ_LEFT_PWM, HORZ_LEFT_AIN1, HORZ_LEFT_AIN2);
Motor horz_right(HORZ_RIGHT_PWM, HORZ_RIGHT_AIN1, HORZ_RIGHT_AIN2);
int32_t count(int pinA, int pinB) {
if (digitalRead(pinA)) return digitalRead(pinB) ? 1 : -1;
else return digitalRead(pinB) ? -1 : 1;
}
void hor_count() {
hor_pos += count(HORZ_CNT_INNER, HORZ_CNT_OUTER);
}
void vert_count() {
vert_pos += count(VERT_CNT_INNER, VERT_CNT_OUTER);
}
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
hor_pos = 0;
vert_pos = 0;
vert_up.setup();
vert_down.setup();
vert_up.stop(true);
vert_down.stop(true);
horz_left.setup();
horz_right.setup();
horz_left.stop(true);
horz_right.stop(true);
pinMode(HORZ_CNT_INNER, INPUT);
pinMode(HORZ_CNT_OUTER, INPUT);
pinMode(VERT_CNT_INNER, INPUT);
pinMode(VERT_CNT_OUTER, INPUT);
digitalWrite(HORZ_CNT_INNER, LOW);
digitalWrite(HORZ_CNT_OUTER, LOW);
digitalWrite(VERT_CNT_INNER, LOW);
digitalWrite(VERT_CNT_OUTER, LOW);
attachInterrupt(digitalPinToInterrupt(HORZ_CNT_INNER), hor_count, CHANGE);
// attachInterrupt(digitalPinToInterrupt(HORZ_CNT_OUTER), hor_count, CHANGE);
attachInterrupt(digitalPinToInterrupt(VERT_CNT_INNER), vert_count, CHANGE);
// attachInterrupt(digitalPinToInterrupt(VERT_CNT_OUTER), vert_count, CHANGE);
}
void loop() {
// Serial.printf("hor_count1: %i | \t hor_count2: %i | \t vert_count1: %i | \t vert_count2: %i \n", digitalRead(HORZ_CNT_INNER), digitalRead(HORZ_CNT_OUTER), digitalRead(VERT_CNT_INNER), digitalRead(VERT_CNT_OUTER));
Serial.printf("hor_count %i | \t vert_count %i \n", hor_pos, vert_pos);
delay(500);
}

11
test/README Normal file
View File

@ -0,0 +1,11 @@
This directory is intended for PlatformIO Unit Testing and project tests.
Unit Testing is a software testing method by which individual units of
source code, sets of one or more MCU program modules together with associated
control data, usage procedures, and operating procedures, are tested to
determine whether they are fit for use. Unit testing finds problems early
in the development cycle.
More information about PlatformIO Unit Testing:
- https://docs.platformio.org/page/plus/unit-testing.html