Skip to content
Snippets Groups Projects
Commit 75a907f6 authored by Sam Calisch's avatar Sam Calisch
Browse files

working on getting framing to work consistently

parent 3a65606a
No related branches found
No related tags found
No related merge requests found
Pipeline #
......@@ -10,46 +10,65 @@ import matplotlib.animation as animation
import bitstring
n_samples = 20
Nx = 20
Ny = 20
halls = np.zeros((Nx,Ny,5))
def roll_left(lst):
return lst[1:] + [lst[0]]
def read_as5013_12bit(bytes):
return (bitstring.Bits(bytes=bytes, length=16)<<4).unpack('uint:16')[0]
#signed 12 bit val comes in two bytes
#return struct.unpack("<h",bytearray([ (high<<4) , low<<4 ]))[0]
def read_int16(bytes,signed=False):
try:
n = struct.unpack("<h",bytearray(bytes))[0] if signed else struct.unpack("<H",bytearray(bytes))[0]
return n
except:
print "not properly formatted bytes: ",list(bytes)
return None
def read(ser=None):
if ser is None:
print "No serial connection!"
else:
c = ser.read(n_samples)
data = [read_as5013_12bit(c[x:x+2]) for x in xrange(0,n_samples,2)]
#wait for framing signal at tail of packet
framing = range(8) #we expect the sequence 012...8
c = [ser.read(1) for i in range(n_samples+4)]
c += [ser.read(1) for i in framing]
while( map(ord,c[n_samples+4:]) != framing ):
c = roll_left(c)
c[-1] = ser.read(1)
print c
#c = ser.read(n_samples+12) #samples (20) + xi (2) + yi (2) + framing (8)
try:
data = [read_as5013_12bit(c[x:x+2]) for x in xrange(0,n_samples,2)]
data += [read_int16(c[x:x+2]) for x in xrange(n_samples,n_samples+4,2)]
except(bitstring.CreationError):
print c
return data
def main():
try:
ser = serial.Serial(port='/dev/tty.usbserial-FT9L3KWR',baudrate=1000000,timeout=1.)
ser.isOpen()
print "Established serial connection."
#make sure timeout is longer than any period between data.
ser = serial.Serial(port='/dev/tty.usbserial-FT9L3KWR',baudrate=1000000,timeout=5.)
print "Established serial connection"
ser.flush()
while True:
try:
data = np.asarray(read(ser=ser))
print data[10], data[11], data[1:10:2]-data[:10:2]
if (data[10]<=Nx) and (data[11]<=Ny):
halls[data[10]-1,data[11]-1] = data[1:10:2] - data[:10:2]
except(KeyboardInterrupt):
ser.close()
break
except(OSError):
#no serial port
print "Couldn't find the serial port, entering debug mode."
ser = None
ser.flush()
while True:
#connection, address = serversocket.accept()
try:
data = np.asarray(read(ser=ser))
print data[1::2] - data[::2]
except(KeyboardInterrupt):
break
#if data:
# clientsocket.send(data)
print "Couldn't find the serial port."
ser = None
print "quitting"
ser.close()
if __name__ == '__main__':
main()
......@@ -14,16 +14,16 @@
#define sda_pin 1 //XL2
#define rst_pin 3 //A1
static uint8_t txdata[n_tx_bytes] = {0};
static uint8_t rxdata[n_rx_bytes] = {0};
//uint16_t step_period = 30000; //microseconds
static uint8_t rxdata[n_rx_bytes+12] = {0}; //add four bytes for xi, yi over uarte, 8 bytes for framing
//uarte
const uint8_t pin_rx = 8;
const uint8_t pin_tx = 6;
uint32_t last_tx_time = 0;
uint32_t tx_period = 1000; //ms
uint32_t tx_period = 200; //ms
int16_t step_period = 4;
//
//UARTE
......@@ -37,7 +37,7 @@ void uarte_setup(){
NRF_UARTE0->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud1M;
NRF_UARTE0->ENABLE = (UARTE_ENABLE_ENABLE_Enabled << UARTE_ENABLE_ENABLE_Pos) & UARTE_ENABLE_ENABLE_Msk;
NRF_UARTE0->TXD.MAXCNT = n_rx_bytes;
NRF_UARTE0->TXD.MAXCNT = n_rx_bytes+12;
}
......@@ -120,38 +120,93 @@ void as5013_read(){
twi_rx();
}
//
//main
//
void setup() {
//Serial.begin(115200);
uarte_setup();
twi_setup();
as5013_setup();
radio_setup();
void configure_motor(){
//set step speed
radio_buffer[0] = 2; //set step speed
radio_buffer[1] = 10000; //30ms step period
radio_buffer[1] = step_period*1000; //10ms step period
radio_send_redundant();
//set full steps
radio_buffer[0] = 3; //set step speed
radio_buffer[1] = 0; //M1,M0 = 0,1
radio_buffer[1] = 0; //M1,M0 = 0,0
radio_send_redundant();
//set current limit
radio_buffer[0] = 4; //set step speed
radio_buffer[1] = 100; //M1,M0 = 0,1
radio_send_redundant();
}
//
//main
//
void setup() {
//Serial.begin(115200);
uarte_setup();
twi_setup();
as5013_setup();
radio_setup();
NRF_RADIO->TXADDRESS = 0x00UL; // Set device address 0: X axis
configure_motor();
NRF_RADIO->TXADDRESS = 0x01UL; // Set device address 1: Y axis
configure_motor();
int16_t xi=0; //counter
int16_t yi=0; //counter
int16_t Nx = 20; //max number of x moves
int16_t Ny = 20; //max number of y moves
int16_t moves_per_step = 8; //8 steps = 10 microns
//insert framing.
rxdata[n_rx_bytes+4] = (uint8_t)0;
rxdata[n_rx_bytes+5] = (uint8_t)1;
rxdata[n_rx_bytes+6] = (uint8_t)2;
rxdata[n_rx_bytes+7] = (uint8_t)3;
rxdata[n_rx_bytes+8] = (uint8_t)4;
rxdata[n_rx_bytes+9] = (uint8_t)5;
rxdata[n_rx_bytes+10] = (uint8_t)6;
rxdata[n_rx_bytes+11] = (uint8_t)7;
while(1){
if (millis() - last_tx_time > tx_period){
as5013_read();
NRF_RADIO->TXADDRESS = 0x00UL; // Set device address 0: X axis
radio_buffer[0] = 1; //send a move command
radio_buffer[1] = moves_per_step; //move 10 steps forward
radio_send_redundant(); //send command
delay(step_period*moves_per_step+1);
xi++;
as5013_read();
rxdata[n_rx_bytes] = (uint8_t)(xi);
rxdata[n_rx_bytes+1] = (uint8_t)(xi>>8);
rxdata[n_rx_bytes+2] = (uint8_t)(yi);
rxdata[n_rx_bytes+3] = (uint8_t)(yi>>8);
NRF_UARTE0->TXD.PTR = (uint32_t)(&rxdata); //reset pointer to start of buffer
NRF_UARTE0->TASKS_STARTTX = 1; //trigger start task to send data to host
delay(50); //for demonstration, not really necessary.
if (xi==Nx){
xi=0;
NRF_RADIO->TXADDRESS = 0x00UL; // Set device address 0: X axis
radio_buffer[0] = 1; //send a move command
radio_buffer[1] = -moves_per_step*Nx; //move back to start
radio_send_redundant(); //send command
delay(step_period*Nx*moves_per_step+1);
NRF_RADIO->TXADDRESS = 0x01UL; // Set device address 1: Y axis
radio_buffer[0] = 1; //send a move command
radio_buffer[1] = moves_per_step; //move back to start
radio_send_redundant(); //send command
delay(step_period*moves_per_step+1);
yi++;
}
if (yi==Ny){
delay(10);
yi=0;
NRF_RADIO->TXADDRESS = 0x01UL; // Set device address 1: Y axis
radio_buffer[0] = 1; //send a move command
radio_buffer[1] = 10; //move 10 steps forward
radio_send_redundant(); //send command
NRF_UARTE0->TXD.PTR = (uint32_t)(&rxdata); //reset pointer to start of buffer
NRF_UARTE0->TASKS_STARTTX = 1; //trigger start task
last_tx_time = millis();
radio_buffer[1] = -moves_per_step*Ny; //move back to start
radio_send_redundant(); //send command
delay(step_period*Ny*moves_per_step+1);
}
}
}
......
......@@ -21,8 +21,10 @@ void radio_setup(){
NRF_RADIO->PREFIX0 = ((uint32_t)0xC0 << 0); // Prefix byte of address 0
NRF_RADIO->BASE0 = 0x01234567UL; // Base address for prefix 0
NRF_RADIO->BASE1 = 0x02345678UL; // Base address for prefix 0
NRF_RADIO->TXADDRESS = 0x00UL; // Set device address 0 to use when transmitting
NRF_RADIO->RXADDRESSES = 0x01UL; // Enable device address 0 to use to select which addresses to receive
//NRF_RADIO->RXADDRESSES = 0x02UL; // Enable device address 1 to use to select which addresses to receive
// Packet configuration
NRF_RADIO->PCNF0 = (0 << RADIO_PCNF0_S1LEN_Pos) | (0 << RADIO_PCNF0_S0LEN_Pos) | (0 << RADIO_PCNF0_LFLEN_Pos);
......
......@@ -50,6 +50,7 @@ void parse_command(){
} else {
NRF_GPIO->OUTCLR = (1<<pin_direction); //set direction backwards
}
radio_buffer[1] = radio_buffer[1] > 0 ? radio_buffer[1] : -radio_buffer[1];
for(i=0; i<radio_buffer[1]; i++){
NRF_GPIO->OUTSET = (1<<pin_step);
delayMicroseconds(step_period);
......
......@@ -21,8 +21,10 @@ void radio_setup(){
NRF_RADIO->PREFIX0 = ((uint32_t)0xC0 << 0); // Prefix byte of address 0
NRF_RADIO->BASE0 = 0x01234567UL; // Base address for prefix 0
NRF_RADIO->BASE1 = 0x02345678UL; // Base address for prefix 0
NRF_RADIO->TXADDRESS = 0x00UL; // Set device address 0 to use when transmitting
NRF_RADIO->RXADDRESSES = 0x01UL; // Enable device address 0 to use to select which addresses to receive
//NRF_RADIO->RXADDRESSES = 0x01UL; // Enable device address 0 to use to select which addresses to receive
NRF_RADIO->RXADDRESSES = 0x02UL; // Enable device address 1 to use to select which addresses to receive
// Packet configuration
NRF_RADIO->PCNF0 = (0 << RADIO_PCNF0_S1LEN_Pos) | (0 << RADIO_PCNF0_S0LEN_Pos) | (0 << RADIO_PCNF0_LFLEN_Pos);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment