Find out how commercial charges do this - I think they're using the current flowing through the battery as measure, not sure, not a battery expert - and mimic that in your setup.
The hardest part is probably determining the 50% level, especially when the battery is under load/charging at the same time.