custom library HX711 Index exceeds the number of array elements. Index must not exceed 2.
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
% Copyright 2018, Nicholas Giacoboni
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%
% 1. Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
%
% 2. Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in the
% documentation and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
% IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
% INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
% NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
% PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
% WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
% OF SUCH DAMAGE.
%**************************************************************************
classdef basic_HX711 < matlabshared.addon.LibraryBase
properties(Access = protected)
Pins %Data and Clock pin
end
properties (Access = private, Constant = true)
READ_HX711 = hex2dec('01')
end
properties(Access = protected, Constant = true)
LibraryName = 'basicHX711/basic_HX711'
DependentLibraries = {}
LibraryHeaderFiles = {}
CppHeaderFile = fullfile(arduinoio.FilePath(mfilename('fullpath')), 'src', 'basic_HX711.h')
CppClassName = 'basic_HX711'
end
methods(Hidden, Access = public)
function obj = basic_HX711(parentObj,inputPins)
obj.Parent = parentObj;
obj.Pins = getTerminalsFromPins(obj.Parent,inputPins);
configurePin(parentObj,inputPins{2},'DigitalInput'); % Data Pin
configurePin(parentObj,inputPins{3},'DigitalOutput'); % Clock Pin
end
end
methods(Access = public)
function force = read_HX711(obj)
cmdID = obj.READ_HX711;
inputs = obj.Pins;
value = sendCommand(obj, obj.LibraryName, cmdID, inputs);
value(3)=bitshift(value(3),16);
value(2)=bitshift(value(2),8);
force = bitor(value(3),bitor(value(2),value(1)));
end
end
end
%**************************************************************************
listArduinoLibraries
ans =
12×1 cell array
{'APDS9960' }
{'Adafruit/MotorShieldV2'}
{'CAN' }
{'I2C' }
{'MotorCarrier' }
{'RotaryEncoder' }
{'SPI' }
{'Serial' }
{'Servo' }
{'ShiftRegister' }
{'Ultrasonic' }
{'basicHX711/basic_HX711'}
a = arduino('COM6', 'Uno', 'Libraries','basicHX711/basic_HX711','ForceBuildOn','True');
>> Loadcell = addon(a,'basicHX711/basic_HX711',{'D2','D3'})
Index exceeds the number of array elements. Index must not exceed 2.
%**************************************************************************
I'm currently working on creating a custom library for the HX711 sensor, but I'm encountering an issue with the 'Index exceeds the number of array elements. Index must not exceed 2.' I hope someone can help me troubleshoot.
0 commentaires
Réponses (1)
Namnendra
le 24 Juil 2024
Hi Azrul,
The error message "Index exceeds the number of array elements. Index must not exceed 2." suggests that there might be an issue with how the `inputPins` array is being handled in your `basic_HX711` class constructor. Specifically, it seems like the code is trying to access an index that doesn't exist in the `inputPins` array.
Let's go through the possible issues and solutions step by step:
1. Check Input Pins
Ensure that you are passing the correct number of pins when creating the `basic_HX711` object. The constructor expects an array with at least three elements:
- the first element is the parent object,
- the second is the Data Pin,
- the third is the Clock Pin.
2. Constructor Definition
Verify that the constructor of the `basic_HX711` class correctly handles the `inputPins` array. The constructor should look something like this:
methods(Hidden, Access = public)
function obj = basic_HX711(parentObj, inputPins)
obj.Parent = parentObj;
obj.Pins = getTerminalsFromPins(obj.Parent, inputPins);
configurePin(parentObj, inputPins{1}, 'DigitalInput'); % Data Pin
configurePin(parentObj, inputPins{2}, 'DigitalOutput'); % Clock Pin
end
end
3. Verify Pin Configuration
Ensure that the pin configuration commands in the constructor are correct. The `inputPins` array should have at least two elements, and the pins should be configured properly.
4. Creating the Addon
When creating the addon, make sure you are passing the correct pins:
Loadcell = addon(a, 'basicHX711/basic_HX711', {'D2', 'D3'});
5. Debugging
Add debugging statements to check the contents of `inputPins` and ensure they are being passed correctly:
methods(Hidden, Access = public)
function obj = basic_HX711(parentObj, inputPins)
disp('Input Pins:');
disp(inputPins);
obj.Parent = parentObj;
obj.Pins = getTerminalsFromPins(obj.Parent, inputPins);
configurePin(parentObj, inputPins{1}, 'DigitalInput'); % Data Pin
configurePin(parentObj, inputPins{2}, 'DigitalOutput'); % Clock Pin
end
end
Example Code with Debugging Statements
Here is the modified `basic_HX711` class with added debugging statements:
classdef basic_HX711 < matlabshared.addon.LibraryBase
properties(Access = protected)
Pins % Data and Clock pin
end
properties (Access = private, Constant = true)
READ_HX711 = hex2dec('01')
end
properties(Access = protected, Constant = true)
LibraryName = 'basicHX711/basic_HX711'
DependentLibraries = {}
LibraryHeaderFiles = {}
CppHeaderFile = fullfile(arduinoio.FilePath(mfilename('fullpath')), 'src', 'basic_HX711.h')
CppClassName = 'basic_HX711'
end
methods(Hidden, Access = public)
function obj = basic_HX711(parentObj, inputPins)
% Debugging statements
disp('Input Pins:');
disp(inputPins);
obj.Parent = parentObj;
obj.Pins = getTerminalsFromPins(obj.Parent, inputPins);
configurePin(parentObj, inputPins{1}, 'DigitalInput'); % Data Pin
configurePin(parentObj, inputPins{2}, 'DigitalOutput'); % Clock Pin
end
end
methods(Access = public)
function force = read_HX711(obj)
cmdID = obj.READ_HX711;
inputs = obj.Pins;
value = sendCommand(obj, obj.LibraryName, cmdID, inputs);
value(3) = bitshift(value(3), 16);
value(2) = bitshift(value(2), 8);
force = bitor(value(3), bitor(value(2), value(1)));
end
end
end
Conclusion
Ensure that:
- You are passing the correct number of pins when creating the `basic_HX711` object.
- The constructor handles the `inputPins` array correctly.
- Add debugging statements to verify the contents of `inputPins`.
By following these steps, you should be able to identify and resolve the issue with the `inputPins` array, allowing you to successfully create the custom library for the HX711 sensor.
Above information and code is just to give you an approach on how to resolve the issue and may not handle all edge cases.
Thank you.
0 commentaires
Voir également
Catégories
En savoir plus sur Run on Target Hardware dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!