-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobot.c
More file actions
86 lines (76 loc) · 1.64 KB
/
Copy pathRobot.c
File metadata and controls
86 lines (76 loc) · 1.64 KB
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
#include <stdio.h>
#include <bcm2835.h>
#include <time.h>
#include <assert.h>
#include "MotorHat.h"
#include "Robot.h"
static int _left;
static int _right;
static int _left_trim;
static int _right_trim;
extern void initRobot(unsigned int addr, int stop_at_exit){
initHat(0x60, 1600);
_left = 0;
_right = 1;
_left_trim = 0;
_right_trim = 0;
run(RELEASE, _left);
run(RELEASE, _right);
if (stop_at_exit){
stop();
}
}
extern void stop(){
run(RELEASE, _left);
run(RELEASE, _right);
}
void _left_speed(int speed){
assert(0 <= speed <= 255);
speed += _left_trim;
setSpeed(speed, 0);
}
void _right_speed(int speed){
assert(0 <= speed <= 255);
speed += _right_trim;
setSpeed(speed, 1);
}
extern void forward(int speed, int seconds){
_left_speed(speed);
_right_speed(speed);
run(FORWARD, _left);
run(FORWARD, _right);
if (seconds != 0){
bcm2835_delay(seconds*1000);
stop();
}
}
extern void backward(int speed, int seconds){
_left_speed(speed);
_right_speed(speed);
run(BACKWARD, _left);
run(BACKWARD, _right);
if (seconds != 0){
bcm2835_delay(seconds*1000);
stop();
}
}
extern void right(int speed, int seconds){
_left_speed(speed);
_right_speed(speed);
run(FORWARD, _right);
run(BACKWARD, _left);
if (seconds != 0){
bcm2835_delay(seconds*1000);
stop();
}
}
extern void left(int speed, int seconds){
_left_speed(speed);
_right_speed(speed);
run(FORWARD, _left);
run(BACKWARD, _right);
if (seconds != 0){
bcm2835_delay(seconds*1000);
stop();
}
}