-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArm.cpp
79 lines (72 loc) · 1.24 KB
/
Arm.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
#include "Arduino.h"
#include "Arm.h"
#include <Servo.h>
/*
* pin_ servo set the pin to control the servo
* pin_onoff set the pin to turn on or turn off the dc motor
* pin_dir set the pin to control the direction of the dc motor
*/
Arm::Arm(int pin_servo, int pin_onoff, int pin_dir)
{
//servo
_pin_servo = pin_servo;
//clamp
_pin_onoff = pin_onoff;
_pin_dir = pin_dir;
}
/*
* initial set up
*/
void Arm::initialize(int current)
{
//servo
_servo.attach(_pin_servo);
_current = current;
delay(5);
_servo.write(_current);
_target = current;
sem = true;
//clamp
}
/*
* set the target postion
* to activete the servo
* if the servo is already active
* this function do nothing
*/
void Arm::goTo(int target)
{
if (_current != target && sem)
{
_target = target;
sem = false;
}
}
/*
* return the current position of the servo
*/
int Arm::position()
{
return _servo.read();
}
/*
* call this in the main loop
* return true if servo is active
* return false if servo isn't active
*/
boolean Arm::run()
{
if (_current != _target)
{
if (_current > _target ) _current--;
else _current++;
_servo.write(_current);
delay(2);
return true;
}
else
{
sem = true;
return false;
}
}