2024年4月15日 星期一

ADXL345谐振测量 - 各種派

此文件是依購買套件所提供的文件整理的,方便自己參考。

这是什么?

一种可用于减少打印中震纹(也称为重影或波纹)的技术。震纹是一种表面打印缺陷。


震纹是由打印机中的机械振动引起的,原因是打印方向的快速变化。请注意,震纹通常有机械原因:打印机框架刚性不足、皮带不紧或弹性太大、机械部件对齐问题、移动质量大等。如果可能,应首先检查和修复这些问题。

谐振补偿是一种开环控制技术,它创建一个命令信号来取消其自身的振动。谐振补偿需要一些调整和测量才能启用。除了震纹之外,谐振补偿通常会减少打印机的振动和晃动,并且还可以提高 Trinamic 步进驱动器的 StealthChop 模式的可靠性。

以下将提供 ADXL345 模块进行谐振测量的教程

第一步,材料准备

ADXL345 加速度传感器一块

必须采购如上图所示的模块,其他模块可能无法识别。

第二步,安装及接线

这是官方的接线图,不过研究下来其实 3.3V 和 GND 可以直接图中箭头指的这两个,这样就可以直接压一个 4×2 的插头一次性解决了。板子上應有 5V-3.3V 的降壓 IC,可以用 5V 的電源。

PIN 對應,MISO - DO - SDO,MOSI - DI - SDA,SCK - SCK - SCL。

安装位置可以参考这个


安装有几个要注意的点:

1)具体安装位置其实没有太大讲究,毕竟这个模块作用只是测试,测试好后可以拆掉,但是一定要尽量靠近喷嘴。

2)ADXL345板子上有XYZ的方向指向,但是按照资料好像说方向不一定非要和打印机的方向一致,但是每个轴必须平行,譬如 ADXL345 板子上的 Z 轴可以对着打印机的 X 轴,但是 Z 轴和 X 轴平行。(轴方向不需要一致这一点其实不是非常确认,如果有不同意见的可以反馈)

第三步,配置树莓派

硬件接好之后就可以试着软件的安装了,首先因为Klipper需要通过树莓派的SPI针脚读取加速度传感器的数据,所以需要先将树莓派变成klipper的一块mcu板子,这样klipper才能读取到加速度传感器的数据。这一步在klipper官方的教程中是没有的,所以很多人这一步会忽略。

下面的命令都是在树莓派系统中执行,使用 putty 软件登录树莓派按步骤执行以下命令

首先安装 Python 的科学计算库,用作分析计算共振数据(实际执行的pip路径需要根据你klipper的venv环境的实际安装的路径来定,绝大部分情况是home目录下的klippy-env中)

~/klippy-env/bin/pip install -v numpy

安装系统依赖库)

sudo apt install python-numpy python-matplotlib

将树莓派变成klipper的一块mcu,复制官方提供的系统启动脚本,并设置成系统自动启动

cd ~/klipper/
sudo cp "./scripts/klipper-mcu-start.sh" /etc/init.d/klipper_mcu
sudo update-rc.d klipper_mcu defaults

配置并编译树莓派的固件

cd ~/klipper/
make menuconfig

配置的控制器类型的时候,切记得要选择Linux process的



配置完成后按Q键保存退出,就可以编译固件了

按顺序运行以下命令

sudo service klipper stop
make flash
sudo service klipper start
sudo usermod -a -G tty pi

树莓派必須再執行 raspi-config 打开的SPI的接口。

第四步,配置Klipper

在网页操作界面中打开Klipper的配置文件printe.cfg,在合适的地方加入下面的配置代码

[mcu rpi]
serial: /tmp/klipper_host_mcu

[adxl345]
cs_pin: rpi:None

[resonance_tester]
accel_chip: adxl345

probe_points:
    150,150,20  # 这个地方建议配置成热床的中间(此150参数配置为300热床)

配置完成后重启一下Klipper

在控制台中输入以下命令


ACCELEROMETER_QUERY 这个命令是查询加速器的参数,如果前面的软硬件安装正常的话,就会收到返回的加数据传感器参数,类似于下面内容……如果返回其他则可能是接线问题或adxl345模块问题

.....Recv: // adxl345 values (x, y, z): 470.719200, 941.438400, 9728.196800...

第五步,测试加速度

开始测试前,先把打印机的加速度配置调大(这个测试完之后可以还原回去)码

[printer]
max_accel: 7000
max_accel_to_decel: 7000

如果你之前有配置过input_shaper的功能,需要先关掉之前配置好的参数

