-
Notifications
You must be signed in to change notification settings - Fork 0
/
motor.cpp
72 lines (65 loc) · 1.94 KB
/
motor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "motor.h"
#include <bcm2835.h>
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <chrono>
#define DEBUG TRUE
void Motor::init()
{
// Set the pin to be an output
bcm2835_gpio_fsel(STEP_M1, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(DIR_M1, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(STEP_M2, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(DIR_M2, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(ENABLE, BCM2835_GPIO_FSEL_OUTP);
//enable motor
bcm2835_gpio_write(ENABLE, LOW);
}
void Motor1::run() //steps is only return variable for the main loop
{
bcm2835_gpio_write(ENABLE, LOW);
valid=true;
while (valid)
{
// set the dir pin to the right value
bcm2835_gpio_write_multi(DIR_PIN_M1, dir);
// set the step pin to high
bcm2835_gpio_set_multi(STEP_PIN_M1);
bcm2835_delayMicroseconds(delay);
// clear the step pin to LOW
bcm2835_gpio_clr_multi(STEP_PIN_M1);
bcm2835_delayMicroseconds(delay);
dir ? steps++ : steps--; // if dir is true, steps++, else steps--
}
printf("motor 1 stopped\n");
}
void Motor2::run() //steps is only return variable for the main loop
{
bcm2835_gpio_write(ENABLE, LOW);
valid=true;
while (valid)
{
// set the dir pin to the right value
bcm2835_gpio_write_multi(DIR_PIN_M2, dir);
// set the step pin to high
bcm2835_gpio_set_multi(STEP_PIN_M2);
bcm2835_delayMicroseconds(delay);
// clear the step pin to LOW
bcm2835_gpio_clr_multi(STEP_PIN_M2);
bcm2835_delayMicroseconds(delay);
dir ? steps++ : steps--; // if dir is true, steps++, else steps--
}
printf("motor 2 stopped\n");
}
Motor::Motor():valid(true)
{
}
Motor::~Motor()
{
valid=false;
//disable motor
bcm2835_gpio_write(ENABLE, HIGH);
// print "the motor is disabled"
std::cout << "the motor is disabled" << std::endl;
}