-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
137 lines (108 loc) · 3.39 KB
/
main.rs
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use embassy_executor::Spawner;
use embassy_time::{Duration, Timer};
use esp_backtrace as _;
use esp_hal::{
gpio::{Input, Level, Output, Pull},
mcpwm::{
operator::{PwmPin, PwmPinConfig},
timer::PwmWorkingMode,
McPwm, PeripheralClockConfig,
},
peripherals::MCPWM0,
prelude::*,
timer::timg::TimerGroup,
};
use filament_changer::FilamentChanger;
mod filament_changer;
extern crate alloc;
#[embassy_executor::task]
async fn filament_changer_task(mut filament_changer: FilamentChanger<'static>) {
filament_changer.run().await;
}
/*
Servo Motor Limits:
300 is min
2500 is max
0deg is 500
90deg is 1500
180deg is 2500
*/
#[esp_hal_embassy::main]
async fn main(spawner: Spawner) -> ! {
esp_println::logger::init_logger_from_env();
esp_println::println!("Init!");
let peripherals = esp_hal::init(esp_hal::Config::default());
esp_alloc::heap_allocator!(72 * 1024);
// initialize embasy
let timg1 = TimerGroup::new(peripherals.TIMG1);
esp_hal_embassy::init(timg1.timer0);
// initialize filament changer
// Pin configuration
let servo_pin = Output::new(peripherals.GPIO23, Level::Low);
let stepper_a_dir = Output::new(peripherals.GPIO15, Level::Low);
let stepper_a_step = Output::new(peripherals.GPIO4, Level::Low);
let stepper_a_en = Output::new(peripherals.GPIO16, Level::High);
let stepper_b_dir = Output::new(peripherals.GPIO17, Level::Low);
let stepper_b_step = Output::new(peripherals.GPIO5, Level::Low);
let stepper_b_en = Output::new(peripherals.GPIO18, Level::High);
let endswitch = Input::new(peripherals.GPIO19, Pull::Down);
let led = Output::new(peripherals.GPIO2, Level::Low);
// MCPWM setup ( for Servo )
let clock_cfg = PeripheralClockConfig::with_frequency(32.MHz()).unwrap();
let mut mcpwm = McPwm::new(peripherals.MCPWM0, clock_cfg);
// connect operator0 to timer0
mcpwm.operator0.set_timer(&mcpwm.timer0);
// connect operator0 to pin
let mut pwm_pin: PwmPin<'_, MCPWM0, 0, true> = mcpwm
.operator0
.with_pin_a(servo_pin, PwmPinConfig::UP_ACTIVE_HIGH);
// start timer with timestamp values in the range of 0..=99 and a frequency of
// 20 kHz
let timer_clock_cfg = clock_cfg
.timer_clock_with_frequency(20000, PwmWorkingMode::Increase, 50.Hz())
.unwrap();
mcpwm.timer0.start(timer_clock_cfg);
pwm_pin.set_timestamp(1000);
let filament_changer = FilamentChanger::new(
stepper_a_dir,
stepper_a_step,
stepper_a_en,
stepper_b_dir,
stepper_b_step,
stepper_b_en,
endswitch,
led,
pwm_pin,
);
spawner
.spawn(filament_changer_task(filament_changer))
.unwrap();
loop {
Timer::after(Duration::from_millis(5_000)).await;
}
}
/*
Hardware specs:
17HS08-1004S Nema 17 Bipolar 1.8deg 13Ncm (18.4oz.in) 1A 3.5V 42x42x20mm 4 Wires
1.BLK(+) / 2.GRN(-)
3.RED(+) / 4.BLU(-)
TMC2208 V3.0 Stepper Motor Driver Bigtreetech
From TOP View:
1. GND 9. DIR
2. VIO 10. STEP
3. OB2 11. CLK
4. OB1 12. PDN_UART
5. OA1 13. PDN_UART
6. OA2 14. MS2
7. GND 15. MS1
8. VM 16. EN
MS1 | MS2 | Microstep Resolution
H | L | 2
L | H | 4
L | L | 8
H | H | 16
# ESP WROOM 32 Pinout (30 Pin Version)
*/