SET_INPUT_SHAPER SHAPER_FREQ_X=0 SHAPER_FREQ_Y=0

然后就开始可以测试共振了,比较省事的就是直接用自动测试配置命令

SHAPER_CALIBRATE

测试的时候,刀头会从 0MHz~120MHz 震动,逐渐测试 x轴和 y轴数据,期间声音会逐渐增大,如果机子组装过程没有太大问题的话,机子不应该会有太剧烈反应(如果有太剧烈的反应建议检查组装过程)

完成测试后会进行一下计算,然后会显示出x轴和y轴推荐的配置方法和配置值,你可以根据这个配置方法和值手工配置 input_shaper 参数,类似这种

[input_shaper]
shaper_type_x = 3hump_ei
shaper_freq_x = 52.4
shaper_type_y = 2hump_ei
shaper_freq_y = 37.5

不过也能直接使用

SAVE_CONFIG

命令直接保存进配置文件

保存完毕后需要回到 printe.cfg 配置文件中,拉倒最底部可以找到被注释的内容,如下

#*# [input_shaper]
#*# shaper_type_x = mzv
#*# shaper_freq_x = 58.4
#*# shaper_type_y = mzv
#*# shaper_freq_y = 44.2

整个复制,替换原来的参数,并取消注释,最后保存并重启即可



2024年3月20日 星期三

製作一個簡單的鋰電池轉 1.5V 的轉換器

參考 YouTube 影片 How to Make Lifetime Battery for any Clock | 3.7v to 1.5v converter at home

擷了兩幅主要的圖如下,第一圖是實體圖,第二圖是示意圖。



把電路整理計算一下,各點電壓大致如下。


參考 時鐘7天換1顆電池,耗電算正常嗎?,一顆3號碳鋅電池的電量約 662.5mAh,假如我家的時鐘使用約 3個月就沒電,反推機芯的耗電量為 662.5/(24*90) = 0.307 (mA)。

假設電晶體的電流放大率為 10,則 B極的電流為 0.03mA。1.85/0.03 = 61.7 (kΩ)。將 10k電阻換成 47k 的電阻,可以更省電。電晶體承受的功率為 (3.7-1.25)*0.03 = 0.07mW,嗯,不用管電晶體的散熱吧。




2024年3月6日 星期三

Fly-3d pi 使用

3D 印表機要裝 Klipper,必須要多一個上位機,這個可以用各種不同相容的 pi 系統。先前買套件附的是 MKS PI,這次想找不同的更便宜的方案,試試看 Mellow-3D 出的 Fly pi。

先說一下使用後的想法,針對其包好的程序,說明詳細,提共各個板子的 Klipper 設定。但是,若想進一步了解板子的技術規格,幾乎找不到,甚至連板子外形和螺絲孔位的尺寸資料都沒有,對於想自己動手改的人來說,非常不便。

相對的來說,MKS 的文件算是比較有條理和完備,價格也不算太貴。另外,蜘蛛的板子價格較貴,但文件是一團混亂。

編譯 Armbian image 

參考

因為沒有 USB wifi 網卡的驅動,而這個網卡在 MKS pi 是可以用的。比較差異,發現 fly-3d 提供的 image 是版本比較舊的。因此,試試看自己編譯 armbian 的 image。

因為 armbian 中沒有 fly-3d 板子的設定,必須打上網友提供的 patch。

# 下載 patch 檔
$ git clone https://github.com/reemo3dp/mellowfly-geminipi-armbian

# Check out armbian's build repository
$ git clone https://github.com/armbian/build/ armbian
$ cd armbian/

