-
Notifications
You must be signed in to change notification settings - Fork 0
/
LSA08_USART.ino
105 lines (91 loc) · 2.34 KB
/
LSA08_USART.ino
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
#include <avr/io.h>
#include <avr/interrupt.h>
int x, y;
int setpoint = 35;
float motorSpeed = 0;
float Kp = 0.2, Kd = 0;
float pwm1 = 50, pwm2 = 50, pwm3 = 50;
#define m1pwm 10
#define m1dir 9
#define m2pwm 12
#define m2dir 11
#define m3pwm 7
#define m3dir 8
void USART_Init3(unsigned int baud)
{
// set baud rate
UBRR3H = (unsigned char)(baud >> 8);
UBRR3L = (unsigned char) baud;
// enable reciever and transmitter
UCSR3B = (1 << TXEN3) | (1 << RXEN3);
// set frame format: 8data, 2 stop bit
UCSR3C = (1 << USBS3) | (3 << UCSZ30);
}
void USART_Init2(unsigned int baud)
{
// set baud rate
UBRR2H = (unsigned char)(baud >> 8);
UBRR2L = (unsigned char) baud;
// enable reciever and transmitter
UCSR2B = (1 << TXEN2) | (1 << RXEN2);
// set frame format: 8data, 2 stop bit
UCSR2C = (1 << USBS2) | (3 << UCSZ20);
}
unsigned char USART_RECIEVE2(void)
{
//wait for data to be recieved
while (!(UCSR2A & (1 << RXC2)))
;
//get and recieve data from buffer
return UDR2;
}
unsigned char USART_RECIEVE3(void)
{
//wait for data to be recieved
while (!(UCSR3A & (1 << RXC3)));
//get and recieve data from buffer
return UDR3;
}
void setup() {
Serial.begin(9600);
//Serial3.begin(9600);
//Serial2.begin(9600);
pinMode(m1pwm, OUTPUT);
pinMode(m2pwm, OUTPUT);
pinMode(m3pwm, OUTPUT);
pinMode(m1dir, OUTPUT);
pinMode(m2dir, OUTPUT);
pinMode(m3dir, OUTPUT);
digitalWrite(m1dir, LOW);
digitalWrite(m2dir, LOW);
digitalWrite(m3dir, LOW);
analogWrite(m1pwm, 0);
analogWrite(m2pwm, 0);
analogWrite(m3pwm, 0);
}
void loop() {
x = USART_RECIEVE2();
int error = x - setpoint; // Calculate the deviation from position to the set point
motorSpeed = Kp * error;
pwm3 = pwm3 - motorSpeed;
pwm2 = pwm2 + motorSpeed;
Serial.print(" x= ");
Serial.print(x);
Serial.print(" pwm2= ");
Serial.print(pwm2);
Serial.print(" pwm3= ");
Serial.print(pwm3);
pwm2=constrain(pwm2, 10, 100);
pwm3=constrain(pwm3, 10, 100);
digitalWrite(m2dir, LOW);
digitalWrite(m3dir, HIGH);
analogWrite(m2pwm,pwm2 );
analogWrite(m3pwm,pwm3);
Serial.print(" x2= ");
Serial.print(x);
Serial.print(" pwm22= ");
Serial.print(pwm2);
Serial.print(" pwm32= ");
Serial.print(pwm3);
Serial.println();
}