Skip to content

Commit

Permalink
fix loadcell
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzhiyong0410 committed Jan 9, 2025
1 parent abaa928 commit da05f15
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 42 deletions.
2 changes: 1 addition & 1 deletion _locales/zh-CN/sugar-strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@
"Sugar.SolarpwrDate.Minute|block": "",
"Sugar.SolarpwrDate.Sec|block": "",
"Sugar.loadcellCalibrateScale|block": "称重传感器 开始校准",
"Sugar.loadcellBegin|block": "称重传感器 设置空载值 %zeroOffset 校准值 %factor(校准后获得)",
"Sugar.loadcellBegin|block": "称重传感器 设置校准值 %factor(校准后获得)",
"Sugar.loadcellCalibrateSetPeel|block": "称重传感器 去皮(重量清零)",
"Sugar.loadcellGetWeight|block": "称重传感器 测量重量(克)",
"{id:subcategory}Sensor": "传感器",
Expand Down
2 changes: 1 addition & 1 deletion _locales/zh-TW/sugar-strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@
"Sugar.SolarpwrDate.Minute|block": "",
"Sugar.SolarpwrDate.Sec|block": "",
"Sugar.loadcellCalibrateScale|block": "稱重感測器 開始校準",
"Sugar.loadcellBegin|block": "稱重感測器 設定空載值 %zeroOffset 校準值 %factor(校準後獲得)",
"Sugar.loadcellBegin|block": "稱重感測器 設定校準值 %factor(校準後獲得)",
"Sugar.loadcellCalibrateSetPeel|block": "稱重感測器 去皮(重量清零)",
"Sugar.loadcellGetWeight|block": "稱重感測器 測量重量(克)",
"{id:subcategory}Sensor": "感應器",
Expand Down
6 changes: 3 additions & 3 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1043,15 +1043,15 @@ namespace Sugar {
* @param zeroOffset is zero offset, eg: 2071.921875
* @param factor is calibration factor, eg: 1.53034747292419
*/
//% blockId=loadcellBegin block="loadcell sensor set zeroOffset %zeroOffset factor %factor"
//% blockId=loadcellBegin block="loadcell sensor set factor %factor"
//% subcategory=Sensor group=I2C weight=38 color=#49CEF7
export function loadcellBegin(zeroOffset: number, factor: number): void {
export function loadcellBegin(factor: number): void {
if (!loadcellInit) {
loadcell = new SugarLoadcell()
loadcell.begin()
loadcellInit = true
}
loadcell.begin(true, zeroOffset, factor)
loadcell.begin(true, factor)
}

//% blockId=loadcellCali block="loadcell sensor calibration begins"
Expand Down
4 changes: 2 additions & 2 deletions pxt.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sugar",
"version": "0.4.4",
"version": "0.4.5",
"description": "Extension for Kittenbot Sugar",
"license": "MIT",
"dependencies": {
Expand All @@ -24,7 +24,7 @@
],
"public": true,
"targetVersions": {
"target": "7.0.51",
"target": "7.0.57",
"targetId": "microbit"
},
"supportedTargets": [
Expand Down
55 changes: 20 additions & 35 deletions sugarLoadcell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,10 @@ const NAU7802_CAL_IN_PROGRESS = 1;
const NAU7802_CAL_FAILURE = 2;

class SugarLoadcell {

_zeroOffset: number;
_calibrationFactor: number;
_peel: number;

begin(initialize: boolean = true, zeroOffset: number = 2071.921875, factor: number = 1.53034747292419): boolean {
begin(initialize: boolean = true, factor: number = 102123.5): boolean {
this._peel = 0
let result = true
if (initialize) {
Expand All @@ -111,10 +109,16 @@ class SugarLoadcell {
result = result && this.calibrateAFE()
}
if (result) {
this.setZeroOffset(zeroOffset)
this.setCalibrationFactor(factor)
}


// 54
// 0b00110110
// bit 0~1: Offset Calibration System
// bit 2: start Calibration
this.setRegister(NAU7802_CTRL2,54)
basic.pause(2000)

return result
}

Expand Down Expand Up @@ -251,10 +255,6 @@ class SugarLoadcell {
return this.waitForCalibrateAFE(1000)
}

setZeroOffset(new_zero_offset: number) {
this._zeroOffset = new_zero_offset
}

setCalibrationFactor(new_cal_factor: number): void {
this._calibrationFactor = new_cal_factor
}
Expand All @@ -264,15 +264,16 @@ class SugarLoadcell {
}

getReading(): number {
try {
pins.i2cWriteNumber(DEVICE_ADDRESS, NAU7802_ADCO_B2, NumberFormat.UInt8BE)
let value_list = pins.i2cReadBuffer(DEVICE_ADDRESS, 3)
let value: number = (value_list[0] << 24) | (value_list[1] << 16) | (value_list[2] << 8);
value >>= 16;
return value
} catch {
return 0
pins.i2cWriteNumber(DEVICE_ADDRESS, NAU7802_ADCO_B2, NumberFormat.UInt8BE)
let value_list = pins.i2cReadBuffer(DEVICE_ADDRESS, 3)
let _adc_out_2 = value_list[0]
let _adc_out_1 = value_list[1]
let _adc_out_0 = value_list[2]
let value = (_adc_out_2 << 24) | (_adc_out_1 << 16) | (_adc_out_0 << 8)
if (value & (1 << 31)){
value -= 1 << 32
}
return value
}

getAverage(average_amount: number): number {
Expand All @@ -292,21 +293,12 @@ class SugarLoadcell {
total /= average_amount
return total
}

calculateZeroOffset(average_amount: number = 8): void {
this.setZeroOffset(this.getAverage(average_amount))
}

calculateCalibrationFactor(weight_on_scale: number, average_amount: number = 8): void {
let onScale = this.getAverage(average_amount)
let newCalFactor = (onScale - this._zeroOffset) / weight_on_scale
let newCalFactor = onScale / weight_on_scale
this.setCalibrationFactor(newCalFactor)
}

getZeroOffset(): number {
return this._zeroOffset
}

getCalibrationFactor(): number {
return this._calibrationFactor
}
Expand All @@ -317,12 +309,7 @@ class SugarLoadcell {

getWeight(allow_negative_weights: boolean = true, samples_to_take: number = 8): number {
let on_scale = this.getAverage(samples_to_take)
if (!allow_negative_weights) {
if (on_scale < this._zeroOffset) {
on_scale = this._zeroOffset
}
}
let weight = Math.round((on_scale - this._zeroOffset) / this._calibrationFactor)
let weight = Math.round(on_scale / this._calibrationFactor)
return weight
}

Expand All @@ -332,8 +319,6 @@ class SugarLoadcell {

calibrateScale(): void {
serial.writeString("start calibrate.\n")
this.calculateZeroOffset(64)
serial.writeValue("new unloaded value", this.getZeroOffset())
serial.writeString("Place an object of known weight on the scale and press enter when ready.\n")
serial.readLine()
serial.writeString("Enter the weight of the object and press enter (g). \n")
Expand Down

0 comments on commit da05f15

Please sign in to comment.