# Apply patches from this repository
$ git am ../mellowfly-geminipi-armbian/patches/*.patch

# Compile ubuntu jammy with default kernel config
$ ./compile.sh BOARD=mellowflygeminipi BRANCH=current RELEASE=jammy BUILD_MINIMAL=no BUILD_DESKTOP=no KERNEL_CONFIGURE=no INSTALL_HEADERS=yes

接下來就可以依據官方的文件,使用 compile.sh 建立 image 了。

其中,linux-complete.git.tar 超過 2.5GB,要下載很久,可以到目錄 armbian/cache/git-bundles/kernel/linux-complete.git.tar.oras.pull.tmp,看看下載的進度。不過,在 docker 下編譯失敗後,改在磁碟空間比較小的 VM 上編譯,則只會下載較小的 share 版本。

選擇 standard,image 約 2.4GB,改選 minimal,image 約 1.4GB。但是使用 minimal 的 image,要編譯 wifi 的 module,還要補裝好多東西,試了之後,還是改回 standard 吧。

經過幾次的測試後,比較好的版本是 https://github.com/lwfinger/rtl8188gu,可以順利的編譯完成安裝。

下載的 image,只能用 Win32DiskImager 來寫入 SD 卡,用 balenaEtcher 則會在驗證時失敗。但是自己建的 image,就可以用 Win32DiskImager 來寫入 SD 卡了。

無線網卡的安裝

無線網卡,是比較麻煩的部分,要找到 Linux 有支援的才行,這算是一種冒險吧。

在淘寶可以找到在 Windows 下免裝驅動程式的 USB 網卡,但有很多在 Linux 下很難裝起來。先前有失敗的例子,買來在 MKS PI 無法驅動。這次就找 MKS PI 附的同型網卡,COMFAST 的 CF-WU816N,結果還是不行,但還好後來解決了。

試了幾個,https://github.com/lwfinger/rtl8188gu 是比較新的,不用修改東西就能成功編譯安裝。在編譯時,可能會出現 "No rule to make target 'arch/aarch64/Makefile'" 的錯誤,要加上 ARCH 的選項,即 "make ARCH=arm64"。

$ git clone https://github.com/lwfinger/rtl8188gu
$ cd rtl8188gu
$ make ARCH=arm64
$ sudo make install
$ sudo depmod -a
$ sudo modprobe 8188gu

usb 網卡默認為 u盤模式。要用 usb_modeswitch 切換其模式。執行一次後,就可以保持此結果。

$ lsusb
Bus 009 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 114: ID 0bda:1a2b Realtek Semiconductor Corp. RTL8188GU 802.11n WLAN Adapter (Driver CDROM Mode)

# usb_modeswitch -KW -v 0bda -p 1a2b
Take all parameters from the command line


 * usb_modeswitch: handle USB devices with multiple modes
 * Version 2.6.1 (C) Josua Dietze 2017
 * Based on libusb1/libusbx

 ! PLEASE REPORT NEW CONFIGURATIONS !

DefaultVendor=  0x0bda
DefaultProduct= 0x1a2b

StandardEject=1

Look for default devices ...
  found USB ID 1d6b:0001
  found USB ID 1d6b:0002
  found USB ID 1d6b:0001
  found USB ID 0bda:b711
   vendor ID matched
  found USB ID 1d6b:0002
  found USB ID 1d6b:0001
  found USB ID 1d6b:0002
  found USB ID 1d6b:0001
  found USB ID 1d6b:0002
  found USB ID 1d6b:0002
 No devices in default mode found. Nothing to do. Bye!

~# lsusb
Bus 009 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 054: ID 0bda:b711 Realtek Semiconductor Corp. RTL8188GU 802.11n WLAN Adapter (After Modeswitch)

安裝好了之後,還要重裝 firmware。

[  299.156391] usb 3-1: new high-speed USB device number 3 using ehci-platform
[  299.313341] usb 3-1: New USB device found, idVendor=0bda, idProduct=b711, bcdDevice= 2.00
[  299.313369] usb 3-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[  299.313381] usb 3-1: Product: 802.11n WLAN Adapter
[  299.313391] usb 3-1: Manufacturer: Realtek
[  299.313400] usb 3-1: SerialNumber: 00E04CB82101
[  299.361219] usb 3-1: RTL8710BU rev A (SMIC) romver 1, 1T1R, TX queues 3, WiFi=1, BT=0, GPS=0, HI PA=0
[  299.361255] usb 3-1: RTL8710BU MAC: e0:e1:a9:99:33:d9
[  299.361268] usb 3-1: rtl8xxxu: Loading firmware rtlwifi/rtl8710bufw_SMIC.bin
[  299.361359] usb 3-1: Direct firmware load for rtlwifi/rtl8710bufw_SMIC.bin failed with error -2
[  299.361377] usb 3-1: request_firmware(rtlwifi/rtl8710bufw_SMIC.bin) failed
[  299.361389] usb 3-1: Fatal - failed to load firmware
[  299.361433] rtl8xxxu: probe of 3-1:1.0 failed with error -11
[  299.361548] RTW: 
               usb_endpoint_descriptor(0):
[  299.361556] RTW: bLength=7

..........


$ sudo apt install --reinstall linux-firmware
Recommended packages:
  firmware-sof-signed
The following packages will be REMOVED:
  armbian-firmware
The following NEW packages will be installed:
  linux-firmware
0 upgraded, 1 newly installed, 1 to remove and 0 not upgraded.
Need to get 266 MB of archives.

.......
[ 2055.140613] usb 3-1: USB disconnect, device number 4
[ 2055.419081] usb 3-1: new high-speed USB device number 5 using ehci-platform
[ 2055.575985] usb 3-1: New USB device found, idVendor=0bda, idProduct=b711, bcdDevice= 2.00
[ 2055.576015] usb 3-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 2055.576027] usb 3-1: Product: 802.11n WLAN Adapter
[ 2055.576036] usb 3-1: Manufacturer: Realtek
[ 2055.576045] usb 3-1: SerialNumber: 00E04CB82101
[ 2055.625487] usb 3-1: RTL8710BU rev A (SMIC) romver 1, 1T1R, TX queues 3, WiFi=1, BT=0, GPS=0, HI PA=0
[ 2055.625522] usb 3-1: RTL8710BU MAC: e0:e1:a9:99:33:d9
[ 2055.625535] usb 3-1: rtl8xxxu: Loading firmware rtlwifi/rtl8710bufw_SMIC.bin
[ 2055.625880] usb 3-1: Firmware revision 16.0 (signature 0x10b1)
[ 2057.333949] rtl8xxxu 3-1:1.0 wlxe0e1a99933d9: renamed from wlan0

對了,使用 minimal 的 image,也無法重裝 firmware,這是放棄的原因之一,反正 SD 卡的空間也很大。

要設定網卡,可安裝 network-manager,其將安裝 nmtui,即可使用 nmtui 來設定網卡,設定資料在重新開機後仍能保存。

安裝 Klipper

接下來只要參考 https://github.com/dw-0/kiauh 的說明,就可以很容易的安裝 Klipper。


2024年2月19日 星期一

STM32F103 的 timer 的使用

目的

在改裝 3D 印表機時,需要在有限的空間內加上一些調整時會用的裝置,只要把它安裝在可動的座上。參考網路的作法,使用舵機 MG90S 來改變位置。MG90S 是使用 50Hz 的方波的 duty cycle 寬度來控制舵的角度。

為了瞭解 MG90S 的運作方式,使用 STM32F103 的開發板來控制它,程式語言則是使用 Forth。Forth 兼具 interactive 和 compile 的特性,可以在 terminal 直接發送指令,也可將一堆指令包成一個 word (等同於其他程式言的 function),然後在 terminal 直接測試新的 word。並且有能夠直接存取特定 IO 的低階指令,在開發硬體上很方便。

參考 

STM32 的 PWM 原理

STM32 有不同編號的 MCU,功能上差不多,但控制上稍微不同,必須找對應的 Reference manual 來看,如 STM32F101xx 要看 RM0008 的文件,STM32F407/417 則要看 RM0090 的文件。

一張圖勝於長篇解說,下面的圖借自網站 deepbluembedded 的圖。



STM32F103 微控制器采用 Cortex-M3 内核,CPU 最高速度达 72 MHz。MG90S 控制信號的頻率為 50Hz。

以下參考 embeddedexpert 的關於 timer 使用的說明。

1. stm32 的 timer 的工作簡要說明:

A Timer Module in its most basic form is a digital logic circuit that counts up every clock cycle. More functionalities are implemented in hardware to support the timer module so it can count up or down. It can have a Prescaler to divide the input clock frequency by a selectable value. It can also have circuitry for input capture, PWM signal generation, and much more as we’ll see in this tutorial.

Let’s consider a basic 16-Bit timer like the one shown below. As a 16-Bit time, it can count from 0 up to 65535. Every clock cycle, the value of the timer is incremented by 1. And as you can see, the Fsys is not the frequency that is incrementing the timer module. But it gets divided by the Prescaler, then it gets fed to the timer.

Basically, in timer mode, the TCNT register is incremented by 1 each clock cycle @ the following frequency (Fsys/PSC). This means if the Fsys is 80MHz & PSC is 1:1024, the TCNT gets incremented by 1 every 12.8μSec. Therefore, if you start this timer to count from 0 until it reaches overflow (at 65535), a flag will be set and starts over from zero.

2. 設定延遲時間:

Since STM32F103 has multiple timers, we shall use timer2 for this guide.

First, we need to enable clock access to TIM2 of STM32F1. In order to find which bus is connected to, we need the block diagram of STM32F103 which can be found in datasheet of STM32F1.

From the block diagram, we can find that TIM2 is connected to APB1 bus:


Hence, to enable clock access to TIM2:
/*Enable clock access to timer2*/
RCC->APB1ENR|=RCC_APB1ENR_TIM2EN;

