-
Notifications
You must be signed in to change notification settings - Fork 180
/
motor.rs.disabled
111 lines (88 loc) · 2.39 KB
/
motor.rs.disabled
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
108
109
110
111
//! Open loop motor control
#![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]
extern crate cortex_m_rt as rt;
extern crate cortex_m_semihosting as sh;
extern crate motor_driver;
extern crate panic_semihosting;
#[macro_use(block)]
extern crate nb;
extern crate stm32f1xx_hal as hal;
use core::fmt::Write;
use hal::prelude::*;
use hal::serial::Serial;
use hal::stm32f103xx;
use motor_driver::Motor;
use rt::{entry, exception, ExceptionFrame};
use sh::hio;
#[entry]
fn main() -> ! {
let p = stm32f103xx::Peripherals::take().unwrap();
let mut flash = p.FLASH.constrain();
let mut rcc = p.RCC.constrain();
let clocks = rcc.cfgr.freeze(&mut flash.acr);
let mut afio = p.AFIO.constrain();
let mut gpioa = p.GPIOA.split();
let tx = gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh);
let rx = gpioa.pa10;
let serial = Serial::usart1(
p.USART1,
(tx, rx),
&mut afio.mapr,
115_200.bps(),
clocks,
);
let mut rx = serial.split().1;
let pwm = p.TIM2.pwm(
gpioa.pa0.into_alternate_push_pull(&mut gpioa.crl),
&mut afio.mapr,
1.kHz(),
clocks,
);
let max_duty = pwm.get_max_duty() as i16;
let mut motor = Motor::tb6612fng(
gpioa.pa1.into_push_pull_output(&mut gpioa.crl),
gpioa.pa2.into_push_pull_output(&mut gpioa.crl),
pwm,
);
let mut duty = max_duty;
let mut brake = true;
motor.duty(duty as u16);
let mut hstdout = hio::hstdout().unwrap();
writeln!(hstdout, "{} {}", max_duty, brake).unwrap();
loop {
match block!(rx.read()).unwrap() {
b'*' => duty *= 2,
b'+' => duty += 1,
b'-' => duty -= 1,
b'/' => duty /= 2,
b'r' => duty *= -1,
b's' => brake = !brake,
_ => continue,
}
if duty > max_duty {
duty = max_duty;
} else if duty < -max_duty {
duty = -max_duty;
}
if brake {
motor.brake();
} else if duty > 0 {
motor.cw();
} else {
motor.ccw();
}
motor.duty(duty.abs() as u16);
writeln!(hstdout, "{} {}", duty, brake).unwrap();
}
}
#[exception]
fn HardFault(ef: &ExceptionFrame) -> ! {
panic!("{:#?}", ef);
}
#[exception]
fn DefaultHandler(irqn: i16) {
panic!("Unhandled exception (IRQn = {})", irqn);
}