Dear poster,
Twitching right before sleep IS common just before sleep for some people.
Meds can cause this or it can be a condition and there are meds to calm this.
I was very jerky legged and it is scary as well as frustrating.

Talk with your dr. or/and get 2nd opinion about your meds.
Be well, and welcome to PC