Now, we can create a function named delay which takes ms as argument to indicate the amount to be delayed as following:

void delay(uint16_t ms)

Within the function, set the prescaller for the timer, since STM32F103C8 by default runs at 8MHz, we shall use prescaller of 8000-1 as following:

TIM2->PSC=8000-1; //8 000 000 Hz / 8 000 = 1 000 Hz (1 ms) 

Set the Auto reload register the desired delay -1 :

TIM2->ARR=ms-1;   // desired delay

Enable the timer from control register 1 (CR1):

TIM2->CR1|=TIM_CR1_CEN; 

Wait until the update flag is set in status register and then clear it once it is set:

while(!(TIM2->SR&TIM_SR_UIF)){} //wait UIF to be set
TIM2->SR&=~TIM_SR_UIF; //reset UIF 

Finally, disable the timer:

TIM2->CR1&=~TIM_CR1_CEN;

3. 全部的程式碼:

Hence, the entire code as following::

#include "stm32f1xx.h"

void delay(uint16_t ms)
{
	TIM2->PSC=8000-1; //8 000 000 Hz / 8 000 = 1 000 Hz (1 ms)
	TIM2->ARR=ms-1;   // desired delay
	TIM2->CR1|=TIM_CR1_CEN;
	while(!(TIM2->SR&TIM_SR_UIF)){} //wait UIF to be set
	TIM2->SR&=~TIM_SR_UIF; //reset UIF
	TIM2->CR1&=~TIM_CR1_CEN; // Disable the timer
}

