How to turn on a GPIO/output for one second

10 ビュー (過去 30 日間)
Nitesh Ramdin
Nitesh Ramdin 2020 年 11 月 24 日
編集済み: Walter Roberson 2020 年 11 月 26 日
Good day,
I am building a project for my degree. I have created a simple neural netowrk to classify images taken from my Pi Cam for an autonomous car.
I am at the piont where i have the trained model and based on the input image iether go FORWARD, RIGHT or LEFT. This is something that seems simple but i am unable to turn on the GPIO of 1sec. For some reason when an image is read it continueously turn on that respective GPIO out command.
I have attached my code if this make sense. i am a begginer.
%% Connect Raspberry Pi 4B & camera Board in Command Window
% mypi = raspi;
% mypi = raspi('192.168.43.157','pi','pi')
% cam = cameraboard(mypi,'Resolution','320x240');
%
%% Algorithm rev2
for i = 1:1
img = snapshot(cam);
% Preproccessing`````````````````````
imgNew = imresize(img,[227 227]); % resize image
[YPred,probs] = classify(trainedNetwork_1,imgNew);
figure(1);
imshow(imgNew)
label = YPred;
title(string(label) + ", " + num2str(100*max(probs),3) + "%");
end
if string(label) == "Forward"%fwd
for W = 1:1
writeDigitalPin(mypi, 5, 1);
writeDigitalPin(mypi, 6, 0);
writeDigitalPin(mypi, 27, 1);
writeDigitalPin(mypi, 22, 0);
pause(0.5);
end
elseif string(label) == "Left"%pivotleft
for Y = 1:1
writeDigitalPin(mypi, 5, 0);
writeDigitalPin(mypi, 6, 1);
writeDigitalPin(mypi, 27, 1);
writeDigitalPin(mypi, 22, 0);
pause(0.5);
end
elseif string(label) == "Right"%pivotleft
for Z = 1:1
writeDigitalPin(mypi, 5, 0);
writeDigitalPin(mypi, 6, 1);
writeDigitalPin(mypi, 27, 1);
writeDigitalPin(mypi, 22, 0);
pause(0.5);
end
end

採用された回答

Prasanth Sunkara
Prasanth Sunkara 2020 年 11 月 26 日
Hi Nitesh,
Since you want your digital pin(or set of digital pins) to be at a particular state, say HIGH, for 1 sec, you need to make sure to "reset" their state after 1sec, by explicitly setting the pin to LOW (eg:writeDigitalPin(mypi,5,0)). Note that, when you execute any wirteDigitalPin command, the pin state would be remebered(or latched) until you modify the pin state explicitly with the writeDigitalPin command again.
You can use "pause" command to give a delay of 1 sec and do the reset of the states.
Hope this helps!
Thanks,
Prasanth
  1 件のコメント
Walter Roberson
Walter Roberson 2020 年 11 月 26 日
編集済み: Walter Roberson 2020 年 11 月 26 日
Caution: pause is not very accurate. And you need to allow time to communicate with the device.
My test with MATLAB Online showed a mean of 1.0003 seconds for pause(1), which does not take into account time to talk to the device.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB Support Package for Raspberry Pi Hardware についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by