-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMotor.cpp
99 lines (71 loc) · 2.2 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Motor.cpp
#include "Motor.h"
#include <Arduino.h>
const int RPWM_PIN = 33;
const int LPWM_PIN = 32;
bool end_left = false;
bool end_right = false;
//overcurrent
float Motor::current = 0;
int Motor::max_current = 2000; //should be 500
bool Motor::running = false;
unsigned long Motor::state_millis = 0;
void Motor::setup() {
pinMode(RPWM_PIN, OUTPUT);
pinMode(LPWM_PIN, OUTPUT);
analogWrite(RPWM_PIN, 0);
analogWrite(LPWM_PIN, 0);
Serial.println("motor setup");
}
void Motor::loop() {
// Your motor loop logic here
// For example, you can call motor_set and other motor-related functions here
}
void Motor::set(int motor_speed, int motor_direction) {
Serial.print("motor_set ");
Serial.println(motor_speed);
if (motor_speed == 0) {
stop();
}
//check for the maximum motor speed
if(motor_speed > 255)motor_speed = 255;
//analog write has a limit of 255.
//Also, DC motors only have a good speed at 50% PWM = 122. May be lower for some motors.
//First check if we are over the current limmit
//current = current*0.99+analogRead(CS)*0.01;
if(motor_direction == 1){
if(end_left || current > max_current){
Serial.print(current);
Serial.println(" current too high end_left reached");
end_left = true;
Motor::stop();
return;
}
if(end_right && motor_speed>0)end_right = false; //if we were at the right end, and now turning left again, than end_right is false;
analogWrite(LPWM_PIN, 0);
analogWrite(RPWM_PIN, motor_speed);
}else{ // motor direct = 0
if(end_right || current > max_current){
Serial.println("current too high end_right reached");
end_right = true;
Motor::stop();
return;
}
if(end_left && motor_speed>0)end_left = false;
analogWrite(RPWM_PIN, 0);
analogWrite(LPWM_PIN, motor_speed);
}
if(!Motor::running){
Motor::running = true;
Motor::state_millis = millis();
}
}
void Motor::stop() {
analogWrite(LPWM_PIN, 0);
analogWrite(RPWM_PIN, 0);
if (Motor::running) {
Motor::running = false;
Motor::state_millis = millis();
}
}
// Define other functions like motor_loop, motor_set, and other helper functions as needed