int main(void)
{
	/*Enable clock access to GPIOA*/
	RCC->APB2ENR|=RCC_APB2ENR_IOPAEN;

	/*Configure PA0 as output*/
	GPIOA->CRL|=GPIO_CRL_MODE0;
	GPIOA->CRL&=~(GPIO_CRL_CNF0);
  
	/*Enable clock access to timer2*/
	RCC->APB1ENR|=RCC_APB1ENR_TIM2EN;

	while(1)
	{

		GPIOA->BSRR=GPIO_BSRR_BS0;//Set PA0 to high
		delay(1000); //Delay for 1 seconds
		GPIOA->BSRR=GPIO_BSRR_BR0; // Set PA0 to low
		delay(1000); // Delay for 1 seconds

	}
}


Forth 程式

依據上面的說明,以及參考其他的 Forth 程式,撰寫的 PWM 測試程式如下

\ STM32f103 PWM functiontest
\ 由 GPIO 可用的 alternate function 決定使用的 timer 和 channel
\ PA1 -- USART2_RTS / ADC12_IN1 / TIM2_CH2

\ 8000000 constant HCLK
72 1000 1000 * *  constant HCLK  \ 72 MHz ?
\ 80 1000 1000 * *  constant HCLK 

$40021000 constant RCC_BASE    
  RCC_BASE $18 + constant RCC_APB2ENR   
  RCC_BASE $1C + constant RCC_APB1ENR   

4 constant GPIOAEN 
1 constant TIM2EN

$40010800 constant GPIOA  
    GPIOA $0 + constant GPIOA_CRL
    GPIOA $4 + constant GPIOA_CRH
    GPIOA $8 + constant GPIOA_IDR
    GPIOA $C + constant GPIOA_ODR
    GPIOA $10 + constant GPIOA_BSRR
    GPIOA $14 + constant GPIOA_BRR
    GPIOA $18 + constant GPIOA_LCKR

$40000000 constant TIM2 ( Advanced timer ) 
TIM2 $0 + constant TIM2_CR1 ( read-write )  \ control register 1
TIM2 $4 + constant TIM2_CR2 ( read-write )  \ control register 2
TIM2 $8 + constant TIM2_SMCR ( read-write )  \ slave mode control register
TIM2 $C + constant TIM2_DIER ( read-write )  \ DMA/Interrupt enable register
TIM2 $10 + constant TIM2_SR ( read-write )  \ status register
TIM2 $14 + constant TIM2_EGR ( write-only )  \ event generation register
TIM2 $18 + constant TIM2_CCMR1 ( read-write )  \ capture/compare mode register output  mode
TIM2 $1C + constant TIM2_CCMR2 ( read-write )  \ capture/compare mode register output  mode
TIM2 $20 + constant TIM2_CCER ( read-write )  \ capture/compare enable  register
TIM2 $24 + constant TIM2_CNT ( read-write )  \ counter
TIM2 $28 + constant TIM2_PSC ( read-write )  \ prescaler
TIM2 $2C + constant TIM2_ARR ( read-write )   \ auto-reload register
TIM2 $34 + constant TIM2_CCR1 ( read-write )  \ capture/compare register 1
TIM2 $38 + constant TIM2_CCR2 ( read-write )  \ capture/compare register 2
TIM2 $3C + constant TIM2_CCR3 ( read-write )  \ capture/compare register 3
TIM2 $40 + constant TIM2_CCR4 ( read-write )  \ capture/compare register 4
TIM2 $48 + constant TIM2_DCR ( read-write )  \ DMA control register
TIM2 $4C + constant TIM2_DMAR ( read-write )  \ DMA address for full transfer
TIM2 $30 + constant TIM2_RCR ( read-write )  \ repetition counter register
TIM2 $44 + constant TIM2_BDTR ( read-write )  \ break and dead-time register

