-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmotor.c
69 lines (51 loc) · 1.21 KB
/
motor.c
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
/*
* motor.c
*
* Created on: Feb 24, 2012
* Author: aah9038
*/
#include "motor.h"
/**
* Set the motor speed
*
* Accepts a signed double from -1 to 1 that determines speed of the motor
*/
void motor_setSpeed(motor_t* motor, double speed){
if(speed <0.0){
motor_setMode(motor, backward);
}
else{
motor_setMode(motor, forward);
}
//Dead zone insertion
if(fabs(speed)>0.01){
pwm_setDuty(&motor->pwm, fabs(speed));
}else{
pwm_setDuty(&motor->pwm, 0);
}
}
/**
* Set the motor mode
*
* mode=freewheel,forward,backward,brake
*/
void motor_setMode(motor_t* motor, motor_mode mode){
if (motor->current_mode!=mode){
motor->current_mode=mode;
//set the appropraite bits of the motor control.
setPin(motor->cnt_port,motor->input_1_pin,(mode & 0b01));
setPin(motor->cnt_port,motor->input_2_pin,(mode & 0b10));
}
}
/**
* Initializes a motor object
*
* mode=freewheel,forward,backward,brake
*/
void init_motor(motor_t* motor, uintptr_t port, char input_1_pin, char input_2_pin, char pwmPin){
motor->cnt_port=port;
motor->input_1_pin=input_1_pin;
motor->input_2_pin=input_2_pin;
motor_setMode(motor,freewheel);
startPWM(&motor->pwm,port,0x1<<pwmPin,0,255);//pwm out on pin A0
}