Hi,
Palpitations or throbbing headaches while passing urine are an indication that your blood pressure is rising or is high during urination. This can be due to a certain amount of kidney function impairment or due to a neoplasm on the adrenals or surrounding structures. This is called a pheochromocytoma and needs early investigations to confirm diagnosis and initiate appropriate treatment.
Other possibilities include the use of any drugs or cough syrups containing pseudoephedrine.
As you have noted that your urine is foamy, it indicates that there is proteinuria. This can be confirmed on urinalysis.
You should meet your doctor and have your blood pressure checked and start medications based on the results and findings.
Do keep us posted on any additional doubts in this regard.