Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added Grid-EYE thermopile integration
Common thermopile functionality has been extracted out for easy integration of other sensors
- Loading branch information
Showing
10 changed files
with
208 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class GridEyeConfig { | ||
busNumber: number = 1; | ||
address: number = 0x69; | ||
deltaThreshold: number = 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { DynamicModule, Module } from '@nestjs/common'; | ||
import { GridEyeService } from './grid-eye.service'; | ||
import { EntitiesModule } from '../../entities/entities.module'; | ||
import { ConfigModule } from '../../config/config.module'; | ||
import { ScheduleModule } from '@nestjs/schedule'; | ||
|
||
@Module({}) | ||
export default class GridEyeModule { | ||
static forRoot(): DynamicModule { | ||
return { | ||
module: GridEyeModule, | ||
imports: [EntitiesModule, ConfigModule, ScheduleModule.forRoot()], | ||
providers: [GridEyeService] | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { GridEyeService } from './grid-eye.service'; | ||
|
||
describe('GridEyeService', () => { | ||
let service: GridEyeService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [GridEyeService] | ||
}).compile(); | ||
|
||
service = module.get<GridEyeService>(GridEyeService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* tslint:disable:no-bitwise */ | ||
import { | ||
Injectable, | ||
OnApplicationBootstrap, | ||
OnApplicationShutdown | ||
} from '@nestjs/common'; | ||
import i2cBus, { PromisifiedBus } from 'i2c-bus'; | ||
import { Entity } from '../../entities/entity.entity'; | ||
import { EntitiesService } from '../../entities/entities.service'; | ||
import { Sensor } from '../../entities/sensor.entity'; | ||
import * as math from 'mathjs'; | ||
import { Interval } from '@nestjs/schedule'; | ||
import { ThermopileOccupancySensor } from '../../util/thermopile/thermopile-occupancy.sensor'; | ||
import { GridEyeConfig } from './grid-eye.config'; | ||
import { ConfigService } from '../../config/config.service'; | ||
|
||
const TEMPERATURE_REGISTER_START = 0x80; | ||
const FRAMERATE_REGISTER = 0x02; | ||
|
||
@Injectable() | ||
export class GridEyeService extends ThermopileOccupancySensor | ||
implements OnApplicationBootstrap, OnApplicationShutdown { | ||
private readonly config: GridEyeConfig; | ||
private i2cBus: PromisifiedBus; | ||
private sensor: Entity; | ||
|
||
constructor( | ||
private readonly entitiesService: EntitiesService, | ||
private readonly configService: ConfigService | ||
) { | ||
super(); | ||
this.config = this.configService.get('gridEye'); | ||
} | ||
|
||
async onApplicationBootstrap(): Promise<void> { | ||
this.i2cBus = await i2cBus.openPromisified(this.config.busNumber); | ||
this.setRegister(FRAMERATE_REGISTER, 1); // set framerate to 1 FPS -> less noise | ||
this.sensor = this.entitiesService.add( | ||
new Sensor('grideye_occupancy_count', 'GridEYE Occupancy Count') | ||
); | ||
} | ||
|
||
async onApplicationShutdown(signal?: string): Promise<void> { | ||
return this.i2cBus.close(); | ||
} | ||
|
||
@Interval(1000) | ||
async updateState(): Promise<void> { | ||
const coordinates = await this.getCoordinates(this.config.deltaThreshold); | ||
|
||
this.sensor.state = coordinates.length; | ||
this.sensor.attributes.coordinates = coordinates; | ||
} | ||
|
||
async getPixelTemperatures(): Promise<number[][]> { | ||
const temperatures = []; | ||
for (let i = 0; i < 64; i++) { | ||
temperatures.push(await this.getPixelTemperature(i)); | ||
} | ||
|
||
return math.reshape(temperatures, [8, 8]) as number[][]; | ||
} | ||
|
||
async getPixelTemperature(pixelAddr: number): Promise<number> { | ||
const pixelLowRegister = TEMPERATURE_REGISTER_START + 2 * pixelAddr; | ||
let temperature = await this.getRegister(pixelLowRegister, 2); | ||
|
||
if (temperature & (1 << 11)) { | ||
temperature &= ~(1 << 11); | ||
temperature = temperature * -1; | ||
} | ||
|
||
return temperature * 0.25; | ||
} | ||
|
||
async getRegister(register: number, length: number): Promise<number> { | ||
const commandBuffer = Buffer.alloc(1); | ||
commandBuffer.writeUInt8(register, 0); | ||
const resultBuffer = Buffer.alloc(length); | ||
|
||
await this.i2cBus.i2cWrite( | ||
this.config.address, | ||
commandBuffer.length, | ||
commandBuffer | ||
); | ||
await this.i2cBus.i2cRead(this.config.address, length, resultBuffer); | ||
|
||
const lsb = resultBuffer.readUInt8(0); | ||
const msb = resultBuffer.readUInt8(1); | ||
|
||
return (msb << 8) | lsb; | ||
} | ||
|
||
async setRegister(register: number, value: number): Promise<void> { | ||
const commandBuffer = Buffer.alloc(2); | ||
commandBuffer.writeUInt8(register, 0); | ||
commandBuffer.writeUInt8(value, 1); | ||
|
||
await this.i2cBus.i2cWrite( | ||
this.config.address, | ||
commandBuffer.length, | ||
commandBuffer | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/integrations/omron-d6t/cluster.entity.ts → src/util/thermopile/cluster.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Pixel } from './pixel'; | ||
import * as math from 'mathjs'; | ||
import { Cluster } from './cluster'; | ||
|
||
export abstract class ThermopileOccupancySensor { | ||
abstract async getPixelTemperatures(): Promise<number[][]>; | ||
|
||
async getCoordinates(deltaThreshold: number): Promise<number[][]> { | ||
const temperatures = await this.getPixelTemperatures(); | ||
const relevantPixels = this.findRelevantPixels( | ||
temperatures as number[][], | ||
deltaThreshold | ||
); | ||
const clusters = this.clusterPixels(relevantPixels); | ||
|
||
return clusters.map(cluster => [cluster.center.x, cluster.center.y]); | ||
} | ||
|
||
findRelevantPixels(data: number[][], deltaThreshold: number): Pixel[] { | ||
const mean = math.mean(data); | ||
const threshold = mean + deltaThreshold; | ||
|
||
const relevantPixels: Pixel[] = []; | ||
for (const [x, row] of data.entries()) { | ||
for (const [y, value] of row.entries()) { | ||
if (value >= threshold) { | ||
relevantPixels.push(new Pixel(x, y, value)); | ||
} | ||
} | ||
} | ||
|
||
return relevantPixels; | ||
} | ||
|
||
clusterPixels(pixels: Pixel[]): Cluster[] { | ||
const clusters: Cluster[] = []; | ||
pixels.forEach(pixel => { | ||
const neighbor = clusters.find(cluster => cluster.isNeighboredTo(pixel)); | ||
if (neighbor === undefined) { | ||
clusters.push(new Cluster([pixel])); | ||
} else { | ||
neighbor.pixels.push(pixel); | ||
} | ||
}); | ||
|
||
return clusters; | ||
} | ||
} |