ShunjiHashimoto / micro_tang

クラシックのマイクロマウスを作る
0 stars 0 forks source link

IMUの動作確認 #8

Closed ShunjiHashimoto closed 4 months ago

ShunjiHashimoto commented 6 months ago

imuのconfigurationはのりさんのブログを参考とした、もう少し理解する必要がある。 https://rt-net.jp/mobility/archives/19984

ShunjiHashimoto commented 6 months ago

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "led.hpp"
#include "timers.hpp"
#include "motor.hpp"
#include "gyro.hpp"
#include "stdio.h"

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int __io_putchar(int ch) {
    HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 100);
    return ch;
}
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
    LedBlink ledBlink;
  Gyro gyro;
  Timers timers;
  Motor motor;

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART3_UART_Init();
  MX_ADC1_Init();
  MX_TIM1_Init();
  MX_TIM2_Init();
  MX_TIM4_Init();
  MX_TIM3_Init();
  /* USER CODE BEGIN 2 */
  // gyro.init()

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
    ledBlink.toggle();
    // printf("test");
    // motor.run(MotorR_TIM1_CH3_Pin, GPIO_PIN_SET, Motor_Mode_Pin, TIM_CHANNEL_4, 30);
    // TODO:IMU、エンコーダのチェック→制御( 一区画前進(台形加減速設定、目標速度の設定をタイマー割り込みでやる)→超信地旋回、、、)

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 168;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
ShunjiHashimoto commented 6 months ago

imuの設定はできた

/*
 * gyro.cpp
 *
 *  Created on: 2024/04/27
 *      Author: hashimoto
 */
#include "../../Core/Inc/gyro.hpp"

uint8_t Gyro::readByte(uint8_t reg)
{
  uint8_t rx_data[2];
  uint8_t tx_data[2];

  tx_data[0] = reg | 0x80;
  tx_data[1] = 0x00;  // dummy

  HAL_GPIO_WritePin(SPI2_CS_GPIO_Port, SPI2_CS_Pin, GPIO_PIN_RESET);
  HAL_SPI_TransmitReceive(&hspi2, tx_data, rx_data, 2, 1);
  HAL_GPIO_WritePin(SPI2_CS_GPIO_Port, SPI2_CS_Pin, GPIO_PIN_SET);

  return rx_data[1];
}

uint8_t Gyro::writeByte(uint8_t reg, uint8_t data)
{
  uint8_t rx_data[2];
  uint8_t tx_data[2];

  tx_data[0] = reg & 0x7F;
  tx_data[1] = data;  // write data

  HAL_GPIO_WritePin(SPI2_CS_GPIO_Port, SPI2_CS_Pin, GPIO_PIN_RESET);
  HAL_SPI_TransmitReceive(&hspi2, tx_data, rx_data, 2, 1);
  HAL_GPIO_WritePin(SPI2_CS_GPIO_Port, SPI2_CS_Pin, GPIO_PIN_SET);
}

void Gyro::init() {
    uint8_t who_am_i;

    HAL_Delay( 100 ); // wait start up
    who_am_i = readByte( WHO_AM_I ); // 1. read who am i 
    printf( "rn0x%xrn", who_am_i ); // 2. check who am i value, 0xE0が帰ってくる
    // 初回に失敗するときがあるので、もう一度動かしてみる
    HAL_Delay( 10 );
    who_am_i = readByte( WHO_AM_I );  
    printf( "rn0x%xrn", who_am_i ); 

    // 2. error check
    if ( who_am_i != 0x70 ){
        while(1){
        printf( "gyro_errorr");
        }
    }

    HAL_Delay( 50 ); // wait
    // sleep 状態から起こす、clearだから0
    // デフォルトでは省電力モード、今回は20Mhzで動かす、0で設定する
    // device resetも1で設定する、右側がかいビット、左が上位ビット
    writeByte(PWR_MGMT_1, 0x80); // 1000 0000 
    HAL_Delay(50);
    writeByte(PWR_MGMT_1, 0x01); // 0000 0001
    HAL_Delay(50);
    // GYRO_FS_SEL: 設定する必要あり、どこの速度まで角度取るかの設定、11(フルスケール)、旋回速度が遅いものは下げる必要
    writeByte(GYRO_CONFIG_1, 0x06); // 0000 0110
    HAL_Delay( 50 );
}

void Gyro::update() {
    HAL_Delay(100);
    // 0x37のアドレス、(指定しないまま送ると次のレジスタが送られる)
    // 0x37 0x00 0x00
    // Hamstarのコードも同じimuを使っているから参考に出来るはず
}
ShunjiHashimoto commented 6 months ago

https://product.tdk.com/system/files/dam/doc/product/sensor/mortion-inertial/imu/data_sheet/ds-000179-icm-20648-v1.2-typ.pdf データシート

ShunjiHashimoto commented 6 months ago

動作確認しようとして、printfの箇所で詰まった。 https://yukblog.net/stm32cubeide-printf-uart/#:~:text=%C2%A0-,%EF%BC%94%EF%BC%8ESTM32CubeIDE%E3%81%AB%E3%82%BF%E3%83%BC%E3%83%9F%E3%83%8A%E3%83%AB%E3%82%92%E5%85%A5%E3%82%8C%E3%82%8B,-STM32CubeIDE%E3%81%AB%E3%82%BF%E3%83%BC%E3%83%9F%E3%83%8A%E3%83%AB linuxだと上記のサイトのtm terminalという箇所でインストール進めればよい、rttxはスキップしても良い

ShunjiHashimoto commented 6 months ago

また、terminalでデータを表示するにはuart通信使って出す必要あり、そのため別途ケーブルも必要 次回はそこから、ケーブル用意して、printfでimuからの情報を表示する

ShunjiHashimoto commented 4 months ago

image imuの動作確認できた! 以降ハマったところや知見を残す

Who am iの送信結果が正確に返ってこなかった

image 原因はPrescalerの設定を16bitに当初設定していたが、16bit一気にデータ転送するとだめで、8bitずつデータを送信する必要がある。したがって、上記画像のように設定する

printfができなかった

extern "C" int __io_putchar(int ch) {
    HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, 100);
    return ch;
}

当初は上記のコードでextern Cをなしに書いていたため、エラーが出た、C言語として認識させてからだとうまくprintfできた

UART通信を使ってPCのターミナルにログを表示する

window→Show View → Others → terminalを選択する baurade設定などはデフォルトでよし、マウスのuart信号とPCをシリアル変換基板使ってつなぐ。
このとき、TXはRXにつなぐようにする。
また、 sudo chmod 666 /dev/ttyUSB を使って権限変更もする必要がある

gyro::update

void Gyro::update() {
    HAL_Delay(100);
    uint8_t zout_h, zout_l;
    zout_h = readByte(0x37);
    zout_l = readByte(0x38);
    int16_t gyro_raw = (((uint16_t)zout_h<<8)&0xff00)|zout_l;
    float gyro_ang_vel = (float)gyro_raw / 16.4;
    printf(" gyro raw %f\n", gyro_ang_vel);

zout_hとzout_lはzの値のHighビット、Lowビット、これらを結合させるため、hを8bit文ずらして、ずらしたあとの下ビットはなくし、代わりにLowビットの値をorで結合する仕組み また、 image

データの粗さは上記のGYRO_FS_SEL=3で設定されているため、1bitあたり16.4として得られた値を割る

floatが使えなかった

Screenshot from 2024-06-01 16-13-02 この画面の通り、floatを有効にする 以上

ShunjiHashimoto commented 4 months ago

今後は割り込みでimuの値を取得する必要がある、周期は1msで、割り込みでupdateを実行するときはprintfとDelayは削除する必要がある。

ShunjiHashimoto commented 4 months ago

image uartのピン

ShunjiHashimoto commented 4 months ago

参考文献 https://rt-net.jp/mobility/archives/7831