Skip to content

Commit

Permalink
Uploaded main files
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterChmurciak committed Sep 6, 2019
1 parent cc6a862 commit 8fa498c
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 0 deletions.
105 changes: 105 additions & 0 deletions +arduinoioaddons/+Pololu/Pololu_VL53L0X.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
% Matlab implementation of Pololu arduino library for
% VL53L0X Time-of-Flight distance sensor.
%
% This code is part of the AutomationShield hardware and software
% ecosystem. Visit http://www.automationshield.com for more
% details. This code is licensed under a Creative Commons
% Attribution-NonCommercial 4.0 International License.
%
% Created by Peter Chmurciak.
% Last update: 6.9.2019.

% Sensor class definition, inheriting LibraryBase class properties
classdef Pololu_VL53L0X < matlabshared.addon.LibraryBase

% Define command IDs for commandHandler method
properties(Access = private, Constant = true)
Pololu_VL53L0X_CREATE = hex2dec('00')
Pololu_VL53L0X_BEGIN = hex2dec('01')
Pololu_VL53L0X_READ = hex2dec('02')
Pololu_VL53L0X_DELETE = hex2dec('03')
end

% Define the required names and pathways to used source files
properties(Access = protected, Constant = true)
LibraryName = 'Pololu/Pololu_VL53L0X'
DependentLibraries = {}
LibraryHeaderFiles = {}
CppHeaderFile = fullfile(arduinoio.FilePath(mfilename('fullpath')), 'src', 'Pololu_VL53L0X.h')
CppClassName = 'Pololu_VL53L0X'
ResourceOwner = 'Pololu/Pololu_VL53L0X'
Pins = {'A4', 'A5'}
end

% Hidden methods (not meant to be used directly by the user) with unrestricted access
methods(Hidden, Access = public)

% Constructor
function obj = Pololu_VL53L0X(parentObj)
obj.Parent = parentObj;
count = getResourceCount(obj.Parent, obj.ResourceOwner);
if count > 0
error('You can use only one VL53L0X sensor!');
end
incrementResourceCount(obj.Parent, obj.ResourceOwner);
createPololu_VL53L0X(obj);
end

% Function for sensor object creation
function createPololu_VL53L0X(obj)
try
cmdID = obj.Pololu_VL53L0X_CREATE;
configurePinResource(obj.Parent, 'A4', obj.ResourceOwner, 'I2C');
configurePinResource(obj.Parent, 'A5', obj.ResourceOwner, 'I2C');
sendCommand(obj, obj.LibraryName, cmdID, []);
catch e
throwAsCaller(e);
end
end
end

% Protected methods (not meant to be used directly by the user)
methods(Access = protected)

% Function for sensor object removal
function delete(obj)
try
parentObj = obj.Parent;
for iLoop = obj.Pins
configurePinResource(parentObj, iLoop{:}, obj.ResourceOwner, 'Unset');
end
decrementResourceCount(parentObj, obj.ResourceOwner);
cmdID = obj.Pololu_VL53L0X_DELETE;
sendCommand(obj, obj.LibraryName, cmdID, []);
catch
end
end
end

% Public methods - meant to be used directly by the user
methods(Access = public)

% Function for sensor initialisation
function beginSensor(obj)
cmdID = obj.Pololu_VL53L0X_BEGIN;
success = sendCommand(obj, obj.LibraryName, cmdID, []);
if ~success
error('Error initialising VL53L0X sensor!');
end
end

% Function for reading data from sensor
function out = readSensor(obj)
cmdID = obj.Pololu_VL53L0X_READ;
out = sendCommand(obj, obj.LibraryName, cmdID, []);
out = 256 * out(1) + out(2);
end

% Function for sensor object removal
function removeSensor(obj)
delete(obj);
end

end

end
78 changes: 78 additions & 0 deletions +arduinoioaddons/+Pololu/src/Pololu_VL53L0X.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Matlab implementation of Pololu arduino library for
VL53L0X Time-of-Flight distance sensor.
This code is part of the AutomationShield hardware and software
ecosystem. Visit http://www.automationshield.com for more
details. This code is licensed under a Creative Commons
Attribution-NonCommercial 4.0 International License.
Created by Peter Chmurciak.
Last update: 4.7.2019.
*/

// Include necessary libraries
#include "LibraryBase.h"
#include "Wire.h"
#include "VL53L0X.h"

// Define command IDs for commandHandler method
#define Pololu_VL53L0X_CREATE 0x00
#define Pololu_VL53L0X_BEGIN 0x01
#define Pololu_VL53L0X_READ 0x02
#define Pololu_VL53L0X_DELETE 0x03

// Sensor class definition, inheriting LibraryBase class properties
class Pololu_VL53L0X : public LibraryBase {

public:
// Sensor object pointer
VL53L0X *distanceSensor;

// Constructor
Pololu_VL53L0X(MWArduinoClass& a) {
libName = "Pololu/Pololu_VL53L0X";
a.registerLibrary(this);
}

// Override the commandHandler method, assigning each command ID to appropriate method
void commandHandler(byte cmdID, byte* dataIn, unsigned int payloadSize) {
// Decide based on received command ID
switch(cmdID) {
// Create sensor object
case Pololu_VL53L0X_CREATE: {
distanceSensor = new VL53L0X();
sendResponseMsg(cmdID, 0, 0);
break;
}

// Initialise sensor and return 1 or 0 on success or failure
case Pololu_VL53L0X_BEGIN: {
Wire.begin();
byte success[1] = {distanceSensor->init()};
distanceSensor->setMeasurementTimingBudget(20000);
distanceSensor->startContinuous();
sendResponseMsg(cmdID, success, 1);
break;
}

// Get measurement from sensor in milimetres and return the information as two bytes
case Pololu_VL53L0X_READ: {
uint16_t milliMeter = distanceSensor->readRangeContinuousMillimeters();
byte high = highByte(milliMeter);
byte low = lowByte(milliMeter);
byte mm[2] = {high,low};
sendResponseMsg(cmdID, mm, 2);
break;
}

// Delete sensor object
case Pololu_VL53L0X_DELETE: {
delete distanceSensor;
sendResponseMsg(cmdID, 0, 0);
break;
}

} // end of switch statement
} // end of commandHandler method definition
}; // end of class definition

0 comments on commit 8fa498c

Please sign in to comment.