Microbit V1 servo motor's problem, it always does the entire return - micropython

the thing is that i'm working with Python in Micro Python for Microbit Editor.
I'm trying to set the angle of a servomotor, but the thing is that it is giving the entire return.
pin0.set_analog_period(20)
while True:
pin0.write_analog(50)
sleep(3000)
pin0.write_analog(75)
The same thing is happening when I try to code it with blocks

Related

How to set Pin touch mode to capacitive on micro:bit v2 in MicroPython

If I use the makecode editor to create the code, it generates:
pins.touch_set_mode(TouchTarget.P0, TouchTargetMode.CAPACITIVE)
However, if I try and run this, it can't find 'pins'.
Note: I then investigated further and found out how to do this before submitting my question...
Capacitive mode can be set for each pin, e.g.
from microbit import *
...
pin0.set_touch_mode(pin0.CAPACITIVE)
...
if pin0.is_touched():
...
The last line checks whether the pin is touched - usually in a loop.
Hope this saves other people some time...

How do I get immediate response between motors and sensors?

I am new to pybricks and have found very little documentation to help answer my own query. I have written what I thought would be a simple program to spin my robot on the spot until the UltrasonicSensor sees something. It will then push forwards. If it is pushed backwards and sees a black line, it should try and swing out of the way.
The following code "works", but it's response to the Ultrasonic and Light sensors is significantly delayed:
#!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import Motor, ColorSensor, UltrasonicSensor
from pybricks.parameters import Port
from pybricks.tools import wait
ev3 = EV3Brick()
eyes = UltrasonicSensor(Port.S2)
left_motor = Motor(Port.B)
right_motor = Motor(Port.A)
right_light = ColorSensor(Port.S1)
left_light = ColorSensor(Port.S4)
while True:
if right_light.reflection() < 50:
ev3.speaker.say('black')
left_motor.run(500)
right_motor.run(-100)
wait(2000)
left_motor.run(500)
right_motor.run(500)
wait(1000)
if eyes.distance() > 200:
left_motor.run(500)
right_motor.run(-500)
else:
left_motor.run(-500)
right_motor.run(-500)
I can see in the (limited) documentation that you can apparently change motor settings but I can't find direction on how to do this (or even if it would be useful). Any help would be appreciated.
ev3.speaker.say(text) synthesizes speech as it goes. This is fun, but it is very slow. This is especially noticeable in a control loop like yours.
I'd recommend using ev3.speaker.beep() instead. You could even select the frequency based on the reflection value so you can "hear" what the sensor "sees".
So the problem was that I used the "run" command to move the motors. Run appears to have an acceleration and deceleration component.
I used "dc" instead of "run" and the motors instantly respond to the sensor data now.

Problems with Micropython command time_pulse_us on Microbit

I would like to ask some help from you. I'm trying to connect a distance sensor to my microbit but when I use the command "time_pulse_us" it always gives -2 or -1. I read the documentation, I understand the meaning of those numbers but I think there's a problem with the command or probably I'm using it the wrong way.
In that regard, I wrote a simple snippet to test the command. Could you tell me what's wrong with it?
from microbit import * //to import microbit modules
from machine import * //to import the time_pulse_us command
while True:
pin1.write_digital(0)
time = time_pulse_us(pin2, 1) //to begin the timing
pin1.write_digital(1) //this pin is connected to an LED
sleep(1000)
value = pin2.read_digital() //gives 1, as this pin is reading the voltage from the led
pin1.write_digital(0) //this will make the time_pulse command to end timing
display.scroll(time) //it should display the duration of the pulse.
//Displays -2 instead.
display.scroll(value) //gives 1, as expected
Why is this not working?
time_pulse_us() runs sequentially, not in the background, so at the call it will wait 1 second for the pin to reach 1, which it will not do, hence time will be set to -2, before the program goes on to the next command write_digital(1).

Running two or more motors at the same time with PiFace Relay Plus and Motor Extra

I have a question. I have a Raspberry Pi connected to PiFace Relay Plus and PiFace Motor Extra. Is it possible to run two or more motors at the same time?
I have no problem with running one motor:
import pifacerelayplus
import time
pfr = pifacerelayplus.PiFaceRelayPlus(pifacerelayplus.MOTOR_DC)
pfr.motors[0].forward()
time.sleep(5)
pfr.motors[0].coast()
I also managed to run one motor and than next one:
import pifacerelayplus
import time
pfr = pifacerelayplus.PiFaceRelayPlus(pifacerelayplus.MOTOR_DC)
pfr.motors[0].forward()
time.sleep(5)
pfr.motors[0].coast()
time.sleep(2)
pfr.motors[1].forward()
time.sleep(5)
time.motors[1].coast()
But I can't find out how to make both motors run at the same time. I tried this code, but that only runs the first motor, than the program ends and the first motor still runs and never stops. The second motor never starts to run.
import pifacerelayplus
import time
pfr = pifacerelayplus.PiFaceRelayPlus(pifacerelayplus.MOTOR_DC)
pfr.motors[0].forward()
pfr.motors[1].forward()
time.sleep(5)
pfr.motors[0].coast()
pfr.motors[1].coast()
I even tried to create another variable "prf2" for second motor, but it didn't help neither. I'm glad for any help.
Are you getting any error when running the program? Looking at the source code of pifacerelayplus, it is INTENDED to fail if you give two motor commands within 0.15 seconds, on the grounds that the startup surge of two motors at once is likely to be more than your power supply can handle. A couple of short sleep()s should avoid this issue.

Blank output for image subtraction when using raspicam library

I am using raspberry pi with raspicam to run a project. I have downloaded the raspicam library from http://sourceforge.net/projects/raspicam/files/?
I am trying to run a code for image subtraction but not getting results. Here is my code
raspicam::RaspiCam_Cv Camera;
Camera.set(cv::CAP_PROP_FRAME, CV_8UC1);
if(!Camera.open())
{
std::cerr<<"cannot open camera"<<std::endl;
}
Camera.grab();
Camera.retrieve(frame1);
Camera.grab();
Camera.retrieve(frame2);
Camera.grab();
Camera.retrieve(frame3);
while (True)
{
frame1=frame2;
frame2=frame3;
Camera.grab();
Camera.retrieve(frame3);
absdiff(frame2,frame1,d1);
imshow("result1",d1);
absdiff(frame2,frame3,d2);
imshow("result2",d2);
}
when I run this code I get blank frames of result1 and result2 as output. This is just a part of my code ignore if i have missed something.
Well, inside your loop
frame1=frame2;
...
absdiff(frame2,frame1,d1);
that'll be sort of identically zero...
Also, have you considered the timing here? You're grabbing images very close together in the time-domain, so naturally they'll be mostly identical (bar noise and fast motion) so the difference will be near-zero.
Cheers,