Forum Discussion
Altera_Forum
Honored Contributor
12 years agoHehe, the forum admins took more than 10 days to approve my post since I'm a new user here. I actually managed to get the whole thing working. I first had a very similar algorithm to the code I posted initially and had the counter reset itself when ((signalon>50000)&&(counter>400000)) and also count the ratio at that point. It worked fairly well but the number varied a bit couple times a second and this wasn't accetable for me. I measured the period of the signal with an oscilloscope and it was 18ms. I tried changing counter?899900 but this didn't work either.
Then I just had a great idea in shower which made a ton of sense and the code simpler as well. It works flawlessly with virtually no deviation from number 0 to 100 when the controller stick isn't moving. Now I'm facing yet another problem. I have 2 choices for the gyros. I can either use one that I've used in my RC airplanes that give me 50hz update rate with pwm signals or I can use a mpu 6050 (http://www.ebay.com/itm/new-mpu-6050-module-3-axis-gyroscope-accelerometer-module-for-arduino-mpu-6050-/170881535422) that I also have now. The mpu 6050 would give me really good refresh rates but requires i2c thingy which I really don't know how to do. I'm done quite a bit of research on how to implement the i2c signal but couldn't find any simple explanation. The minimum refresh rates that the ESCs have to have to keep the quad in the air is about 200hz. 300hz gives stable flight and 400-490hz is butter smooth. So I imagine that 50hz refresh rate from the gyro thats outputting pwm signals won't get the quad up in the air. However, as long as I'm able to control the motors of the quad from RC controller and have gyros do some work, my prof will give me close to full credit on the project. Here is the whole code for the PWM decoder that works great.module gyro(clk, control, pwm,pwmsignal,ratio);
input clk;
input pwmsignal;
input control;
output pwm;
output ratio;
reg pwm;
reg counter;
reg signalon; //# of times a sec pwm signal is at 1
reg signaloff; //# of time a sec pwm signal is at 0
reg ratio;
reg tempcounter;
reg tempratio;
always @ (posedge clk)
begin
if (counter<32'd50) //whenever the counter is reset, signalon is also reset
begin
signalon <= 32'd0;
signaloff <= 32'd0;
end
else
begin
end
tempcounter <= tempcounter+1;
counter <= counter+1;
if (pwmsignal == 1'b1)
begin
signalon <= signalon+1;
end
else
begin
if (signalon>50000-(control*40))
begin
tempratio <= ((signalon - (32'd50000-(control*20)))/(32'd500));
signalon <= 1'b0;
counter <= 1'b0;
end
else
begin
end
end
if (tempcounter >= (32'd50000))
begin
tempcounter <= 32'd0;
ratio <= tempratio;
end
else
begin
end
end
endmodule