I don't understand what's wrong with this micro python - microcontroller

I've tested this script, and it works on independent servos, but when I add more, it stops working, is this something I'm being an idiot about? Or is there an actual problem?
I tried to assign the different potentiometers to control different servos, but it doesn't work with more than 1.
this is my code:
from machine import Pin, PWM, ADC
import utime
pot1 = ADC(Pin(28))
pot2 = ADC(Pin(27))
pot3 = ADC(Pin(26))
pot4 = ADC(Pin(22))
servo1 = PWM(Pin(1))
servo2 = PWM(Pin(2))
servo3 = PWM(Pin(3))
servo4 = PWM(Pin(4))
servo1.freq(50)
servo2.freq(50)
servo3.freq(50)
servo4.freq(50)
# A .duty_u16 value of c.1350 is zero degrees; 8200 is 180 degrees.
# This needs the calculation here for clarity! :)
while True:
value1 = int(1350 + (pot1.read_u16()/9.57))
servo1.duty_u16(value1)
utime.sleep(0.02)
value2 = int(1350 + (pot2.read_u16()/9.57))
servo2.duty_u16(value2)
utime.sleep(0.02)
value3 = int(1350 + (pot3.read_u16()/9.57))
servo3.duty_u16(value3)
utime.sleep(0.02)
value4 = int(1350 + (pot4.read_u16()/9.57))
servo4.duty_u16(value4)
utime.sleep(0.02)
(and yes, everything is hooked up correctly, that's the first thing I verified)

Related

How to start/stop function based on entry start stop time

I've some LED lights that I want to turn on from entry box 0600 to 1800, or perhaps start another function from 1800 to 0300. how can I make a function to set so the action is ON during the period between the entry values and off otherwise. I've used Time but I'm running into issues. I don't know if I should convert the entry to Int() and compare but find it doesn't resolve the evening On entry. Any thoughts on this one would be appreciated.
LED_time_on_txt = StringVar()
LED_time_on = Entry(control_window, width = 4, bd=5, font= "Arial",14),textvariable=LED_time_on_txt)
LED_time_on.grid(row=3,column=8,sticky=W)
LED_time_off_txt = StringVar()
LED_time_off = Entry(control_window, width = 4, bd=5, font=("Arial",14),textvariable=LED_time_off_txt)
LED_time_off.grid(row=3,column=10,sticky=W)
Figured it out wiht other posts... function looks something like this:
now = datetime.now()
timeontxt = LED_time_on_txt.get()
timeofftxt = LED_time_off_txt.get()
time_on = now.replace(hour=int(timeontxt[:2]),
minute=int(timeontxt[:2]),second=0, microsecond=0)
time_off = now.replace(hour=int(timeofftxt[:2]), minute=int(timeofftxt[:2]), second=0, microsecond=0)
if time_on > time_off:
time_off = time_off + timedelta(days=1)
if time_on<now<time_off:
LED_status.config(text="ON")

crc ip hdr checksum in verilog

I am implementing a task that i can use to obtain checksum from modified ip hdr. This is what i got:
task checksum_calc;
input [159:0] IP_hdr_data;
output [15:0] IP_chksum;
reg [19:0] IP_chksum_temp;
reg [19:0] IP_chksum_temp1;
reg [19:0] IP_chksum_temp2;
begin
IP_chksum_temp = IP_hdr_data[15:0] + IP_hdr_data[31:16] + IP_hdr_data[47:32] + IP_hdr_data[63:48] + IP_hdr_data[79:64] + IP_hdr_data[111:96] + IP_hdr_data[127:112] + IP_hdr_data[143:128] + IP_hdr_data[159:144];
IP_chksum_temp1 = IP_chksum_temp[15:0] + IP_chksum_temp[19:16];
IP_chksum_temp2 = IP_chksum_temp1[15:0] + IP_chksum_temp1[19:16];
IP_chksum = ! IP_chksum_temp2[15:0];
end
endtask
It's that correct? Or it will be some timing problems due to using cominational logic?
Looks like all you are doing is some combination logic calculation. A functions is a better choice. The primary purpose of a function is to return a value that is to be used in an expression.
This is huge combo logic, which in most of the scenario's will cause trouble for timing.
Better to run it through synthesis and timings check to know the exact result.
One suggestion as
IP_chksum_temp1 = IP_chksum_temp[15:0] + IP_chksum_temp[19:16];
can only generate flip the 16th bit. Hence, there is no need of 20 bits in next addition.
IP_chksum_temp2 = IP_chksum_temp1[15:0] + IP_chksum_temp1[19:16];
This can be done :-
reg [16:0] IP_chksum_temp1;
reg [16:0] IP_chksum_temp2;