1 7 lshift constant ARPE 			\ Auto-reload preload enable
1 constant CEN								\ Counter enable
1 3 lshift constant OPM	 			\ One-pulse mode
1 constant UG		              \ Update generation

\ GPIOA_CRL - only for PA0 - PA7
: gpioa-mode! ( mode pin# -- )
  4 * dup
  $F swap lshift not GPIOA_CRL @ and
  -rot lshift or GPIOA_CRL !
;

: set-pwm ( period dutycyle -- )
	1- TIM2_CCR2 !	  \ duty cycle
	1- TIM2_ARR !	  \ period
;

: s-psc 
  1- TIM2_PSC !	
;

: p-psc 
  TIM2_PSC @ 1+ .	
;

: s-arr
	1- TIM2_ARR !	  \ period
;

: p-arr
	TIM2_ARR @ 1+ .	  \ period
;

: s-ccr 
  1- TIM2_CCR2 !	
;

: p-ccr 
   TIM2_CCR2 @ 1+ .
;

\  --- init output PA1
: init-PA1 ( -- )
  \ IO port A clock enabled
  GPIOAEN RCC_APB2ENR bis!

  \ PA1 -> mode: alternate -> set value 1010 
  \ CNF  - 10 -> 1: Alternate Function, output, 0: Push-pull
  \ MODE - 10: Maximum output speed 2 MHz
  %1010 1 gpioa-mode!
;

  \ 110: PWM mode 1 - In upcounting, channel 1 is active as long as TIMx_CNT<TIMx_CCR1
  \ else inactive. In downcounting, channel 1 is inactive (OC1REF=‘0) as long as
  \ TIMx_CNT>TIMx_CCR1 else active (OC1REF=1).

\ --- init TIM2_CH2
: init-tim2_ch2 ( -- )
	TIM2EN RCC_APB1ENR bis!			\ TIM2 clock enabled
	ARPE TIM2_CR1 ! 			\ ch2 Auto-reload preload enable

  \ Bit 5 CC2P: Capture/Compare 2 output polarity, 0: OC2 active high.
  \ Bit 4 CC2E: Capture/Compare 2 output enable
	1 4 lshift TIM2_CCER ! 				\ CC2E -> OC2 signal is output on the corresponding output pin
	1 11 lshift TIM2_CCMR1 bis!			\ OC2PE (bit 11) -> Output compare 2 preload enable

	%110 12 lshift TIM2_CCMR1 bis!	\ Output compare 2 mode -> 110: PWM mode 1

  \ Update generation - Reinitialize the counter and generates an update of the registers.
	UG  TIM2_EGR !	
;
	
: pwm-init ( -- )
  init-PA1
  init-tim2_ch2

  \ HCLK 是 72MHz,prescaler 不能超過 65536
  \ 所以只能設到 2KHz
  HCLK 2000 / 1- TIM2_PSC !		\ prescaler -> 2000 Hz = 0.5 ms
  2000 50 set-pwm			\ 1000ms - 50ms period - dutycycle as initial value

  \ counter enable
	CEN TIM2_CR1 bis! 
;



舵機控制



MG90S - (S: 金屬齒輪),模拟舵机,180度 - 才能控制角度,360 度只能旋轉
数字舵机Digital Servo和模拟舵机Analog Servo

PWM 測試

控制信號頻率 50Hz,週期 20ms


8MHz = 8*1000*1000


prescaler: 1000 => 8*1000



prescaler for 1us => 1MHz



-----------

   舵機是一種位置(角度)伺服的驅動器,適用於那些需要角度不斷變化並可以保持的控制系統。目前在高檔遙控玩具,如航模,包括飛機模型,潛艇模型;遙控機器人中已經使用得比較普遍。舵機是一種俗稱,其實是一種伺服馬達。


其工作原理是:


控制信號由接收機的通道進入信號調製晶片,獲得直流偏置電壓。它內部有一個基準電路,產生週期為20ms,寬度為1.5ms的基準信號,將獲得的直流偏置電壓與電位器的電壓比較,獲得電壓差輸出。最後,電壓差的正負輸出到電機驅動晶片決定電機的正反轉。當電機轉速一定時,通過級聯減速齒輪帶動電位器旋轉,使得電壓差為0,電機停止轉動。當然我們可以不用去瞭解它的具體工作原理,知道它的控制原理就夠了。就象我們使用電晶體一樣,知道可以拿它來做開關管或放大管就行了,至於管內的電子具體怎麼流動是可以完全不用去考慮的。


舵機的控制:


舵機的控制一般需要一個20ms左右的時基脈衝,該脈衝的高電平部分一般為0.5ms~2.5ms範圍內的角度控制脈衝部分。以180度角度伺服為例,那麼對應的控制關係是這樣的:


   0.5ms--------------0度;

   1.0ms------------45度;

   1.5ms------------90度;

   2.0ms-----------135度;

   2.5ms-----------180度;


這只是一種參考數值,具體的參數,請參見舵機的技術參數。


   小型舵機的工作電壓一般為4.8V或6V,轉速也不是很快,一般為0.22/60度或0.18/60度,所以假如你更改角度控制脈衝的寬度太快時,舵機可能反應不過來。如果需要更快速的反應,就需要更高的轉速了。


要精確的控制舵機,其實沒有那麼容易,很多舵機的位置等級有1024個,那麼,如果舵機的有效角度範圍為180度的話,其控制的角度精度是可以達到180/1024度約0.18度了,從時間上看其實要求的脈寬控制精度為2000/1024us約2us。如果你拿了個舵機,連控制精度為1度都達不到的話,而且還看到舵機在發抖。在這種情況下,只要舵機的電壓沒有抖動,那抖動的就是你的控制脈衝了。而這個脈衝為什麼會抖動呢?當然和你選用的脈衝發生器有關了。


------------


0.5ms - 0 度

14.7ms - 90度

24.5ms - 180度 


stm32f103

較佳的設定

psc - 7200 --> 10KHz -- 0.1ms

arr - 2000 ->  0.1ms * 200 = 20ms -> 50Hz

0.1ms * 500 = 50ms


0.5ms -- -90度  

150 -- 0度

crr = 250 -- 90度


hclk = 72000000 

hclk 100000 / s-psc   => 100k -- 10us

20ms = 10us*100 *20


0.5ms = 500us = 10us *  50

150ms = 10us * 100*150


1Hz - 100us*10*1000


使用這個設定

hclk 100000 / s-psc     => 100k -- 10us                                             
2000 s-arr  => 20ms -> 50Hz                                                                              
50 s-ccr   ok.       (0 度)                                                                         
150 s-ccr  ok.      (90 度)                                                                        
250 s-ccr  ok.     (180 度)









2024年2月4日 星期日

在 STM32F103 上執行 Mecrisp-Stellaris Forth

Forth,是一種宗教

1980 年代,黃大一 (1948-12-26 -- 2023-01-18) 所撰寫的《符式(FORTH) F83 入門 》(松崗出版社 1986) 是我接觸 Forth 的開始。黃大一對 Forth 如宗教般的推崇,也反應了許多人對 Forth 如宗教般的信仰。雖然如此,因為 Forth 的特性,在一般電腦上其實無法和其他語言競爭,其強項為兼具 interactive/compiling 和精簡的基本系統,很適合在嵌入式系統上使用。

因此,我雖然很早就接觸 Forth,但幾乎沒有實際派上用場。直到最近 (2024),一片獨立的 32位元單晶片系統,如 stm32f103,不到台幣 100元,最適合在上面執行 Forth 了。

Mecrisp-Stellaris Forth

下載版本 mecrisp-stellaris-2.6.5,使用 precompiled 目錄下的 mecrisp-stellaris-stm32f103-with-usb-terminal.bin。使用 ST-LINK 燒錄時,不用切換 BOOT0 的狀態。

連上之後,傳回的裝置為 

---------------------------------
[1217966.500259] usb 1-7.3.1: new full-speed USB device number 16 using xhci_hcd
[1217966.591866] usb 1-7.3.1: New USB device found, idVendor=0483, idProduct=5740, bcdDevice= 2.00
[1217966.591871] usb 1-7.3.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[1217966.591872] usb 1-7.3.1: Product: Forth Serial Port
[1217966.591873] usb 1-7.3.1: Manufacturer: Mecrisp (STM32F10x)
[1217966.591874] usb 1-7.3.1: SerialNumber: 0F775724
[1217966.600191] cdc_acm 1-7.3.1:1.0: ttyACM0: USB ACM device
--------------------------------

連接指令 "sudo minicom -b 115200  -D /dev/ttyACM0",要修改設定,加入在按 Enter 時,送出 CRLF。按鍵順序 Ctrl-A -> Z -> U,即可 "開啟加入換列字元" 或 "關閉加入換列字元"。

--------------------
STM32F103 的 USART I/O
USART1:PA9 - TX,PA10 - RX
USART2:PA2 - TX,PA3 - RX
USART3:PB10 - TX,PB11 - RX
--------------------

假如燒錄完成,連線之後,不斷出現 "Unhandled Interrupt 00000003 !",可能是因為 flash 中有其他先前存在的程式,可以將 flash 完整清除看看,參考 Bugs or Benefits ?

差別在於,stm32f103 的 memmap 定義如下

--------------------
MEMORY
{
   rom(RX)   : ORIGIN = 0x00000000, LENGTH = 0x4000
   ram(WAIL) : ORIGIN = 0x20000000, LENGTH = 0x4000
}
--------------------

stm32f103-ra 的 memmap 定義則為

--------------------
MEMORY
{
   rom(RX)   : ORIGIN = 0x00000000, LENGTH = 0x5000
   ram(WAIL) : ORIGIN = 0x20000000, LENGTH = 0x4000
}
--------------------

相關說明 RA Kernel,"Activate RA compiler optimisations by increasing the reserved core size to 0x5000 (up from the 0x4000 of the classic cores) and by adding an assembler switch"。


Forth 專用連工具 e4thcom - A Terminal for Embedded Forth Systems,方便下載程式。

連線指令 "sudo ./e4thcom -t mecrisp-st -d ttyUSB0 -b B115200 --idm"

ascii      ascii-xfr -dsv -l 200  










2024年1月4日 星期四

小米 2S

 https://www.techbang.com/posts/14921-transmission-lines-millet-2s-easily-root

https://blog.twtnn.com/2016/09/22snote-4g-root.html

https://sixu.life/xiaomi-2s-international-version-of-root-tutorial.html


https://zhidao.baidu.com/question/173620507582411934


https://mi.ezbox.idv.tw/thread-1662-1-1.html

小米2S - 小米MIUI ROM 官方刷機包下載

http://www.android5.online/Android/androidsj/Androidsj/xmsj/201702/42860.html

https://onfix.cn/course/319?bid=1&mid=44


2024年1月3日 星期三

Orange pi zero 3 安裝 Klipper

官網連結 - http://www.orangepi.org/html/hardWare/computerAndMicrocontrollers/service-and-support/Orange-Pi-Zero-3.html


安裝參考 -- https://github.com/nhchiu/Klipper-on-Orange-Pi-Zero-2/tree/main


安裝的 image,Orangepizero3_1.0.0_debian_bullseye_server_linux5.4.125.img


在 Linux 下,使用 USB-RS連結,tx - rx, rx - tx,線要交換才行

minicom -b 115200 -D /dev/ttyUSB0



---
=== 開機後的畫面 ===
[  OK  ] Finished lircd(8) initialization helper tool.                          
[  OK  ] Finished Permit User Sessions.                                         
         Starting LSB: set CPUFreq kernel parameters...                         
         Starting Flexible IR remote input/output application support...        
         Starting Hold until boot process finishes up...                        
         Starting Terminate Plymouth Boot Screen...                             
                                                                                
orangepizero3 login: orangepi (automatic login)                                 
                                                                                
  ___  ____ ___   _____             _____                                       
 / _ \|  _ \_ _| |__  /___ _ __ ___|___ /                                       
| | | | |_) | |    / // _ \ '__/ _ \ |_ \                                       
| |_| |  __/| |   / /|  __/ | | (_) |__) |                                      
 \___/|_|  |___| /____\___|_|  \___/____/                                       
                                                                                
Welcome to Orange Pi 1.0.0 Bullseye with Linux 5.4.125                          
                                                                                
System load:   63%              Up time:       1 min                            
Memory usage:  21% of 952M      IP:                                             
CPU temp:      48°C             Usage of /:    6% of 29G                        
                                                                                
[ General system configuration (beta): orangepi-config ]                        
                                                                                
Last login: Fri Jun 30 06:37:41 UTC 2023 on tty1                                
orangepi@orangepizero3:~$
---

執行更新,apt update --allow-releaseinfo-change。若出現 "Certificate verification failed: The certificate is NOT trusted" 的錯誤,執行下列指令,"apt-get install ca-certificates --reinstall"。


網誌存檔