-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmotor.c
56 lines (46 loc) · 1.14 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
/*
* Author: Michael Ortiz
* Email: [email protected]
*
* Desc: Functions for driving the Traxxas E-MAXX RC truck.
*/
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "motor.h"
void left_motor(int percent) {
int speed;
if(percent<-100)
percent=-100;
if(percent>100)
percent=100;
if(percent > -10 && percent < 10)
percent = 0;
// -100% => PWM_MAX_REVERSE_SPEED
// 100% => PWM_MAX_FORWARD_SPEED
// 0% => PWM_IDLE_STOP
if(percent)
speed = PWM_IDLE_STOP + ((PWM_MAX_FORWARD_SPEED-PWM_IDLE_STOP)*(percent/100.0));
else
speed = 0;
// Set the left PWM to have an active time anywhere from 1-2ms.
// A percentage of 0 should equate to an idle time of 1.5ms.
//fprintf(stderr, "Setting left motor: %04x\n", speed);
pwm_left(speed);
}
void right_motor(int percent) {
int speed;
percent *= -1;
if(percent<-100)
percent=-100;
if(percent>100)
percent=100;
if(percent > -10 && percent < 10)
percent = 0;
if(percent)
speed = PWM_IDLE_STOP + ((PWM_MAX_FORWARD_SPEED-PWM_IDLE_STOP)*(percent/100.0));
else
speed = 0;
//fprintf(stderr, "Setting right motor: %04x\n", speed);
pwm_right(speed);
}