Ciao ragazzi, sono prossima alla laurea triennale e devo svolgere il tirocinio presso un'azienda, ma sono in estrema difficoltà.
Mi hanno dato come compito quello di tradurre un programma da C a Python ma ho problemi con le memory map, non riesco ad usarle in Python allo stesso modo di come vengono usate in C (neanche con la libreria numpy poichè mi da errore sul filename). Inoltre devo implementare una socket lato server ma il mutiprocessing mi lancia errori. Vi allego entrambi i codici così da poter essere più chiara, spero riusciate ad aiutarmi nella traduzione.
[CODE = C]
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdlib.h>
#include <time.h>
#define XADC_reg 0x43C00000
#define PID_reg 0x43C10000
int main(int argc, char **argv)
{
int fd;
uint32_t *pid;
uint32_t *xadc;
char *name = "/dev/mem";
int active = 1;
uint32_t data;
uint32_t pos;
uint32_t position;
if((fd = open(name, O_RDWR)) < 0)
{
perror("open");
return 1;
}
pid = mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ|PROT_WRITE, MAP_SHARED, fd, PID_reg);
xadc = mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ|PROT_WRITE, MAP_SHARED, fd, XADC_reg);
*((uint32_t *)(xadc + 0)) = 0x0000000A; //reset
*((uint32_t *)(pid + 0)) = 0x00000000; //punto da raggiungere
*((uint32_t *)(pid + 1)) = 0x00000064; //azione proporzionale
*((uint32_t *)(pid + 2)) = 0x00000000; //azione integrativa
*((uint32_t *)(pid + 3)) = 0x00000000; //azione derivativa
*((uint32_t *)(pid + 5)) = 0x00000190; //azione derivativa
while(active){
printf("Inserisci posizione [0-10]:\n");
scanf ("%d", &data);
data = data*1000*3;
if(data < 0x00010000)
{
*((uint32_t *)(pid + 0)) = data;
position = *((uint32_t *)(xadc + 0x90));
printf("Valore letto dall'XADC: %d\n", (uint32_t)position);
position = *((uint32_t *)(pid + 0)); //errore calcolato dal pid
printf("Posizione da raggiungere inviata al PID: %d\n", (uint32_t)position);
position = *((uint32_t *)(pid + 4)); //valore ricevuto dal pid
printf("Misura ricevuta dal pid: %d\n", (uint32_t)position);
position = *((uint32_t *)(pid + 6)); //errore calcolato dal pid
printf("Output del PID: %d\n", (uint32_t)position);
position = *((uint32_t *)(pid + 1)); //errore calcolato dal pid
printf("KP del PID: %d\n", (uint32_t)position);
}
if(data == 0x00000000)
active=0;
usleep(100);
}
munmap(pid, sysconf(_SC_PAGESIZE));
munmap(xadc, sysconf(_SC_PAGESIZE));
return 0;
}
[/CODE]
[CODE = Python]
#!/usr/bin/python
import mmap
import time
import socket
import sys
import multiprocessing as Process
import numpy as np
def main():
active = 1
pid_reg = 0x43C10000
xadc_reg = 0x43C00000
pid = np.memmap('/dev/mem', dtype='uint32', mode='r+', offset=pid_reg)
xadc = np.memmap('/dev/mem', dtype='uint32', mode='r+', offset=xadc_reg)
print('pid:', pid, '\nxadc', xadc)
xadc[0] = 0x0000000A #reset
pid[0] = 0x00000000 #punto da raggiungere
pid[1] = 0x00000064 #azione proporzionale
pid[2] = 0x00000000 #azione integrativa
pid[3] = 0x00000000 #azione derivativa
pid[5] = 0x00000190 #azione derivativa
print('pid:', pid, '\nxadc', xadc)
while(active):
data = int(input("Inserisci posizione [0/10]: "))
data = data*1000*3
if(data < 0x00010000):
pid[0] = data
position = xadc[0x90]
print("Valore letto dall'XADC: {}\n".format(position))
position = pid[0]
print("Posizione da raggiungere inviata al PID: {}\n".format(position))
position = pid[4]
print("Misura ricevuta dal pid: {}\n".format(position))
position = pid[6]
print("Output del PID: {}\n".format(position))
position = pid[1]
print("KP del PID: {}\n".format(position))
if(data == 0x00000000):
active = 0;
time.sleep(1)
def socket1():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('localhost', 555)
sock.bind(server_address)
sock.listen(1) #server in ascolto
while True:
sys.stderr,'waiting for a connection'
connection, client_address = sock.accept()
try:
sys.stderr, 'connection from:', client_address
while True:
data = connection.recv(16)
sys.stderr,'receiver %s' %data
if data:
sys.stderr,'sending data back to the client'
connection.sendall(data)
else:
sys.stderr,'no more data from', client_address
break
finally:
connection.close()
if __name__ == '__main__':
p = Process(target = main, args = None)
p.start()
q = Process(target = socket1, args = None)
q.start()
[/CODE]
Mi hanno dato come compito quello di tradurre un programma da C a Python ma ho problemi con le memory map, non riesco ad usarle in Python allo stesso modo di come vengono usate in C (neanche con la libreria numpy poichè mi da errore sul filename). Inoltre devo implementare una socket lato server ma il mutiprocessing mi lancia errori. Vi allego entrambi i codici così da poter essere più chiara, spero riusciate ad aiutarmi nella traduzione.
[CODE = C]
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdlib.h>
#include <time.h>
#define XADC_reg 0x43C00000
#define PID_reg 0x43C10000
int main(int argc, char **argv)
{
int fd;
uint32_t *pid;
uint32_t *xadc;
char *name = "/dev/mem";
int active = 1;
uint32_t data;
uint32_t pos;
uint32_t position;
if((fd = open(name, O_RDWR)) < 0)
{
perror("open");
return 1;
}
pid = mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ|PROT_WRITE, MAP_SHARED, fd, PID_reg);
xadc = mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ|PROT_WRITE, MAP_SHARED, fd, XADC_reg);
*((uint32_t *)(xadc + 0)) = 0x0000000A; //reset
*((uint32_t *)(pid + 0)) = 0x00000000; //punto da raggiungere
*((uint32_t *)(pid + 1)) = 0x00000064; //azione proporzionale
*((uint32_t *)(pid + 2)) = 0x00000000; //azione integrativa
*((uint32_t *)(pid + 3)) = 0x00000000; //azione derivativa
*((uint32_t *)(pid + 5)) = 0x00000190; //azione derivativa
while(active){
printf("Inserisci posizione [0-10]:\n");
scanf ("%d", &data);
data = data*1000*3;
if(data < 0x00010000)
{
*((uint32_t *)(pid + 0)) = data;
position = *((uint32_t *)(xadc + 0x90));
printf("Valore letto dall'XADC: %d\n", (uint32_t)position);
position = *((uint32_t *)(pid + 0)); //errore calcolato dal pid
printf("Posizione da raggiungere inviata al PID: %d\n", (uint32_t)position);
position = *((uint32_t *)(pid + 4)); //valore ricevuto dal pid
printf("Misura ricevuta dal pid: %d\n", (uint32_t)position);
position = *((uint32_t *)(pid + 6)); //errore calcolato dal pid
printf("Output del PID: %d\n", (uint32_t)position);
position = *((uint32_t *)(pid + 1)); //errore calcolato dal pid
printf("KP del PID: %d\n", (uint32_t)position);
}
if(data == 0x00000000)
active=0;
usleep(100);
}
munmap(pid, sysconf(_SC_PAGESIZE));
munmap(xadc, sysconf(_SC_PAGESIZE));
return 0;
}
[/CODE]
[CODE = Python]
#!/usr/bin/python
import mmap
import time
import socket
import sys
import multiprocessing as Process
import numpy as np
def main():
active = 1
pid_reg = 0x43C10000
xadc_reg = 0x43C00000
pid = np.memmap('/dev/mem', dtype='uint32', mode='r+', offset=pid_reg)
xadc = np.memmap('/dev/mem', dtype='uint32', mode='r+', offset=xadc_reg)
print('pid:', pid, '\nxadc', xadc)
xadc[0] = 0x0000000A #reset
pid[0] = 0x00000000 #punto da raggiungere
pid[1] = 0x00000064 #azione proporzionale
pid[2] = 0x00000000 #azione integrativa
pid[3] = 0x00000000 #azione derivativa
pid[5] = 0x00000190 #azione derivativa
print('pid:', pid, '\nxadc', xadc)
while(active):
data = int(input("Inserisci posizione [0/10]: "))
data = data*1000*3
if(data < 0x00010000):
pid[0] = data
position = xadc[0x90]
print("Valore letto dall'XADC: {}\n".format(position))
position = pid[0]
print("Posizione da raggiungere inviata al PID: {}\n".format(position))
position = pid[4]
print("Misura ricevuta dal pid: {}\n".format(position))
position = pid[6]
print("Output del PID: {}\n".format(position))
position = pid[1]
print("KP del PID: {}\n".format(position))
if(data == 0x00000000):
active = 0;
time.sleep(1)
def socket1():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('localhost', 555)
sock.bind(server_address)
sock.listen(1) #server in ascolto
while True:
sys.stderr,'waiting for a connection'
connection, client_address = sock.accept()
try:
sys.stderr, 'connection from:', client_address
while True:
data = connection.recv(16)
sys.stderr,'receiver %s' %data
if data:
sys.stderr,'sending data back to the client'
connection.sendall(data)
else:
sys.stderr,'no more data from', client_address
break
finally:
connection.close()
if __name__ == '__main__':
p = Process(target = main, args = None)
p.start()
q = Process(target = socket1, args = None)
q.start()
[/CODE]