Added support for Reflection Raw mode for the color sensor to the simulator (#1017)

* redesigned-code-to-support-ref-raw

Code that adds reflection raw support for the color sensor. The range of values for this is from 0 to 1023, because analog to digital converter is 10 bits. In fact, the color sensor gives values ​​of about 400 - 700, but I decided to leave the range, which could theoretically be. For other cases (reflections and ambient lighting), the range remains from 0 to 100. The average value when setting the mode in the simulator is displayed as 512, for other modes 50%.

* block-description-update

Block description update, as it did not take into account the mode of raw reflection values.
This commit is contained in:
Dmitriy Antipov 2023-05-06 01:54:56 +03:00 committed by GitHub
parent 2ca706df70
commit 9be35a1034
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 9 deletions

View File

@ -250,7 +250,7 @@ namespace sensors {
}
/**
* Measure the ambient or reflected light value from 0 (darkest) to 100 (brightest).
* Measure the ambient or reflected light value from 0 (darkest) to 100 (brightest). In raw reflection light mode, the range will be different.
* @param sensor the color sensor port
*/
//% help=sensors/color-sensor/light

View File

@ -21,7 +21,7 @@ namespace pxsim {
export class ColorSensorNode extends UartSensorNode {
id = NodeType.ColorSensor;
private color: number = 50;
private color: number = 0;
constructor(port: number) {
super(port);
@ -40,5 +40,13 @@ namespace pxsim {
getValue() {
return this.color;
}
setMode(mode: number) {
this.mode = mode;
if (this.mode == ColorSensorMode.RefRaw) this.color = 512;
else this.color = 50;
this.changed = true;
this.modeChanged = true;
}
}
}

View File

@ -241,6 +241,8 @@ namespace pxsim.visuals {
view = new ColorGridControl(this.element, this.defs, state, port);
} else if (state.getMode() == ColorSensorMode.Reflected) {
view = new ColorWheelControl(this.element, this.defs, state, port);
} else if (state.getMode() == ColorSensorMode.RefRaw) {
view = new ColorWheelControl(this.element, this.defs, state, port);
} else if (state.getMode() == ColorSensorMode.Ambient) {
view = new ColorWheelControl(this.element, this.defs, state, port);
}

View File

@ -26,8 +26,12 @@ namespace pxsim.visuals {
return 131;
}
private getMaxValue() {
return 100;
private getMaxValue(state: ColorSensorMode) {
return (state == ColorSensorMode.RefRaw ? 1023 : 100);
}
private mapValue(x: number, inMin: number, inMax: number, outMin: number, outMax: number) {
return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}
updateState() {
@ -35,10 +39,12 @@ namespace pxsim.visuals {
return;
}
const node = this.state;
const percentage = node.getValue();
const inversePercentage = this.getMaxValue() - percentage;
svg.setGradientValue(this.colorGradient, inversePercentage + "%");
this.reporter.textContent = `${parseFloat((percentage).toString()).toFixed(0)}%`;
const value = node.getValue();
let inverseValue = this.getMaxValue(node.getMode()) - value;
if (node.getMode() == ColorSensorMode.RefRaw) inverseValue = this.mapValue(inverseValue, 0, 1023, 0, 100);
svg.setGradientValue(this.colorGradient, inverseValue + "%");
this.reporter.textContent = `${parseFloat((value).toString()).toFixed(0)}`;
if (node.getMode() != ColorSensorMode.RefRaw) this.reporter.textContent += `%`;
}
updateColorLevel(pt: SVGPoint, parent: SVGSVGElement, ev: MouseEvent) {
@ -47,7 +53,7 @@ namespace pxsim.visuals {
const height = bBox.height;
let t = Math.max(0, Math.min(1, (height + bBox.top / this.scaleFactor - cur.y / this.scaleFactor) / height));
const state = this.state;
state.setColor(t * this.getMaxValue());
state.setColor(t * this.getMaxValue(state.getMode()));
}
getInnerView(parent: SVGSVGElement, globalDefs: SVGDefsElement) {

View File

@ -29,6 +29,7 @@ namespace pxsim.visuals {
switch (mode) {
case ColorSensorMode.Colors: this.updateSensorLightVisual('#0062DD'); return; // blue
case ColorSensorMode.Reflected: this.updateSensorLightVisual('#F86262'); return; // red
case ColorSensorMode.RefRaw: this.updateSensorLightVisual('#F86262'); return; // red
case ColorSensorMode.Ambient: this.updateSensorLightVisual('#67C3E2'); return; // light blue
}
this.updateSensorLightVisual('#ffffff');