PRBS array C code generator (pretty nasty)

Needed an 11 bit PRBS (Pseudo Random Binary Sequence) for use in a project. After some searching online, I came across this sweet wikipedia article on Linear Feedback Shift Registers (LFSRs). This article includes some sample C code to generate a PRBS sequence so I modified this code to generate a sequence that is 2048 bits in length.

// taken from the prbs wikipedia page and modified to make an 11 bit prbs and print to file
// wattnotions april 18th 2015
# include <stdint.h>
# include <stdio.h>
# include <stdbool.h>

bool prbs[2047]; 

int main(void)
{
    uint16_t start_state = 0x7D0;  /* Any nonzero start state will work. */
    uint16_t lfsr = start_state;
    bool bit;
    unsigned period = 0;
	
	FILE *fp;
	fp=fopen("test.txt", "w");
	
	fflush( stdout );
    do
    {
        /* taps: 11 and 9; feedback polynomial: x^11 +x^9 + 1 */
        bit  = ((lfsr >> 0) ^ (lfsr >> 2) ) & 1;
		fprintf(fp, "%d", bit);
        lfsr =  (lfsr >> 1) | (bit << 10);
        ++period;
    } while (lfsr != start_state);
	
	
 
    
}

This 2048 bit sequence in printed as a sequence of 1s and 0s to a .txt file. It looks something like this:
0010010001110110101101011000110001110111101101010010110000110011100111111011110000101001100100011111101……you get the idea

Fairly useless in its current state so some Python magic is required to convert this in to C code.

# opens file containg prbs sequence, parses it into an array of bytes in C code
f = open('Cgen.h', 'w')  # open the file that will hold the generated C code


with open ("test.txt", "r") as myfile:   # open the file containing the prbs 1s and 0s
    data=myfile.read().replace('\n', '')
	

num = 0

