-
Notifications
You must be signed in to change notification settings - Fork 4
初期位置調整
IsakiMatsubara edited this page Sep 18, 2021
·
10 revisions
- サーボの初期位置を格納する配列です
- サーボの0~180を10倍した数字、0~1800の範囲で初期位置を指定します
export let servoSetInit = [1000, 630, 300, 600, 240, 600, 1000, 720, 900, 900, 900, 900]
- EEPROMに保存したサーボの初期位置を取得し、servoSetInitの値を書き換える関数です
- EEPROMに
function loadPos() {
let readBuf = reep(0x00, 1);
if (readBuf[0] == 0x01) {
readBuf = reep(0x02, 16);
for (let i = 0; i < 8; i++) {
servoSetInit[i] = (readBuf[i * 2] << 8) | (readBuf[i * 2 + 1]);
}
}
}
//% blockId=PLEN:bit_reep
//% block="readEEPROM %eepAdr| byte%num"
//% eepAdr.min=910 eepAdr.max=2000
//% num.min=0 num.max=43
//% weight=10 group="Servo Adjust" advanced=true
//% deprecated=true
export function reep(eepAdr: number, num: number) {
let data = pins.createBuffer(2);
data[0] = eepAdr >> 8;
data[1] = eepAdr & 0xFF;
// need adr change code
pins.i2cWriteBuffer(romAdr1, data);
return pins.i2cReadBuffer(romAdr1, num, false);
}
//% block
//% weight=9 group="Servo Adjust" advanced=true
//% deprecated=true
export function savePositon(servoNum: number, adjustNum: number) {
adjustNum = servoSetInit[servoNum] + adjustNum;
weep(0, 1); //write flag
weep(servoNum * 2 + 2, (adjustNum & 0xff00) >> 8);
weep(servoNum * 2 + 3, adjustNum & 0xff);
}
//% block
//% weight=8 group="Servo Adjust" advanced=true
//% deprecated=true
export function resetPosition() {
weep(0, 0); //write flag reset
for (let n = 0; n < 8; n++) {
weep(n * 2 + 2, (servoSetInit[n] & 0xff00) >> 8);
weep(n * 2 + 3, servoSetInit[n] & 0xff);
}
}
//% block
//% weight=7 group="Servo Adjust" advanced=true
//% deprecated=true
export function servoAdjust(servoNum: number, adjustNum: number) {
let adjNum = servoSetInit[servoNum] + adjustNum
if (100 > adjNum) {
adjustNum = adjustNum + 1;
} else if (adjNum > 1700) {
adjustNum = adjustNum - 1;
} else {
servoWrite(servoNum, (adjNum / 10));
basic.pause(0.5);
}
return adjustNum;
}
処理 | 説明 |
---|---|
let servoSetInit = []](servoSetInit) | 初期位置を格納します。 |
loadPos() | 初期位置調整の値を取得し、上書きします。 |