I got my mouse constructed recently, everything were fine except the external crystal wasn’t oscillating, so I used internal crystal instead.
There is always an first function you want to test on your mouse after all devices are properly settled. My favorite function is the one that makes mouse keep distance with wall by using 2 front sensors, also it’s the easiest function for me to write. Here is the code for this function:
int curt = micros();//start to track time in order to make one adjust every 1000us
setLeftPwm((2900- LFSensor));//set left motor speed
setRightPwm((2500 – RFSensor));//set right motor speed
TIM2->CNT=25000;//keep resetting encoder counts prevent overflow
elapseMicros(1000, curt);//elapse 1000 micro seconds
I also took a video to DEMO this function:
once you have this function working, PID is usually the next to test since we will be able to write a similar function to make mouse stop if there is a wall to the front when mouse is moving forward with PID. I will talk about how to write a simple PID by utlizing 2 side sensors next time.