f.write('int prbs[] = {\n')
for i in range( ((len(data))//8)  ):
	
	f.write("'0b" + data[i:i+8] +"'")
	if( i != (((len(data))//8) -1) ):
		f.write(", ")
	num = num + 1
	if (num == 7):        # print a newline after every 7th element to keep things pretty
		f.write("\n")    
		num = 0
		
f.write(");")
		

The output from this python script is saved in a file called Cgen.h . It looks like this:
int prbs[] = {
‘0b00100100′, ‘0b01001000′, ‘0b10010001′, ‘0b00100011′, ‘0b01000111′, ‘0b10001110′, ‘0b00011101′,
‘0b00111011′, ‘0b01110110′, ‘0b11101101′, ‘0b11011010′, ‘0b10110101′, ‘0b01101011′, ‘0b11010110′,
‘0b10101101′, ‘0b01011010′, ‘0b10110101′, ‘0b01101011′, ‘0b11010110′, ‘0b10101100′, ‘0b01011000′,
‘0b10110001′, ‘0b01100011′, ‘0b11000110′, ‘0b10001100′, ‘0b00011000′, ‘0b00110001′, ‘0b01100011′,
‘0b11000111′, ‘0b10001110′, ‘0b00011101′, ‘0b00111011′, ‘0b01110111′, ‘0b11101111′, ‘0b11011110′,

….and so on, then near the end it looks like this:

‘0b10011001′, ‘0b00110010′, ‘0b01100101′, ‘0b11001010′, ‘0b10010101′, ‘0b00101011′, ‘0b01010111′,
‘0b10101111′, ‘0b01011111′, ‘0b10111111′, ‘0b01111110′, ‘0b11111100′, ‘0b11111000′, ‘0b11110000′,
‘0b11100001′, ‘0b11000010′, ‘0b10000100′);

So its just an array definition that can be conveniently hidden away in a header file. There’s a github repo called prbs_C_gen with all of the source files and example output files if you fancy taking a peek.

Low power magnetic hold and release mechanism

How it works:

test

Explanation of the idea:

With the inductor and magnet combined, ferrous metal objects can be held without any power indefinitely. To release the metal object the magnetic field holding it up needs to be temporarily cancelled out. This happens by pulsing current through the inductor which generates a magnetic field separate to that of the magnet. These two magnetic fields are of the opposite polarity meaning the inductors field cancels out the magnets field. The inductor only has to be powered long enough for the item to drop which I haven’t measured but its probably somewhere in the hundreds of milliseconds lets say around 100 milliseconds at a guess. This means you can hold an object for potentially a very long time (months, years maybe I dunno!) and expend a relatively small amount of power releasing it compared to if you’d used an electromagnet to hold the thing up which required a constant power source.

Or you could disregard the above explanation and look to this meme for guidance:
trdsgfd

Automated Plant Waterer

Here is a circuit diagram of the circuit shown in the video:
rect4159-09-4

This is the C program that runs on the dsPIC:

// Program to control automated plant waterer
//wattnotions 2/16/15
#include <xc.h>
#include <libpic30.h>
#include<stdio.h>
//#include <stdlib.h>
 
// Configuration settings
_FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=16x7.5MHz, i.e. 30 MIPS
_FWDT(WDT_OFF);                  // Watchdog timer off
_FBORPOR(MCLR_DIS);              // Disable reset pin

//Functions
void setup(void);
void servo(int pw); // controls the servo angle accepts pulse width in us eg 1500 = 1.5ms pulse
void pump (int n, unsigned long int ms); // flips pins high and low to change direction of actuator motor and pump the handle
// n = number of pumps ; ms = time delay in between pumping motion

//variables
unsigned int pw_cyc;
long int  total_p;
unsigned long int delay_cyc;
int i;


int main(void)
{
	
	setup();
	
	while(1)
	{
		
		for(i=0 ; i<50 ; i++) servo(1800); // off
		pump(10, 300);
		for(i=0 ; i<200 ; i++) servo(1100); //on
	}
	
	

	
	
}

void setup(void)
{
    //configure pins
	TRISD = 0b00000000;
 
}

void servo(int pw)
{
	
	pw_cyc = pw*30;
	total_p = 600000 - pw_cyc;
	_LATD2 = 1;
	__delay32(pw_cyc);
	_LATD2 = 0;
	__delay32(total_p);
	
}

void pump (int n, unsigned long int ms)
{
	delay_cyc = ms*30000;
	for(i=0; i<n; i++)
	{	
		_LATD1 = 1;
		_LATD3 = 0;
		__delay32(delay_cyc);
		
		_LATD1 = 0;
		_LATD3 = 0;
		__delay32(delay_cyc);
		
		
		_LATD3 = 1;
		_LATD1 = 0;
		__delay32(delay_cyc);
		
		_LATD1 = 0;
		_LATD3 = 0;
		__delay32(delay_cyc);
	}
	
}

Testing egg boiled-ness using soundwaves : Experimental results

So I may have gotten a bit carried away before with the whole egg thing. Tried building a working prototype without first doing some experimentation to make sure it is possible to tell how boiled an egg is by using sound waves. I’ve gone back to the basics and done some experimentation. The results are not conclusive really so some further eggsperimentation will be required. I posted about this on reddit and received some great input from several users. One person suggested measuring the time delay between the input and output signal and observing how this changes between boiled and unboiled eggs. In this experiment I measured both amplitude and time lag the results are recorded below.

The experiment:

I bought a six pack of eggs and labelled them individually as seen in this photo:

Individually marked eggs

Individually marked eggs

I made a test rig by mounting two piezos on the end of pens. There is also a place for the egg to sit.
IMG_20150121_154330IMG_20150121_154359

A sine wave with amplitude 9.44V and frequency of 4.46KHz was input into one of the transducers. For this experiment I placed eggs 1-6 in the rig and measured two variables : amplitude ( at receiver piezo ) and the time taken for the sound wave to travel from sender to receiver. This time difference variable will be referred to as delta t in the results. I did this procedure three times for the un-boiled eggs and three times for the boiled eggs. The eggs were boiled for different amounts of time. All of the eggs were placed in one pot, the water was brought to boiling temperature and every two minutes an egg was taken out. So egg 1 was in for 2 mins and egg six was in for a total of 12 mins. Here are some photos of the egg boiling setup:
IMG_20150121_164311IMG_20150121_164505

These are the results from before and after boiling the eggs. The reason I did each test three times was to reduce any errors caused by poor contact between an egg and the transducers and other errors of that nature. Delta T refers to the time lag between the input signal and the output (received) signal it is measured in microseconds. mV refers to the amplitude of the output (received) signal.
g38d34

Taking the difference between each set of numbers gives the following set of data. So to be clear, mV_var refers to the change or variation in output between the unboiled egg and the boiled egg. t_var refers to the change in time lag between the unboiled egg and the boiled egg. T_var is in microseconds still.
g5392

Taking the average of these three tests gives the following results:
g5712

image4369image4358

From the graph on the right, the results looked quite promising up until minute 12 which was egg 6. Egg 6 was the only egg that cracked during boiling. The egg was mostly fine but the reason for the rather low output may be due to the fact that the egg surface had cracks in it. This may be a good reason to repeat this experiment to make sure. Another thing that I’ve noticed is the extremely low amplitude of the received signal, it is normally in the order of tens of milli-volts at best. The piezo transducers used for this experiment are intended for use as speakers presumably for toys and things of that nature. This means that they have a frequency response of only around 1Khz to 7Khz before the sound output drops away to nothing. It may be the case that a higher frequency would pass through an egg much more effectively giving a greater output which would allow for more concrete results. The graph of time lag versus boil time is all over the place, I really don’t know that to make from it. Another thing is that there may be something I’m missing with the data. As in, I’ve graphed change in amplitude and time lag against boil time but this may be the wrong way to think about it. If you have any ideas about this, let me know!