Skip to content

Commit

Permalink
stepper motor program
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimsmary committed Jul 27, 2022
1 parent d8d32c8 commit 2038845
Show file tree
Hide file tree
Showing 9 changed files with 437 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Software/Arduino/StepperMotorDriver/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
10 changes: 10 additions & 0 deletions Software/Arduino/StepperMotorDriver/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}
39 changes: 39 additions & 0 deletions Software/Arduino/StepperMotorDriver/include/README
Original file line number Diff line number Diff line change
@@ -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 changes: 46 additions & 0 deletions Software/Arduino/StepperMotorDriver/lib/README
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions Software/Arduino/StepperMotorDriver/platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
; 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:uno]
platform = atmelavr
board = uno
framework = arduino
210 changes: 210 additions & 0 deletions Software/Arduino/StepperMotorDriver/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
#include <Arduino.h>
#include "motorControl.hpp"

// define your GPIO used here
#define B0 13
#define C0 14
#define D0 15
#define E0 19
#define B1 18
#define C1 17
#define D1 16
#define E1 5
#define B2 6
#define C2 7
#define D2 8
#define E2 9


// define GPIO set of stepper motor
int motorA[4] = {B0, C0, D0, E0};
int motorB[4] = {B1, C1, D1, E1};
int motorC[4] = {B2, C2, D2, E2};

// Motor phase storage
int phaseA = 0;
int phaseB = 0;
int phaseC = 0;

// Motor status storage
bool motorA_begin = false;
bool motorB_begin = false;
bool motorC_begin = false;

bool motorA_forward = false;
bool motorB_forward = false;
bool motorC_forward = false;

// Motor speed storage
int delay_time = 10;

// uart command receive
bool stringComplete = false;
String command = "";


void setup() {
// setting up the GPIO
motorGPIOSetup(motorA);
motorGPIOSetup(motorB);
motorGPIOSetup(motorC);

// setting up the uart
Serial.begin(9600);
}

void loop() {
//** UART COMMAND **//
// read command
if (Serial.available()) {
char singleChar;
singleChar = Serial.read();
command += singleChar;
if(singleChar == 10){
stringComplete = true;
}
}


// if command receive completed
if(stringComplete){
// devide command into head and param
String command_head = command.substring(0,2);
int command_param= command.substring(2, command.length()).toInt();
if(command_head == "SP"){
delay_time = command_param;
Serial.print("Speed Set:");
Serial.println(delay_time);
}

// MotorA
else if(command_head == "AF"){
Serial.println("Motor A StepForwad 1.");
phaseA = phaseForward(phaseA);
}
else if(command_head == "AB"){
Serial.println("Motor A StepBackward 1.");
phaseA = phaseBackward(phaseA);
}
else if(command_head == "AG"){
Serial.println("Motor A Go.");
if(command_param == 0){
motorA_forward = true;
}
else{
motorA_forward = false;
}
motorA_begin = true;
}
else if(command_head == "AS"){
Serial.println("Motor A Stop.");
motorA_begin = false;
}

else if(command_head == "AR"){
Serial.println("Motor A Released.");
motorA_begin = false;
switchPhase(motorA, 8);
phaseA = 8;
}

// MotorB
else if(command_head == "BF"){
Serial.println("Motor B StepForwad 1.");
phaseB = phaseForward(phaseB);
}
else if(command_head == "BB"){
Serial.println("Motor B StepBackward 1.");
phaseB = phaseBackward(phaseB);
}
else if(command_head == "BG"){
Serial.println("Motor B Go.");
if(command_param == 0){
motorB_forward = true;
}
else{
motorB_forward = false;
}
motorB_begin = true;
}
else if(command_head == "BS"){
Serial.println("Motor B Stop.");
motorB_begin = false;
}

else if(command_head == "BR"){
Serial.println("Motor B Released.");
motorB_begin = false;
phaseB = 8;
}

// MotorC
else if(command_head == "CF"){
Serial.println("Motor C StepForwad 1.");
phaseC = phaseForward(phaseC);
}
else if(command_head == "CB"){
Serial.println("Motor C StepBackward 1.");
phaseC = phaseBackward(phaseC);
}
else if(command_head == "CG"){
Serial.println("Motor C Go.");
if(command_param == 0){
motorC_forward = true;
}
else{
motorC_forward = false;
}
motorC_begin = true;
}
else if(command_head == "CS"){
Serial.println("Motor C Stop.");
motorC_begin = false;
}

else if(command_head == "CR"){
Serial.println("Motor C Released.");
motorC_begin = false;
phaseC = 8;
}


command = "";
stringComplete = false;
}

//** LOOP CONTROL **//
if(motorA_begin){
if(motorA_forward){
phaseA = phaseForward(phaseA);
}
else{
phaseA = phaseBackward(phaseA);
}
}

if(motorB_begin){
if(motorB_forward){
phaseB = phaseForward(phaseB);
}
else{
phaseB = phaseBackward(phaseB);
}
}

if(motorC_begin){
if(motorC_forward){
phaseC = phaseForward(phaseC);
}
else{
phaseC = phaseBackward(phaseC);
}
}


switchPhase(motorA, phaseA);
switchPhase(motorB, phaseB);
switchPhase(motorC, phaseC);

delay(delay_time);
}
Loading

0 comments on commit 2038845

Please sign in to comment.