main.c
2.54 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include "cpu.h"
#include "board.h"
#include "xtimer.h"
#include "periph/pwm.h"
#include "periph/gpio.h"
#include "robotcar.h"
#define PWM PWM_DEV(0)
#define MOTOR1_CHANNEL 0
#define MOTOR1_IN1 GPIO_PIN(PORT_E,0)
#define MOTOR1_IN2 GPIO_PIN(PORT_E,2)
#define MOTOR1_ECD GPIO_PIN(PORT_A,8)
#define MOTOR2_CHANNEL 1
#define MOTOR2_IN1 GPIO_PIN(PORT_E,1)
#define MOTOR2_IN2 GPIO_PIN(PORT_E,3)
#define MOTOR2_ECD GPIO_PIN(PORT_A,9)
#define STDBY GPIO_PIN(PORT_D,4)
static robot_t motor_1;
static robot_t motor_2;
static void motor1_cb(void *arg)
{
robot_step_counter(&motor_1);
}
static void motor2_cb(void *arg)
{
robot_step_counter(&motor_2);
}
int main(void)
{
int res;
puts("\nRIOT dc motor test");
puts("Connect an dc motor or scope to PWM_0 channel 0/1 to see anything");
res = robot_init(&motor_1,PWM,MOTOR1_CHANNEL,MOTOR1_IN1,MOTOR1_IN1,STDBY,MOTOR1_ECD);
if (res < 0) {
puts("Errors while initializing motor");
return -1;
}
res = robot_init(&motor_2,PWM,MOTOR2_CHANNEL,MOTOR2_IN2,MOTOR2_IN2,STDBY,MOTOR2_ECD);
if (res < 0) {
puts("Errors while initializing motor");
return -1;
}
puts("all motors initialized.");
if (gpio_init_int(MOTOR1_ECD,GPIO_IN, GPIO_RISING, motor1_cb,
(void *)MOTOR1_ECD) < 0) {
puts("Error while initializing MOTOR1_ECD as external interrupt\n");
return 1;
}
if (gpio_init_int(MOTOR2_ECD,GPIO_IN, GPIO_RISING, motor2_cb,
(void *)MOTOR2_ECD) < 0) {
puts("Error while initializing MOTOR2_ECD as external interrupt\n");
return 1;
}
printf("motor interrupt initialized successfuly\n");
while (1) {
gpio_clear(LED3_PIN);
gpio_clear(LED2_PIN);
gpio_clear(LED1_PIN);
gpio_clear(LED0_PIN);
robot_move_forward(&motor_1,&motor_2,400,8);
gpio_clear(LED3_PIN);
gpio_clear(LED2_PIN);
gpio_clear(LED1_PIN);
gpio_clear(LED0_PIN);
xtimer_sleep(2);
robot_move_reverse(&motor_1,&motor_2,400,11);
gpio_clear(LED3_PIN);
gpio_clear(LED2_PIN);
gpio_clear(LED1_PIN);
gpio_clear(LED0_PIN);
xtimer_sleep(2);
robot_spin_right(&motor_1,&motor_2,400,6);
gpio_clear(LED3_PIN);
gpio_clear(LED2_PIN);
gpio_clear(LED1_PIN);
gpio_clear(LED0_PIN);
xtimer_sleep(2);
robot_spin_left(&motor_1,&motor_2,400,5);
gpio_clear(LED3_PIN);
gpio_clear(LED2_PIN);
gpio_clear(LED1_PIN);
gpio_clear(LED0_PIN);
xtimer_sleep(2);
}
return 0;
}