Skip to content

Commit

Permalink
debug表示をmainに移動で解決
Browse files Browse the repository at this point in the history
  • Loading branch information
chama1176 committed Sep 8, 2024
1 parent 4697873 commit 3d39cda
Showing 1 changed file with 84 additions and 77 deletions.
161 changes: 84 additions & 77 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ fn TIM3() {

// mainで初期化済み
let mut uart = bldc_motor_driver_stm32g4::Uart1::new();
let mut adcd: [u16; 7] = [0; 7];
let mut electrical_angle = 0.0;
let mut mechanical_angle = 0.0;
let mut diff_count = 0;
free(|cs| {
match bldc_motor_driver_stm32g4::G_PERIPHERAL
.borrow(cs)
Expand All @@ -110,10 +106,7 @@ fn TIM3() {
return;
},
Some(app) => {
adcd = free(|cs| bldc_motor_driver_stm32g4::G_ADC_DATA.borrow(cs).borrow().clone());
electrical_angle = app.last_electrical_angle;
mechanical_angle = app.last_mechanical_angle;
diff_count = app.diff_count;
let adcd = free(|cs| bldc_motor_driver_stm32g4::G_ADC_DATA.borrow(cs).borrow().clone());

let mut tv = (adcd[1] as f32 - 2000.0f32) / 1000.0f32;
if tv > 1.0 {
Expand All @@ -135,74 +128,6 @@ fn TIM3() {
});


write!(
uart,
"{{\"ADC\":[{:4}, {:4}, {:4}, {:4}, {:4}, {:4}, {:4}]}}\r\n",
adcd[0],
adcd[1],
adcd[2],
adcd[3],
adcd[4],
adcd[5],
adcd[6]
)
.unwrap();

// I = V / R
// 60V/V, 0.003
let current = ThreePhaseCurrent::<f32> {
i_u: (((adcd[2] as f32) / adcd[6] as f32 * 1.5) - 1.5) / 60.0 / 0.003 - 0.0,
i_v: (((adcd[3] as f32) / adcd[6] as f32 * 1.5) - 1.5) / 60.0 / 0.003 - 0.0,
i_w: 0.0,
};
let dq = current.to_dq(electrical_angle);

defmt::info!("diff: {}", diff_count);

// floatのまま送るとFLASHをバカほど食うのでcastする
write!(
uart,
"{{\"ea\":{:4}}}\r\n",
(electrical_angle * 1000.0) as i32,
)
.unwrap();

// floatのまま送るとFLASHをバカほど食うのでcastする
write!(
uart,
"{{\"ma\":{:4}}}\r\n",
(mechanical_angle * 1000.0) as i32,
)
.unwrap();

// floatのまま送るとFLASHをバカほど食うのでcastする
write!(
uart,
"{{\"iu\":{:4}}}\r\n",
(current.i_u * 1000.0) as i32,
)
.unwrap();
// floatのまま送るとFLASHをバカほど食うのでcastする
write!(
uart,
"{{\"iv\":{:4}}}\r\n",
(current.i_v * 1000.0) as i32,
)
.unwrap();

// floatのまま送るとFLASHをバカほど食うのでcastする
write!(
uart,
"{{\"d\":{:4}}}\r\n",
(dq.i_d * 1000.0) as i32,
)
.unwrap();
write!(
uart,
"{{\"q\":{:4}}}\r\n",
(dq.i_q * 1000.0) as i32,
)
.unwrap();

}

Expand Down Expand Up @@ -301,23 +226,105 @@ fn main() -> ! {
});
if (t + 10000 - prev) % 10000 >= 1000 {
cnt += 1;
if cnt > 500 {
if cnt > 50 {
// defmt::info!("hello from defmt");

cnt = 0;
let mut adcd: [u16; 7] = [0; 7];
let mut electrical_angle = 0.0;
let mut mechanical_angle = 0.0;
let mut diff_count = 0;

let mut rad = 0.;
let mut calib_count = 7;
free(|cs| match G_APP.borrow(cs).borrow_mut().deref_mut() {
None => (),
Some(app) => {
rad = app.read_encoder_data();
calib_count = app.calib_count();
adcd = free(|cs| bldc_motor_driver_stm32g4::G_ADC_DATA.borrow(cs).borrow().clone());
electrical_angle = app.last_electrical_angle;
mechanical_angle = app.last_mechanical_angle;
diff_count = app.diff_count;

}
});
let deg = rad.rad2deg();
defmt::info!("deg: {}, rad: {}", deg, rad);
// write!(uart, "{}, {:4}, {:4}", calib_count, deg, rad).unwrap();
// write!(uart, "\"tv\": {:4}\r\n", tv,).unwrap();

write!(
uart,
"{{\"ADC\":[{:4}, {:4}, {:4}, {:4}, {:4}, {:4}, {:4}]}}\r\n",
adcd[0],
adcd[1],
adcd[2],
adcd[3],
adcd[4],
adcd[5],
adcd[6]
)
.unwrap();

// I = V / R
// 60V/V, 0.003
let current = ThreePhaseCurrent::<f32> {
i_u: (((adcd[2] as f32) / adcd[6] as f32 * 1.5) - 1.5) / 60.0 / 0.003 - 0.0,
i_v: (((adcd[3] as f32) / adcd[6] as f32 * 1.5) - 1.5) / 60.0 / 0.003 - 0.0,
i_w: 0.0,
};
let dq = current.to_dq(electrical_angle);

defmt::info!("diff: {}", diff_count);

// floatのまま送るとFLASHをバカほど食うのでcastする
write!(
uart,
"{{\"ea\":{:4}}}\r\n",
(electrical_angle * 1000.0) as i32,
)
.unwrap();

// floatのまま送るとFLASHをバカほど食うのでcastする
write!(
uart,
"{{\"ma\":{:4}}}\r\n",
(mechanical_angle * 1000.0) as i32,
)
.unwrap();

// floatのまま送るとFLASHをバカほど食うのでcastする
write!(
uart,
"{{\"iu\":{:4}}}\r\n",
(current.i_u * 1000.0) as i32,
)
.unwrap();
// floatのまま送るとFLASHをバカほど食うのでcastする
write!(
uart,
"{{\"iv\":{:4}}}\r\n",
(current.i_v * 1000.0) as i32,
)
.unwrap();

// floatのまま送るとFLASHをバカほど食うのでcastする
write!(
uart,
"{{\"d\":{:4}}}\r\n",
(dq.i_d * 1000.0) as i32,
)
.unwrap();
write!(
uart,
"{{\"q\":{:4}}}\r\n",
(dq.i_q * 1000.0) as i32,
)
.unwrap();



}
prev = t;
}
Expand Down

0 comments on commit 3d39cda

Please sign in to comment.