Torch nn. Current error always is nan

I've wrote the following code:
require 'nn'
require 'cunn'
file = torch.DiskFile('train200.data', 'r')
size = file:readInt()
inputSize = file:readInt()
outputSize = file:readInt()
dataset = {}
function dataset:size() return size end;
for i=1,dataset:size() do
local input = torch.Tensor(inputSize)
for j=1,inputSize do
input[j] = file:readFloat()
end
local output = torch.Tensor(outputSize)
for j=1,outputSize do
output[j] = file:readFloat()
end
dataset[i] = {input:cuda(), output:cuda()}
end
net = nn.Sequential()
hiddenSize = inputSize * 2
net:add(nn.Linear(inputSize, hiddenSize))
net:add(nn.Tanh())
net:add(nn.Linear(hiddenSize, hiddenSize))
net:add(nn.Tanh())
net:add(nn.Linear(hiddenSize, outputSize))
criterion = nn.MSECriterion()
net = net:cuda()
criterion = criterion:cuda()
trainer = nn.StochasticGradient(net, criterion)
trainer.learningRate = 0.02
trainer.maxIteration = 100
trainer:train(dataset)
And it must works good (At least I think so), and it works correct when inputSize = 20. But when inputSize = 200 current error always is nan. At first I've thought that file reading part is incorrect. I've recheck it some times but it is working great. Also I found that sometimes too small or too big learning rate may affect on it. I've tried learning rate from 0.00001 up to 0.8, but still the same result. What I'm doing wrong?
Thanks,
Igor

How to print a complex number without percent sign in Scilab?

I tried this
a = 1+3*%i;
disp("a = "+string(a))
I got a = 1+%i*3 , but what I want is a = 1. + 3.i
So is there any method in Scilab to print a complex number without the percent sign?
Similarly to Matlab, you can format the output string by including the real and imaginary parts separately.
mprintf('%g + %gi\n', real(a) , imag(a))
However, that looks pretty ugly when the imaginary part is negative. I suggest writing a formatting function:
function s = complexstring(a)
if imag(a)>=0 then
s = sprintf('%g+%gi', real(a) , imag(a))
else
s = sprintf('%g%gi', real(a) , imag(a))
end
endfunction
Examples:
disp('a = '+complexstring(1+3*%i))
disp('b = '+complexstring(1-3*%i))
Output:
a = 1+3i
b = 1-3i

How would you index a table that is being initialized?

An example of what I desire:
local X = {["Alpha"] = 5, ["Beta"] = this.Alpha+3}
print(X.Beta) --> error: [string "stdin"]:1: attempt to index global 'this' (a nil value)
is there a way to get this working, or a substitute I can use without too much code bloat(I want it to look presentable, so fenv hacks are out of the picture)
if anyone wants to take a crack at lua, repl.it is a good testing webpage for quick scripts
No there is no way to do this because the table does not yet exist and there is no notion of "self" in Lua (except via syntactic sugar for table methods). You have to do it in two steps:
local X = {["Alpha"] = 5}
X["Beta"] = X.Alpha+3
Note that you only need the square brackets if your key is not a string or if it is a string with characters other than any of [a-z][A-Z][0-9]_.
local X = {Alpha = 5}
X.Beta = X.Alpha+3
Update:
Based on what I saw on your pastebin, you probably should do this slightly differently:
local Alpha = 5
local X = {
Alpha = Alpha,
Beta = Alpha+3,
Gamma = someFunction(Alpha),
Eta = Alpha:method()
}
(obviously Alpha has no method because in the example it is a number but you get the idea, just wanted to show if Alpha were an object).

Resources